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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 125 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,125 @@
@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 = 7;
private int?[] datapoints;
andreassundstrom marked this conversation as resolved.
Show resolved Hide resolved
Random rand = new Random();
andreassundstrom marked this conversation as resolved.
Show resolved Hide resolved
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 };

IEnumerable<string> labels = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
andreassundstrom marked this conversation as resolved.
Show resolved Hide resolved

_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>
{
andreassundstrom marked this conversation as resolved.
Show resolved Hide resolved
new CategoryAxis
{
ScaleLabel = new ScaleLabel
{
LabelString = "Month"
}
}
},
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",
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 (default)",
BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Green),
BorderColor = ColorUtil.FromDrawingColor(ChartColors.Green),
Fill = FillingMode.Disabled,
LineTension = 0

};

_config.Data.Labels.AddRange(labels);
_config.Data.Datasets.Add(dataset1);
_config.Data.Datasets.Add(dataset2);
_config.Data.Datasets.Add(dataset3);
}

private void RandomizeData()
{

andreassundstrom marked this conversation as resolved.
Show resolved Hide resolved
for(int i = 0; i < datapoints.Count(); i++)
{
datapoints[i] = rand.NextDouble() < 0.05 ? null : (int?)RandomScalingFactor();
}

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

_chart.Update();
}
}
12 changes: 8 additions & 4 deletions ChartJs.Blazor.Samples/Server/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
"Title": "Basic",
"Path": "charts/line/basic"
},
{
"Title": "Stepped",
"Path": "charts/line/stepped"
}
{
"Title": "Stepped",
"Path": "charts/line/stepped"
},
{
"Title": "Interpolation",
"Path": "charts/line/interpolation"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should stay two-space-indented. You might need to edit this file outside of Visual Studio, it can mess with the json indents :/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is in .editorconfig, it has default to indent_size = 4. Is it ok to add:

[samples.json]
indent_size = 2

I will regardless of course edit the file to have correct spacing.

]
},
{
Expand Down