From 809b533dd8006c5fe9bfd282d1b23e8b98cdcd87 Mon Sep 17 00:00:00 2001 From: Mischa Spelt Date: Thu, 12 Sep 2024 10:30:57 +0200 Subject: [PATCH] Added missing documentation file --- .../LineChart_Demo_07_Time_Axis.razor | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChart_Demo_07_Time_Axis.razor diff --git a/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChart_Demo_07_Time_Axis.razor b/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChart_Demo_07_Time_Axis.razor new file mode 100644 index 000000000..eb12a1062 --- /dev/null +++ b/BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChart_Demo_07_Time_Axis.razor @@ -0,0 +1,95 @@ + + +@* You need to enable a date adapter for ChartJS, e.g. Luxon: *@ + + + +
+ + + +
+ +@code { + private LineChart lineChart = default!; + private LineChartOptions lineChartOptions = default!; + private ChartData chartData = default!; + private DateTime startOfYear = new DateTime( DateTime.Today.Year, 1, 1 ); + + protected override void OnInitialized() + { + var colors = ColorUtility.CategoricalTwelveColors; + + var rng = new Random(); + var labels = new List(); + var data = new List(); + for( int i = 0; i < 20; i++ ) + { + labels.Add( startOfYear.AddDays( rng.Next( 0, 365 ) ).ToString( "yyyy-MM-dd" ) ); + data.Add( rng.Next( 0, 200 ) ); + } + + var datasets = new List(); + labels.Sort(); + var dataset = new LineChartDataset + { + Label = "Value", + Data = data, + BackgroundColor = colors[ 0 ], + BorderColor = colors[ 0 ], + BorderWidth = 2, + HoverBorderWidth = 4 + }; + datasets.Add( dataset ); + + chartData = new ChartData { Labels = labels, Datasets = datasets }; + + lineChartOptions = new(); + lineChartOptions.Responsive = true; + lineChartOptions.Interaction = new Interaction { Mode = InteractionMode.Index }; + + lineChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Day", Display = true }; + lineChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Value", Display = true }; + } + + protected async Task TextLabels() + { + // Set the defaults + lineChartOptions.Scales.X!.Type = null; + lineChartOptions.Scales.X!.Time = null; + + lineChartOptions.Scales.X!.Min = null; + lineChartOptions.Scales.X!.Max = null; + + await lineChart.UpdateAsync( chartData, lineChartOptions ); + } + + protected async Task DateLabels( string format ) + { + lineChartOptions.Scales.X!.Type = ChartAxesType.Time; + lineChartOptions.Scales.X!.Time = new ChartTimeAxisOptions() + { + TooltipFormat = format, + DisplayFormats = new + { + month = format + }, + Unit = ChartTimeUnit.Month + }; + + lineChartOptions.Scales.X!.Min = startOfYear.ToString( "yyyy-MM-dd" ); + lineChartOptions.Scales.X!.Max = startOfYear.AddYears( 1 ).ToString( "yyyy-MM-dd" ); + + await lineChart.UpdateAsync( chartData, lineChartOptions ); + } + + protected override async Task OnAfterRenderAsync( bool firstRender ) + { + if( firstRender ) + { + await lineChart.InitializeAsync( chartData, lineChartOptions ); + } + await base.OnAfterRenderAsync( firstRender ); + } + +} \ No newline at end of file