Skip to content

Commit 17b8ca1

Browse files
author
Mischa Spelt
committed
Merge branch 'pr-chartjs-time-options' into wise2
# Conflicts: # blazorbootstrap/Models/Charts/ChartOptions/ChartOptions.cs
2 parents 51b0690 + 809b533 commit 17b8ca1

File tree

4 files changed

+208
-183
lines changed

4 files changed

+208
-183
lines changed

BlazorBootstrap.Demo.RCL/Components/Pages/Charts/LineCharts/LineChartDocumentation.razor

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@
66

77
<h1>Blazor Line Chart</h1>
88
<div class="lead mb-3">
9-
A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time.
10-
It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value.
9+
A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time.
10+
It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value.
1111
</div>
1212

1313
<CarbonAds />
1414

1515
<SectionHeading Size="HeadingSize.H4" Text="Prerequisites" PageUrl="@pageUrl" HashTagName="prerequisites" />
1616
<div class="mb-3">
17-
Refer to the <a href="/getting-started/blazor-webassembly">getting started guide</a> for setting up charts.
17+
Refer to the <a href="/getting-started/blazor-webassembly">getting started guide</a> for setting up charts.
1818
</div>
1919

2020
<SectionHeading Size="HeadingSize.H4" Text="How it works" PageUrl="@pageUrl" HashTagName="how-it-works" />
2121
<div class="mb-3">
22-
In the following example, a <a href="/utils/color-utility#categorical-12-color">categorical 12-color</a> palette is used.
22+
In the following example, a <a href="/utils/color-utility#categorical-12-color">categorical 12-color</a> palette is used.
2323
</div>
2424
<Callout Heading="TIP" Color="CalloutColor.Success">
25-
For data visualization, you can use the predefined palettes <code>ColorUtility.CategoricalTwelveColors</code> for a 12-color palette and <code>ColorUtility.CategoricalSixColors</code> for a 6-color palette.
26-
These palettes offer a range of distinct and visually appealing colors that can be applied to represent different categories or data elements in your visualizations.
25+
For data visualization, you can use the predefined palettes <code>ColorUtility.CategoricalTwelveColors</code> for a 12-color palette and <code>ColorUtility.CategoricalSixColors</code> for a 6-color palette.
26+
These palettes offer a range of distinct and visually appealing colors that can be applied to represent different categories or data elements in your visualizations.
2727
</Callout>
2828
<Demo Type="typeof(LineChart_Demo_01_A_Examples)" Tabs="true" />
2929
<div class="my-3"></div>
3030
<Demo Type="typeof(LineChart_Demo_01_B_Examples)" Tabs="true" />
3131

3232
<SectionHeading Size="HeadingSize.H4" Text="Locale" PageUrl="@pageUrl" HashTagName="locale" />
3333
<div class="my-3">
34-
By default, the chart is using the default locale of the platform on which it is running.
35-
In the following example, you will see the chart in the <b>German</b> locale (<b>de_DE</b>).
34+
By default, the chart is using the default locale of the platform on which it is running.
35+
In the following example, you will see the chart in the <b>German</b> locale (<b>de_DE</b>).
3636
</div>
3737
<Demo Type="typeof(LineChart_Demo_02_Locale)" Tabs="true" />
3838

@@ -48,9 +48,12 @@
4848
<SectionHeading Size="HeadingSize.H4" Text="Fill between datasets" PageUrl="@pageUrl" HashTagName="dataset-fill" />
4949
<Demo Type="typeof(LineChart_Demo_06_Dataset_Fill)" Tabs="true" />
5050

51+
<SectionHeading Size="HeadingSize.H4" Text="Time Axis" PageUrl="@pageUrl" HashTagName="time-axis" />
52+
<Demo Type="typeof(LineChart_Demo_07_Time_Axis)" Tabs="true" />
53+
5154
@code {
52-
private readonly string pageUrl = "/charts/line-chart";
53-
private readonly string title = "Blazor Line Chart";
54-
private readonly string description = "A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time. It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value.";
55-
private readonly string imageUrl = "https://i.imgur.com/8b7jH0D.png";
55+
private readonly string pageUrl = "/charts/line-chart";
56+
private readonly string title = "Blazor Line Chart";
57+
private readonly string description = "A Blazor Bootstrap line chart component is a graphical representation of data that uses a series of connected points to show how the data changes over time. It is a type of x-y chart, where the x-axis represents the independent variable, such as time, and the y-axis represents the dependent variable, such as the value.";
58+
private readonly string imageUrl = "https://i.imgur.com/8b7jH0D.png";
5659
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)