1+ <LineChart Height =" 200" @ref =" lineChart" Width =" 500" />
2+
3+ @* You need to enable a date adapter for ChartJS, e.g. Luxon: *@
4+ <script src =" https://cdnjs.cloudflare.com/ajax/libs/luxon/3.5.0/luxon.min.js" integrity =" sha512-SN7iwxiJt9nFKiLayg3NjLItXPwRfBr4SQSIugMeBFrD4lIFJe1Z/exkTZYAg3Ul+AfZEGws2PQ+xSoaWfxRQQ==" crossorigin =" anonymous" referrerpolicy =" no-referrer" ></script >
5+ <script src =" https://cdnjs.cloudflare.com/ajax/libs/chartjs-adapter-luxon/1.3.1/chartjs-adapter-luxon.umd.js" integrity =" sha512-shMguvn9vyPHoXeEEPgqLX+f7zEHpuYbhK4xcBMS+IATkIj/1P+PhnWC4wMhFUekmSZqKTvL43cHnRkAlrMJ3Q==" crossorigin =" anonymous" referrerpolicy =" no-referrer" ></script >
6+
7+ <div class =" mt-5" >
8+ <Button Color =" ButtonColor.Primary" Class =" my-1 mr-1" @onclick =" @(async () => await TextLabels())" Size =" ButtonSize.Small" Type =" ButtonType.Button" > Text labels</Button >
9+ <Button Color =" ButtonColor.Primary" Class =" my-1 mr-1" @onclick =" @(async () => await DateLabels(" dd-MM-yyyy " ))" Size =" ButtonSize.Small" Type =" ButtonType.Button" > Date labels [European format]</Button >
10+ <Button Color =" ButtonColor.Primary" Class =" my-1 mr-1" @onclick =" @(async () => await DateLabels(" MM /dd /yy " ))" Size =" ButtonSize.Small" Type =" ButtonType.Button" > Date labels [US format]</Button >
11+ </div >
12+
13+ @code {
14+ private LineChart lineChart = default ! ;
15+ private LineChartOptions lineChartOptions = default ! ;
16+ private ChartData chartData = default ! ;
17+ private DateTime startOfYear = new DateTime ( DateTime .Today .Year , 1 , 1 );
18+
19+ protected override void OnInitialized ()
20+ {
21+ var colors = ColorUtility .CategoricalTwelveColors ;
22+
23+ var rng = new Random ();
24+ var labels = new List <string >();
25+ var data = new List <double ?>();
26+ for ( int i = 0 ; i < 20 ; i ++ )
27+ {
28+ labels .Add ( startOfYear .AddDays ( rng .Next ( 0 , 365 ) ).ToString ( " yyyy-MM-dd" ) );
29+ data .Add ( rng .Next ( 0 , 200 ) );
30+ }
31+
32+ var datasets = new List <IChartDataset >();
33+ labels .Sort ();
34+ var dataset = new LineChartDataset
35+ {
36+ Label = " Value" ,
37+ Data = data ,
38+ BackgroundColor = colors [ 0 ],
39+ BorderColor = colors [ 0 ],
40+ BorderWidth = 2 ,
41+ HoverBorderWidth = 4
42+ };
43+ datasets .Add ( dataset );
44+
45+ chartData = new ChartData { Labels = labels , Datasets = datasets };
46+
47+ lineChartOptions = new ();
48+ lineChartOptions .Responsive = true ;
49+ lineChartOptions .Interaction = new Interaction { Mode = InteractionMode .Index };
50+
51+ lineChartOptions .Scales .X ! .Title = new ChartAxesTitle { Text = " Day" , Display = true };
52+ lineChartOptions .Scales .Y ! .Title = new ChartAxesTitle { Text = " Value" , Display = true };
53+ }
54+
55+ protected async Task TextLabels ()
56+ {
57+ // Set the defaults
58+ lineChartOptions .Scales .X ! .Type = null ;
59+ lineChartOptions .Scales .X ! .Time = null ;
60+
61+ lineChartOptions .Scales .X ! .Min = null ;
62+ lineChartOptions .Scales .X ! .Max = null ;
63+
64+ await lineChart .UpdateAsync ( chartData , lineChartOptions );
65+ }
66+
67+ protected async Task DateLabels ( string format )
68+ {
69+ lineChartOptions .Scales .X ! .Type = ChartAxesType .Time ;
70+ lineChartOptions .Scales .X ! .Time = new ChartTimeAxisOptions ()
71+ {
72+ TooltipFormat = format ,
73+ DisplayFormats = new
74+ {
75+ month = format
76+ },
77+ Unit = ChartTimeUnit .Month
78+ };
79+
80+ lineChartOptions .Scales .X ! .Min = startOfYear .ToString ( " yyyy-MM-dd" );
81+ lineChartOptions .Scales .X ! .Max = startOfYear .AddYears ( 1 ).ToString ( " yyyy-MM-dd" );
82+
83+ await lineChart .UpdateAsync ( chartData , lineChartOptions );
84+ }
85+
86+ protected override async Task OnAfterRenderAsync ( bool firstRender )
87+ {
88+ if ( firstRender )
89+ {
90+ await lineChart .InitializeAsync ( chartData , lineChartOptions );
91+ }
92+ await base .OnAfterRenderAsync ( firstRender );
93+ }
94+
95+ }
0 commit comments