Skip to content

Commit 391f176

Browse files
committed
Bar Chart: Border radius demos added.
1 parent e2bfc2f commit 391f176

3 files changed

Lines changed: 145 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@
9191
<Demo Type="typeof(BarChart_Demo_05_Locale)" Tabs="true" />
9292
</Section>
9393

94+
<Section Class="p-0" Size="HeadingSize.H4" Name="Border radius" PageUrl="@pageUrl" Link="border-radius">
95+
<Block>
96+
TODO: Update the description once the demo is implemented.
97+
</Block>
98+
<Demo Type="typeof(BarChart_Demo_06_Border_Radius)" Tabs="true" />
99+
</Section>
100+
94101
@code {
95102
private const string pageUrl = DemoRouteConstants.Demos_BarChart;
96103
private const string pageUrl2 = DemoRouteConstants.Demos_Prefix;
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<BarChart @ref="barChart" Width="600" />
2+
3+
<div class="mt-5">
4+
<Button Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="RandomizeAsync"> Randomize </Button>
5+
</div>
6+
7+
@code {
8+
private BarChart barChart = default!;
9+
private BarChartOptions barChartOptions = default!;
10+
private ChartData chartData = default!;
11+
12+
private int datasetsCount = 0;
13+
private int labelsCount = 0;
14+
private string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
15+
private Random random = new();
16+
17+
protected override void OnInitialized()
18+
{
19+
chartData = new ChartData { Labels = GetDefaultDataLabels(6), Datasets = GetDefaultDataSets(3) };
20+
barChartOptions = new BarChartOptions { Responsive = true, Interaction = new Interaction { Mode = InteractionMode.Index } };
21+
}
22+
23+
protected override async Task OnAfterRenderAsync(bool firstRender)
24+
{
25+
if (firstRender)
26+
{
27+
await barChart.InitializeAsync(chartData, barChartOptions);
28+
}
29+
await base.OnAfterRenderAsync(firstRender);
30+
}
31+
32+
private async Task RandomizeAsync()
33+
{
34+
if (chartData is null || chartData.Datasets is null || !chartData.Datasets.Any()) return;
35+
36+
var newDatasets = new List<IChartDataset>();
37+
38+
foreach (var dataset in chartData.Datasets)
39+
{
40+
if (dataset is BarChartDataset barChartDataset
41+
&& barChartDataset is not null
42+
&& barChartDataset.Data is not null)
43+
{
44+
var count = barChartDataset.Data.Count;
45+
46+
var newData = new List<double?>();
47+
for (var i = 0; i < count; i++)
48+
{
49+
newData.Add(NewRandom());
50+
}
51+
52+
barChartDataset.Data = newData;
53+
newDatasets.Add(barChartDataset);
54+
}
55+
}
56+
57+
chartData.Datasets = newDatasets;
58+
59+
await barChart.UpdateAsync(chartData, barChartOptions);
60+
}
61+
62+
#region Data Preparation
63+
64+
private List<IChartDataset> GetDefaultDataSets(int numberOfDatasets)
65+
{
66+
var datasets = new List<IChartDataset>();
67+
68+
for (var index = 0; index < numberOfDatasets; index++)
69+
{
70+
datasets.Add(GetRandomBarChartDataset());
71+
}
72+
73+
return datasets;
74+
}
75+
76+
private BarChartDataset GetRandomBarChartDataset()
77+
{
78+
var c = ColorUtility.CategoricalTwelveColors[datasetsCount].ToColor();
79+
80+
datasetsCount += 1;
81+
82+
return new BarChartDataset()
83+
{
84+
Label = $"Product {datasetsCount}",
85+
Data = GetRandomData(),
86+
BorderColor = new List<string> { c.ToRgbString() },
87+
BackgroundColor = new List<string> { c.ToRgbaString(0.5) },
88+
BorderWidth = new List<double> { 2 },
89+
BorderRadius = new List<double> { 10 },
90+
BorderSkipped = false
91+
};
92+
}
93+
94+
private List<double?> GetRandomData()
95+
{
96+
var data = new List<double?>();
97+
for (var index = 0; index < labelsCount; index++)
98+
{
99+
data.Add(NewRandom());
100+
}
101+
102+
return data;
103+
}
104+
105+
private List<string> GetDefaultDataLabels(int numberOfLabels)
106+
{
107+
var labels = new List<string>();
108+
for (var index = 0; index < numberOfLabels; index++)
109+
{
110+
labels.Add(GetNextDataLabel());
111+
}
112+
113+
return labels;
114+
}
115+
116+
private string GetNextDataLabel()
117+
{
118+
labelsCount += 1;
119+
return months[labelsCount - 1];
120+
}
121+
122+
private double NewRandom() => random.Next(-100, 100);
123+
124+
#endregion Data Preparation
125+
}

BlazorExpress.ChartJS/Models/ChartDataset/BarChart/BarChartDataset.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,19 @@ public class BarChartDataset : ChartDataset<double?>
8383
[ParameterTypeName("List<double>?")]
8484
public List<double>? BorderRadius { get; set; }
8585

86-
//BorderSkipped
87-
//https://www.chartjs.org/docs/latest/charts/bar.html#styling
88-
//https://www.chartjs.org/docs/latest/api/interfaces/BarControllerDatasetOptions.html#borderskipped
86+
/// <summary>
87+
/// This setting is used to avoid drawing the bar stroke at the base of the fill, or disable the border radius.
88+
/// In general, this does not need to be changed except when creating chart types that derive from a bar chart.
89+
/// Supported values are 'start', 'end', 'middle', 'bottom', 'left', 'top', 'right', 'false', and 'true'.
90+
/// <para>
91+
/// Default value is 'start'.
92+
/// </para>
93+
/// <see href="https://www.chartjs.org/docs/latest/charts/bar.html#styling" />
94+
/// </summary>
95+
[AddedVersion("1.2.0")]
96+
[DefaultValue("start")]
97+
[Description("This setting is used to avoid drawing the bar stroke at the base of the fill, or disable the border radius. In general, this does not need to be changed except when creating chart types that derive from a bar chart. <br />Supported values are <b>start</b>, <b>end</b>, <b>middle</b>, <b>bottom</b>, <b>left</b>, <b>top</b>, <b>right</b>, <b>false</b>, and <b>true</b>.")]
98+
public object BorderSkipped { get; set; } = "start";
8999

90100
/// <summary>
91101
/// The bar border width (in pixels).

0 commit comments

Comments
 (0)