1+ <LineChart @ref =" lineChart" Width =" 600" />
2+
3+ @code {
4+ private LineChart lineChart = default ! ;
5+ private LineChartOptions lineChartOptions = default ! ;
6+ private ChartData chartData = default ! ;
7+
8+ private int dataCount = 12 ;
9+ private List <double ? > datapoints = [0 , 20 , 20 , 60 , 60 , 120 , null , 180 , 120 , 125 , 105 , 110 , 170 ];
10+ private int datasetsCount ;
11+ private Random random = new ();
12+
13+ protected override void OnInitialized ()
14+ {
15+ chartData = new ChartData { Labels = GetDefaultDataLabels (), Datasets = GetDefaultDataSets () };
16+ lineChartOptions = new ()
17+ {
18+ IndexAxis = " x" ,
19+ Interaction = new Interaction { Mode = InteractionMode .Index , Intersect = false },
20+ Responsive = true ,
21+ };
22+ }
23+
24+ protected override async Task OnAfterRenderAsync (bool firstRender )
25+ {
26+ if (firstRender )
27+ {
28+ await lineChart .InitializeAsync (chartData , lineChartOptions );
29+ }
30+ await base .OnAfterRenderAsync (firstRender );
31+ }
32+
33+ #region Data Preparation
34+
35+ private List <string > GetDefaultDataLabels ()
36+ {
37+ var labels = new List <string >();
38+
39+ for (var index = 0 ; index < dataCount ; index ++ )
40+ labels .Add (index .ToString ());
41+
42+ return labels ;
43+ }
44+
45+ private List <IChartDataset > GetDefaultDataSets ()
46+ {
47+ var datasets = new List <IChartDataset >();
48+
49+ // 1.
50+ datasets .Add (new LineChartDataset
51+ {
52+ Label = " Cubic interpolation (monotone)" ,
53+ Data = datapoints ,
54+ BorderColor = ColorUtility .CategoricalSixColors [0 ].ToColor ().ToRgbString (),
55+ CubicInterpolationMode = " monotone" ,
56+ Fill = false ,
57+ Tension = 0 . 4 ,
58+ });
59+
60+ // 2.
61+ datasets .Add (new LineChartDataset
62+ {
63+ Label = " Cubic interpolation" ,
64+ Data = datapoints ,
65+ BorderColor = ColorUtility .CategoricalSixColors [1 ].ToColor ().ToRgbString (),
66+ Fill = false ,
67+ Tension = 0 . 4 ,
68+ });
69+
70+ // 3.
71+ datasets .Add (new LineChartDataset
72+ {
73+ Label = " Linear interpolation (default)" ,
74+ Data = datapoints ,
75+ BorderColor = ColorUtility .CategoricalSixColors [2 ].ToColor ().ToRgbString (),
76+ Fill = false ,
77+ });
78+
79+ return datasets ;
80+ }
81+
82+ #endregion Data Preparation
83+ }
0 commit comments