Skip to content

Commit 4d21b7e

Browse files
committed
Add animation support to BarChart components and docs
This commit introduces new sections in the `BarChartDocumentation.razor` file to document animations related to chart delays, including "Animations - Delay" and "Animations - DataSet level delay". New `Animation` properties of type `ChartAnimation?` are added to the `ChartDataset<TData>` and `ChartOptions` classes, allowing for configurable animations with detailed XML documentation. The `ChartAnimation` class is introduced, featuring properties like `Delay`, `Duration`, `Easing`, and `Loop`, each with comprehensive documentation. Demo files `BarChart_Demo_07_Animations_A_Delay.razor` and `BarChart_Demo_08_Animations_B_DataSet_Level_Delay.razor` are updated to showcase animation settings, with the first demo applying a global delay and the second demonstrating dataset-level customization. Additionally, several properties in the `ChartOptions` class have had their `[Parameter]` attributes removed, reflecting a change in their intended usage. NOTE: This commit message is auto-generated using GitHub Copilot.
1 parent 49ae6ca commit 4d21b7e

5 files changed

Lines changed: 224 additions & 6 deletions

File tree

BlazorExpress.ChartJS.Demo.RCL/Pages/Demos/BarChart/BarChartDocumentation.razor

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@
106106
<Demo Type="typeof(BarChart_Demo_06_Border_Radius)" Tabs="true" />
107107
</Section>
108108

109+
<Section Class="p-0" Size="HeadingSize.H4" Name="Animations - Delay" PageUrl="@pageUrl" Link="animations-delay">
110+
<Block>
111+
TODO: Update the description once the demo is implemented.
112+
</Block>
113+
<Demo Type="typeof(BarChart_Demo_07_Animations_A_Delay)" Tabs="true" />
114+
</Section>
115+
116+
<Section Class="p-0" Size="HeadingSize.H4" Name="Animations - DataSet level delay" PageUrl="@pageUrl" Link="animations-dataset-level-delay">
117+
<Block>
118+
TODO: Update the description once the demo is implemented.
119+
</Block>
120+
<Demo Type="typeof(BarChart_Demo_08_Animations_B_DataSet_Level_Delay)" Tabs="true" />
121+
</Section>
122+
109123
@code {
110124
private const string pageUrl = DemoRouteConstants.Demos_BarChart;
111125
private const string pageUrl2 = DemoRouteConstants.Demos_Prefix;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<BarChart @ref="barChart" Width="600" />
2+
3+
@code {
4+
private BarChart barChart = default!;
5+
private BarChartOptions barChartOptions = default!;
6+
private ChartData chartData = default!;
7+
8+
protected override void OnInitialized()
9+
{
10+
var colors = ColorUtility.CategoricalTwelveColors;
11+
12+
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
13+
var datasets = new List<IChartDataset>();
14+
15+
var dataset1 = new BarChartDataset()
16+
{
17+
Label = "Windows",
18+
Data = new List<double?> { 28000, 8000, 2000, 17000 },
19+
BackgroundColor = new List<string> { colors[3] },
20+
BorderColor = new List<string> { colors[3] },
21+
BorderWidth = new List<double> { 0 }
22+
};
23+
datasets.Add(dataset1);
24+
25+
var dataset2 = new BarChartDataset()
26+
{
27+
Label = "macOS",
28+
Data = new List<double?> { 8000, 10000, 14000, 8000 },
29+
BackgroundColor = new List<string> { colors[4] },
30+
BorderColor = new List<string> { colors[4] },
31+
BorderWidth = new List<double> { 0 }
32+
};
33+
datasets.Add(dataset2);
34+
35+
var dataset3 = new BarChartDataset()
36+
{
37+
Label = "Other",
38+
Data = new List<double?> { 28000, 10000, 14000, 8000 },
39+
BackgroundColor = new List<string> { colors[5] },
40+
BorderColor = new List<string> { colors[5] },
41+
BorderWidth = new List<double> { 0 }
42+
};
43+
datasets.Add(dataset3);
44+
45+
chartData = new ChartData { Labels = labels, Datasets = datasets };
46+
47+
barChartOptions = new();
48+
barChartOptions.Responsive = true;
49+
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
50+
51+
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
52+
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
53+
54+
barChartOptions.Scales.X.Stacked = true;
55+
barChartOptions.Scales.Y.Stacked = true;
56+
57+
barChartOptions.Plugins.Title!.Text = "Operating system";
58+
barChartOptions.Plugins.Title.Display = true;
59+
60+
barChartOptions.Animation = new ChartAnimation { Duration = 1000, Delay = 1500 };
61+
}
62+
63+
protected override async Task OnAfterRenderAsync(bool firstRender)
64+
{
65+
if (firstRender)
66+
{
67+
await barChart.InitializeAsync(chartData, barChartOptions);
68+
}
69+
await base.OnAfterRenderAsync(firstRender);
70+
}
71+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<BarChart @ref="barChart" Width="600" />
2+
3+
@code {
4+
private BarChart barChart = default!;
5+
private BarChartOptions barChartOptions = default!;
6+
private ChartData chartData = default!;
7+
8+
protected override void OnInitialized()
9+
{
10+
var colors = ColorUtility.CategoricalTwelveColors;
11+
12+
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
13+
var datasets = new List<IChartDataset>();
14+
15+
var dataset1 = new BarChartDataset()
16+
{
17+
Label = "Windows",
18+
Data = new List<double?> { 28000, 8000, 2000, 17000 },
19+
BackgroundColor = new List<string> { colors[6] },
20+
BorderColor = new List<string> { colors[6] },
21+
BorderWidth = new List<double> { 0 },
22+
Animation = new ChartAnimation { Duration = 1000, Delay = 500 }
23+
};
24+
datasets.Add(dataset1);
25+
26+
var dataset2 = new BarChartDataset()
27+
{
28+
Label = "macOS",
29+
Data = new List<double?> { 8000, 10000, 14000, 8000 },
30+
BackgroundColor = new List<string> { colors[7] },
31+
BorderColor = new List<string> { colors[7] },
32+
BorderWidth = new List<double> { 0 },
33+
Animation = new ChartAnimation { Duration = 1000, Delay = 1000 }
34+
};
35+
datasets.Add(dataset2);
36+
37+
var dataset3 = new BarChartDataset()
38+
{
39+
Label = "Other",
40+
Data = new List<double?> { 28000, 10000, 14000, 8000 },
41+
BackgroundColor = new List<string> { colors[8] },
42+
BorderColor = new List<string> { colors[8] },
43+
BorderWidth = new List<double> { 0 },
44+
Animation = new ChartAnimation { Duration = 1000, Delay = 1500 }
45+
};
46+
datasets.Add(dataset3);
47+
48+
chartData = new ChartData { Labels = labels, Datasets = datasets };
49+
50+
barChartOptions = new();
51+
barChartOptions.Responsive = true;
52+
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
53+
54+
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
55+
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
56+
57+
barChartOptions.Scales.X.Stacked = true;
58+
barChartOptions.Scales.Y.Stacked = true;
59+
60+
barChartOptions.Plugins.Title!.Text = "Operating system";
61+
barChartOptions.Plugins.Title.Display = true;
62+
}
63+
64+
protected override async Task OnAfterRenderAsync(bool firstRender)
65+
{
66+
if (firstRender)
67+
{
68+
await barChart.InitializeAsync(chartData, barChartOptions);
69+
}
70+
await base.OnAfterRenderAsync(firstRender);
71+
}
72+
}

BlazorExpress.ChartJS/Models/ChartDataset/ChartDataset.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public ChartDataset()
1919

2020
#region Properties, Indexers
2121

22+
/// <summary>
23+
/// Chart.js animates charts out of the box.
24+
/// A number of options are provided to configure how the animation looks and how long it takes.
25+
/// </summary>
26+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
27+
public ChartAnimation? Animation { get; set; }
28+
2229
/// <summary>
2330
/// How to clip relative to chartArea. Positive value allows overflow, negative value clips that
2431
/// many pixels inside chartArea. 0 = clip at chartArea. Clipping can also be configured

BlazorExpress.ChartJS/Models/ChartOptions/ChartOptions.cs

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public class ChartOptions : IChartOptions
1212
{
1313
#region Properties, Indexers
1414

15+
/// <summary>
16+
/// Chart.js animates charts out of the box.
17+
/// A number of options are provided to configure how the animation looks and how long it takes.
18+
/// </summary>
19+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
20+
public ChartAnimation? Animation { get; set; }
21+
1522
//aspectRatio
1623
//https://www.chartjs.org/docs/latest/configuration/responsive.html#configuration-options
1724

@@ -27,7 +34,7 @@ public class ChartOptions : IChartOptions
2734
[DefaultValue("By default, the chart is using the default locale of the platform which is running on.")]
2835
[Description("Gets or sets the locale for the chart.")]
2936
[ParameterTypeName("string?")]
30-
[Parameter]
37+
//[Parameter]
3138
public string? Locale { get; set; }
3239

3340
/// <summary>
@@ -42,7 +49,7 @@ public class ChartOptions : IChartOptions
4249
[AddedVersion("1.0.0")]
4350
[DefaultValue(true)]
4451
[Description("Gets or sets a value indicating whether to maintain the original canvas aspect ratio (width / height) when resizing.")]
45-
[Parameter]
52+
//[Parameter]
4653
public bool MaintainAspectRatio { get; set; } = true;
4754

4855
//onResize
@@ -63,7 +70,7 @@ public class ChartOptions : IChartOptions
6370
[AddedVersion("1.0.0")]
6471
[DefaultValue(true)]
6572
[Description("Gets or sets a value indicating whether the chart canvas should resize when its container does.")]
66-
[Parameter]
73+
//[Parameter]
6774
public bool Responsive { get; set; } = true;
6875

6976
#endregion
@@ -86,7 +93,7 @@ public class ChartLayout
8693
[AddedVersion("1.0.0")]
8794
[DefaultValue(true)]
8895
[Description("Gets or sets a value indicating whether to apply automatic padding so visible elements are completely drawn.")]
89-
[Parameter]
96+
//[Parameter]
9097
public bool AutoPadding { get; set; } = true;
9198

9299
/// <summary>
@@ -98,7 +105,7 @@ public class ChartLayout
98105
[AddedVersion("1.0.0")]
99106
[DefaultValue(0)]
100107
[Description("Gets or sets the padding to add inside the chart.")]
101-
[Parameter]
108+
//[Parameter]
102109
public int Padding { get; set; } = 0;
103110

104111
#endregion
@@ -610,7 +617,7 @@ public TitleAlignment TitleAlignment
610617
}
611618

612619
/// <summary>
613-
/// <see href="https://www.chartjs.org/docs/latest/general/fonts.html" />
620+
/// <see href="https://www.chartjs.org/docs/latest/general/fonts.html" />
614621
/// </summary>
615622
public class ChartFont
616623
{
@@ -650,3 +657,50 @@ public class ChartFont
650657

651658
#endregion
652659
}
660+
661+
/// <summary>
662+
/// Chart.js animates charts out of the box.
663+
/// A number of options are provided to configure how the animation looks and how long it takes.
664+
/// <see href="https://www.chartjs.org/docs/latest/configuration/animations.html#animations" />
665+
/// </summary>
666+
public class ChartAnimation
667+
{
668+
/// <summary>
669+
/// Delay before starting the animations.
670+
/// <para>
671+
/// Default value is <see langword="null" />.
672+
/// </para>
673+
/// </summary>
674+
public double? Delay { get; set; } = null;
675+
676+
/// <summary>
677+
/// The number of milliseconds an animation takes.
678+
/// <para>
679+
/// Default value is 1000
680+
/// </para>
681+
/// </summary>
682+
public double Duration { get; set; } = 1000;
683+
684+
/// <summary>
685+
/// Easing function to use.
686+
/// <para>
687+
/// Default value is "easeOutQuart".
688+
/// </para>
689+
/// <para>
690+
/// Supported values are: 'linear', 'easeInQuad', 'easeOutQuad', 'easeInOutQuad', 'easeInCubic', 'easeOutCubic', 'easeInOutCubic', 'easeInQuart', 'easeOutQuart', 'easeInOutQuart', 'easeInQuint', 'easeOutQuint', 'easeInOutQuint', 'easeInSine', 'easeOutSine', 'easeInOutSine', 'easeInExpo', 'easeOutExpo', 'easeInOutExpo', 'easeInCirc', 'easeOutCirc', 'easeInOutCirc', 'easeInElastic', 'easeOutElastic', 'easeInOutElastic', 'easeInBack', 'easeOutBack', 'easeInOutBack', 'easeInBounce', 'easeOutBounce', and 'easeInOutBounce'.
691+
/// </para>
692+
/// <see href="https://www.chartjs.org/docs/latest/configuration/animations.html#easing" />
693+
/// </summary>
694+
public string Easing { get; set; } = "easeOutQuart";
695+
696+
/// <summary>
697+
/// If set to <see langword="true"/>, the animations loop endlessly.
698+
/// <para>
699+
/// Default value is <see langword="null" />.
700+
/// </para>
701+
/// </summary>
702+
public bool? Loop { get; set; } = null;
703+
704+
//Animation Callbacks
705+
//https://www.chartjs.org/docs/latest/configuration/animations.html#animation-callbacks
706+
}

0 commit comments

Comments
 (0)