Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Line Chart Interpolation sample #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ trim_trailing_whitespace = true
[project.json]
indent_size = 2

[samples.json]
indent_size = 2

# C# files
[*.cs]
# New line preferences
Expand Down
118 changes: 118 additions & 0 deletions ChartJs.Blazor.Samples/Client/Pages/Charts/Line/Interpolation.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
@page "/charts/line/interpolation"
@using ChartJs.Blazor.LineChart
@layout SampleLayout

<Chart Config="_config" @ref="_chart"></Chart>

<button @onclick="RandomizeData">Randomize Data</button>

@code {
private const int InitalCount = 12;
private int?[] _datapoints;
private Random _rng = new Random();
private LineConfig _config;
private Chart _chart;

protected override void OnInitialized()
{
_datapoints = new int?[] { 0, 20, 20, 60, 60, 120, null, 180, 120, 125, 105, 110, 170 };

_config = new LineConfig
{
Options = new LineOptions
{
Responsive = true,
Title = new OptionsTitle
{
Display = true,
Text = "ChartJs.Blazor Line Chart - Cubic interpolation mode"
},
Tooltips = new Tooltips
{
Mode = InteractionMode.Nearest,
Intersect = true
},
Hover = new Hover
{
Mode = InteractionMode.Nearest,
Intersect = true
},
Scales = new Scales
{
XAxes = new List<CartesianAxis>
{
new CategoryAxis
{
ScaleLabel = new ScaleLabel()
}
},
YAxes = new List<CartesianAxis>
{
new LinearCartesianAxis
{
ScaleLabel = new ScaleLabel
{
LabelString = "Value",
Display = true
},
Ticks = new LinearCartesianTicks
{
SuggestedMin = -10,
SuggestedMax = 200
}
}
}
}
}
};

IDataset<int?> dataset1 = new LineDataset<int?>(_datapoints)
{
Label = "Cubic interpolation (monotone)",
BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Red),
BorderColor = ColorUtil.FromDrawingColor(ChartColors.Red),
Fill = FillingMode.Disabled,
CubicInterpolationMode = CubicInterpolationMode.Monotone,
LineTension = 0.4
};

IDataset<int?> dataset2 = new LineDataset<int?>(_datapoints)
{
Label = "Cubic interpolation (default)",
BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Blue),
BorderColor = ColorUtil.FromDrawingColor(ChartColors.Blue),
Fill = FillingMode.Disabled,
LineTension = 0.4
};

IDataset<int?> dataset3 = new LineDataset<int?>(_datapoints)
{
Label = "Linear interpolation",
BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Green),
BorderColor = ColorUtil.FromDrawingColor(ChartColors.Green),
Fill = FillingMode.Disabled,
LineTension = 0
};

_config.Data.Labels.AddRange(Enumerable.Range(0, InitalCount).Select(i => i.ToString()));
_config.Data.Datasets.Add(dataset1);
_config.Data.Datasets.Add(dataset2);
_config.Data.Datasets.Add(dataset3);
}

private void RandomizeData()
{
for(int i = 0; i < _datapoints.Count(); i++)
{
_datapoints[i] = _rng.NextDouble() < 0.05 ? null : (int?)RandomScalingFactor();
}

foreach (IDataset<int?> dataset in _config.Data.Datasets)
{
dataset.Clear();
dataset.AddRange(_datapoints);
}

_chart.Update();
}
}
4 changes: 4 additions & 0 deletions ChartJs.Blazor.Samples/Server/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
{
"Title": "Stepped",
"Path": "charts/line/stepped"
},
{
"Title": "Interpolation",
"Path": "charts/line/interpolation"
}
]
},
Expand Down