Skip to content

Commit

Permalink
Merge pull request #112 from Joelius300/feature/dataset-rework
Browse files Browse the repository at this point in the history
Dataset rework
  • Loading branch information
mariusmuntean authored May 23, 2020
2 parents 4d99fa5 + d80f3f6 commit d9a313b
Show file tree
Hide file tree
Showing 63 changed files with 1,730 additions and 918 deletions.
105 changes: 105 additions & 0 deletions ChartJs.Blazor.Tests/ClippingTests.Deserialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using ChartJs.Blazor.ChartJS.Common;
using ChartJs.Blazor.ChartJS.LineChart;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xunit;

namespace ChartJs.Blazor.Tests
{
public partial class ClippingTests
{
[Theory]
[InlineData(0)]
[InlineData(-100)]
[InlineData(100)]
[InlineData(int.MinValue)]
[InlineData(int.MaxValue)]
public void Deserialize_AllEdgesEqual_FromRoot(int value)
{
// Arrange
string json = value.ToString(CultureInfo.InvariantCulture);
Clipping expected = new Clipping(value);

// Act
Clipping deserialized = JsonConvert.DeserializeObject<Clipping>(json);

// Assert
Assert.Equal(expected, deserialized);
}

[Fact]
public void Deserialize_DifferentEdges_FromRoot()
{
// Arrange
const string json = "{\"Bottom\":0,\"Left\":false,\"Top\":10,\"Right\":-100}";
Clipping expected = new Clipping(top: 10, right: -100, bottom: 0, left: null);

// Act
Clipping deserialized = JsonConvert.DeserializeObject<Clipping>(json);

// Assert
Assert.Equal(expected, deserialized);
}

[Fact]
public void Deserialize_DifferentEdges_MissingMembers_FromRoot()
{
// Arrange
const string json = "{\"Top\":-123,\"Right\":false}";
Clipping expected = new Clipping(top: -123);

// Act
Clipping deserialized = JsonConvert.DeserializeObject<Clipping>(json);

// Assert
Assert.Equal(expected, deserialized);
}

[Fact]
public void Deserialize_DifferentEdges_AdditionalMembers_FromRoot()
{
// Arrange
const string json = "{\"Left\":500,\"Top\":false,\"ABC\":19.2}";
Clipping expected = new Clipping(left: 500);

// Act
Clipping deserialized = JsonConvert.DeserializeObject<Clipping>(json);

// Assert
Assert.Equal(expected, deserialized);
}

[Fact]
public void Deserialize_Double_ThrowsJsonReaderException()
{
// Arrange
const string json = "19.2";

// Act & Assert
Assert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<Clipping>(json));
}

[Fact]
public void Deserialize_Array_ThrowsJsonReaderException()
{
// Arrange
const string json = "[]";

// Act & Assert
Assert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<Clipping>(json));
}

[Fact]
public void Deserialize_String_ThrowsJsonReaderException()
{
// Arrange
const string json = "\"asdf\"";

// Act & Assert
Assert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<Clipping>(json));
}
}
}
58 changes: 58 additions & 0 deletions ChartJs.Blazor.Tests/ClippingTests.Equality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using ChartJs.Blazor.ChartJS.Common;
using ChartJs.Blazor.ChartJS.LineChart;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace ChartJs.Blazor.Tests
{
public partial class ClippingTests
{
[Fact]
public void Equals_SameAll_ReturnsTrue()
{
// Arrange
const int Value = 10;
Clipping a = new Clipping(Value);
Clipping b = new Clipping(Value);

// Act
bool equals = a.Equals(b);

// Assert
Assert.True(equals);
}

[Fact]
public void Equals_AllAndIndividual_ReturnsTrue()
{
// Arrange
const int Value = -10;
Clipping a = new Clipping(Value);
Clipping b = new Clipping(Value, Value, Value, Value);

// Act
bool equals = a.Equals(b);

// Assert
Assert.True(equals);
}

[Fact]
public void GetHashCode_AllAndIndividual_Equals()
{
// Arrange
const int Value = 12345;
Clipping a = new Clipping(Value);
Clipping b = new Clipping(Value, Value, Value, Value);

// Act
int hashA = a.GetHashCode();
int hashB = b.GetHashCode();

// Assert
Assert.Equal(hashA, hashB);
}
}
}
47 changes: 47 additions & 0 deletions ChartJs.Blazor.Tests/ClippingTests.Serialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using ChartJs.Blazor.ChartJS.Common;
using ChartJs.Blazor.ChartJS.LineChart;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xunit;

namespace ChartJs.Blazor.Tests
{
public partial class ClippingTests
{
[Theory]
[InlineData(0)]
[InlineData(-100)]
[InlineData(100)]
[InlineData(int.MinValue)]
[InlineData(int.MaxValue)]
public void Serialize_AllEdgesEqual_AsRoot(int value)
{
// Arrange
string Expected = value.ToString(CultureInfo.InvariantCulture);
Clipping clipping = new Clipping(value);

// Act
string serialized = JsonConvert.SerializeObject(clipping);

// Assert
Assert.Equal(Expected, serialized);
}

[Fact]
public void Serialize_DifferentEdges_AsRoot()
{
// Arrange
const string Expected = "{\"Top\":0,\"Right\":false,\"Bottom\":10,\"Left\":-100}";
Clipping clipping = new Clipping(top: 0, right: null, bottom: 10, left: -100);

// Act
string serialized = JsonConvert.SerializeObject(clipping);

// Assert
Assert.Equal(Expected, serialized);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@using ChartJs.Blazor.ChartJS.BarChart.Axes
@using ChartJs.Blazor.ChartJS.Common
@using ChartJs.Blazor.ChartJS.Common.Axes
@using ChartJs.Blazor.ChartJS.Common.Wrappers
@using ChartJs.Blazor.ChartJS.Common.Axes.Ticks
@using ChartJs.Blazor.ChartJS.Common.Handlers
@using ChartJs.Blazor.Interop
Expand All @@ -22,7 +21,7 @@
private ChartJsBarChart barChartJs;
private BarConfig _config;
public int Rows { get; set; } = 3;
private BarDataset<DoubleWrapper> _barDataSet;
private BarDataset<double> _barDataSet;

public string EventArgs { get; set; } = "";

Expand Down Expand Up @@ -70,7 +69,7 @@

_config.Data.Labels.AddRange(new[] {"A", "B", "C", "D"});

_barDataSet = new BarDataset<DoubleWrapper>
_barDataSet = new BarDataset<double>
{
Label = "My double dataset",
BackgroundColor = new[] {ColorUtil.RandomColorString(), ColorUtil.RandomColorString(), ColorUtil.RandomColorString(), ColorUtil.RandomColorString()},
Expand All @@ -81,7 +80,7 @@
BorderColor = "#ffffff"
};

_barDataSet.AddRange(new double[] {8, 5, 3, 7}.Wrap());
_barDataSet.AddRange(new double[] { 8, 5, 3, 7 });
_config.Data.Datasets.Add(_barDataSet);
}

Expand All @@ -100,4 +99,4 @@
Console.WriteLine($"{System.Threading.Interlocked.Increment(ref s_eventCount)}. event");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@using ChartJs.Blazor.ChartJS.BarChart.Axes
@using ChartJs.Blazor.ChartJS.Common.Enums
@using ChartJs.Blazor.ChartJS.Common.Axes
@using ChartJs.Blazor.ChartJS.Common.Wrappers
@using ChartJs.Blazor.ChartJS.Common.Axes.Ticks
@using ChartJs.Blazor.ChartJS.Common.Handlers
@using ChartJs.Blazor.Interop
Expand Down Expand Up @@ -84,7 +83,7 @@
BorderColor = "#ffffff",
};

pieSet.Data.AddRange(new double[] { 4, 5, 6, 7, 8 });
pieSet.AddRange(new double[] { 4, 5, 6, 7, 8 });
_config.Data.Datasets.Add(pieSet);
}

Expand All @@ -96,7 +95,7 @@
var rand = new Random();
var newVal = 1 + rand.NextDouble() * 10;
_config.Data.Labels.Add(newVal.ToString(CultureInfo.InvariantCulture));
dataset.Data.Add(newVal);
dataset.Add(newVal);
dataset.BackgroundColor = dataset.BackgroundColor.IndexedValues.Append(ColorUtil.RandomColorString()).ToArray();

await _pieChartJs.Update();
Expand All @@ -108,7 +107,7 @@
if (dataset == null || dataset.Data.Count < 1) return;

_config.Data.Labels.RemoveAt(_config.Data.Labels.Count - 1);
dataset.Data.RemoveAt(dataset.Data.Count - 1);
dataset.RemoveAt(dataset.Data.Count - 1);

await _pieChartJs.Update();
}
Expand All @@ -128,7 +127,7 @@
int count = _config.Data.Labels.Count;

var rand = new Random();
pieSet.Data.AddRange(Enumerable.Range(0, count).Select(i => Convert.ToDouble(rand.Next(0, 10))));
pieSet.AddRange(Enumerable.Range(0, count).Select(i => Convert.ToDouble(rand.Next(0, 10))));
pieSet.BackgroundColor = Enumerable.Range(0, count).Select(i => ColorUtil.RandomColorString()).ToArray();

_config.Data.Datasets.Add(pieSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@using ChartJs.Blazor.ChartJS.Common.Axes.Ticks
@using ChartJs.Blazor.ChartJS.Common.Enums
@using ChartJs.Blazor.ChartJS.Common.Properties
@using ChartJs.Blazor.ChartJS.Common.Wrappers
@using ChartJs.Blazor.Charts
@using ChartJs.Blazor.Util
<h2>Horizontal Bar Chart</h2>
Expand All @@ -20,7 +19,7 @@
{
private BarConfig _barChartConfig;
private ChartJsBarChart _barChart;
private BarDataset<DoubleWrapper> _barDataSet;
private BarDataset<double> _barDataSet;

protected override Task OnInitializedAsync()
{
Expand Down Expand Up @@ -55,15 +54,15 @@
_barChartConfig.Data.Labels.AddRange(new[] { "A", "B", "C", "D" });

//Note the constructor argument
_barDataSet = new BarDataset<DoubleWrapper>(ChartType.HorizontalBar)
_barDataSet = new BarDataset<double>(horizontal: true)
{
Label = "My double dataset",
BackgroundColor = new[] { ColorUtil.RandomColorString(), ColorUtil.RandomColorString(), ColorUtil.RandomColorString(), ColorUtil.RandomColorString() },
BorderColor = ColorUtil.RandomColorString(),
BorderWidth = 1
};

_barDataSet.AddRange(new double[] { 8, 5, 3, 7 }.Wrap());
_barDataSet.AddRange(new double[] { 8, 5, 3, 7 });
_barChartConfig.Data.Datasets.Add(_barDataSet);

return Task.CompletedTask;
Expand All @@ -73,7 +72,7 @@
{
var nowSecond = DateTime.Now.Second;
_barChartConfig.Data.Labels.Add(nowSecond.ToString());
_barDataSet.Add(new DoubleWrapper(nowSecond));
_barDataSet.Add(nowSecond);
_barDataSet.BackgroundColor = _barChartConfig.Data.Labels.Select(l => ColorUtil.RandomColorString()).ToArray();

_barChartConfig.Options.Title.Text += DateTime.Now.Second.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@using ChartJs.Blazor.ChartJS.Common.Axes
@using ChartJs.Blazor.ChartJS.Common.Axes.Ticks
@using ChartJs.Blazor.ChartJS.Common.Properties
@using ChartJs.Blazor.ChartJS.Common.Wrappers
@using ChartJs.Blazor.Charts
@using ChartJs.Blazor.Util

Expand All @@ -26,7 +25,7 @@

private BarConfig _barChartConfig;
private ChartJsBarChart _barChart;
private BarDataset<DoubleWrapper> _barDataSet;
private BarDataset<double> _barDataSet;

protected override void OnInitialized()
{
Expand Down Expand Up @@ -65,7 +64,7 @@

_barChartConfig.Data.Labels.AddRange(new[] { "A", "B", "C", "D" });

_barDataSet = new BarDataset<DoubleWrapper>
_barDataSet = new BarDataset<double>
{
Label = "My double dataset",
BackgroundColor = new[] { ColorUtil.RandomColorString(), ColorUtil.RandomColorString(), ColorUtil.RandomColorString(), ColorUtil.RandomColorString() },
Expand All @@ -76,15 +75,15 @@
BorderColor = "#ffffff"
};

_barDataSet.AddRange(new double[] { 8, 5, 3, 7 }.Wrap());
_barDataSet.AddRange(new double[] { 8, 5, 3, 7 });
_barChartConfig.Data.Datasets.Add(_barDataSet);
}

private async Task AddData()
{
var nowSecond = DateTime.Now.Second;
_barChartConfig.Data.Labels.Add(nowSecond.ToString());
_barDataSet.Add(new DoubleWrapper(nowSecond));
_barDataSet.Add(nowSecond);

await _barChart.Update();
}
Expand Down
Loading

0 comments on commit d9a313b

Please sign in to comment.