Skip to content

Commit 1feb905

Browse files
committed
Refactor data label enums and update chart properties
Updated the `Anchor` and `Alignment` enums to `DataLabelAnchor` and `DataLabelAlignment`. Modified related properties and methods across various files to utilize the new enums. Added a `BackgroundColor` property to the `Datalabels` in chart datasets and adjusted default values for alignment and anchor properties. Increased the width of the `LineChart` component from 700 to 800 pixels. NOTE: This commit message is auto-generated using GitHub Copilot.
1 parent 901b225 commit 1feb905

9 files changed

Lines changed: 137 additions & 73 deletions

File tree

BlazorExpress.ChartJS.Demo.RCL/Pages/Demos/DoughnutChart/DoughnutChart_Demo_02_Datalabels.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@
9595
var dataset = GetRandomDoughnutChartDataset();
9696

9797
if (index == 0)
98-
dataset.Datalabels.Anchor = Anchor.End;
98+
dataset.Datalabels.Anchor = DataLabelAnchor.End;
9999
else if (index == numberOfDatasets - 1)
100-
dataset.Datalabels.Anchor = Anchor.Start;
100+
dataset.Datalabels.Anchor = DataLabelAnchor.Start;
101101
else
102-
dataset.Datalabels.Anchor = Anchor.Center;
102+
dataset.Datalabels.Anchor = DataLabelAnchor.Center;
103103

104104
datasets.Add(dataset);
105105
}

BlazorExpress.ChartJS.Demo.RCL/Pages/Demos/LineChart/LineChart_Demo_02_Datalabels.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<LineChart @ref="lineChart" Width="700" />
1+
<LineChart @ref="lineChart" Width="800" />
22

33
@code {
44
private LineChart lineChart = default!;
@@ -25,7 +25,7 @@
2525
// PointHoverRadius = 4,
2626
2727
// datalabels
28-
Datalabels = new() { Alignment = Alignment.End, Anchor = Anchor.End }
28+
Datalabels = new() { Alignment = DataLabelAlignment.End, Anchor = DataLabelAnchor.End, BackgroundColor = colors[0] }
2929
};
3030
datasets.Add(dataset1);
3131

@@ -42,7 +42,7 @@
4242
// PointHoverRadius = 4,
4343
4444
// datalabels
45-
Datalabels = new() { Alignment = Alignment.End, Anchor = Anchor.End }
45+
Datalabels = new() { Alignment = DataLabelAlignment.End, Anchor = DataLabelAnchor.End, BackgroundColor = colors[1] }
4646
};
4747
datasets.Add(dataset2);
4848

@@ -59,7 +59,7 @@
5959
// PointHoverRadius = 4,
6060
6161
// datalabels
62-
Datalabels = new() { Alignment = Alignment.Start, Anchor = Anchor.Start }
62+
Datalabels = new() { Alignment = DataLabelAlignment.Start, Anchor = DataLabelAnchor.Start, BackgroundColor = colors[2] }
6363
};
6464
datasets.Add(dataset3);
6565

BlazorExpress.ChartJS.Demo.RCL/Pages/Demos/PieChart/PieChart_Demo_02_Datalabels.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@
9898
var dataset = GetRandomPieChartDataset();
9999

100100
if (index == 0)
101-
dataset.Datalabels.Anchor = Anchor.End;
101+
dataset.Datalabels.Anchor = DataLabelAnchor.End;
102102
else if (index == numberOfDatasets - 1)
103-
dataset.Datalabels.Anchor = Anchor.End;
103+
dataset.Datalabels.Anchor = DataLabelAnchor.End;
104104
else
105-
dataset.Datalabels.Anchor = Anchor.Center;
105+
dataset.Datalabels.Anchor = DataLabelAnchor.Center;
106106

107107
datasets.Add(dataset);
108108
}

BlazorExpress.ChartJS/Enums/Alignment.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

BlazorExpress.ChartJS/Enums/Anchor.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace BlazorExpress.ChartJS;
2+
3+
/// <summary>
4+
/// The align option defines the position of the label relative to the anchor point position and orientation.
5+
/// <para>
6+
/// <see href="https://chartjs-plugin-datalabels.netlify.app/guide/positioning.html#alignment-and-offset" />
7+
/// </para>
8+
/// </summary>
9+
public enum DataLabelAlignment
10+
{
11+
/// <summary>
12+
/// 'center' (default): the label is centered on the anchor point
13+
/// </summary>
14+
Center,
15+
16+
/// <summary>
17+
/// 'start': the label is positioned before the anchor point, following the same direction
18+
/// </summary>
19+
Start,
20+
21+
/// <summary>
22+
/// 'end': the label is positioned after the anchor point, following the same direction
23+
/// </summary>
24+
End,
25+
26+
/// <summary>
27+
/// 'left': the label is positioned to the left of the anchor point (180°)
28+
/// </summary>
29+
Left,
30+
31+
/// <summary>
32+
/// 'top': the label is positioned to the top of the anchor point (270°)
33+
/// </summary>
34+
Top,
35+
36+
/// <summary>
37+
/// 'right': the label is positioned to the right of the anchor point (0°)
38+
/// </summary>
39+
Right,
40+
41+
/// <summary>
42+
/// 'bottom': the label is positioned to the bottom of the anchor point (90°)
43+
/// </summary>
44+
Bottom
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace BlazorExpress.ChartJS;
2+
3+
/// <summary>
4+
/// An anchor point is defined by an orientation vector and a position on the data element.
5+
/// The orientation depends on the scale type (vertical, horizontal or radial).
6+
/// The position is calculated based on the anchor option and the orientation vector.
7+
/// <para>
8+
/// <see href="https://chartjs-plugin-datalabels.netlify.app/guide/positioning.html#anchoring" />
9+
/// </para>
10+
/// </summary>
11+
public enum DataLabelAnchor
12+
{
13+
/// <summary>
14+
/// 'center' (default): element center
15+
/// </summary>
16+
Center,
17+
18+
/// <summary>
19+
/// 'start': lowest element boundary
20+
/// </summary>
21+
Start,
22+
23+
/// <summary>
24+
/// 'end': highest element boundary
25+
/// </summary>
26+
End
27+
}

BlazorExpress.ChartJS/Extensions/EnumExtensions.cs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,21 @@ public static class EnumExtensions
44
{
55
#region Methods
66

7-
public static string? ToAlignmentString(this Alignment alignment) =>
7+
public static string? ToChartDatasetDataLabelAlignmentString(this DataLabelAlignment alignment) =>
88
alignment switch
99
{
10-
Alignment.Start => "start",
11-
Alignment.Center or Alignment.None => "center", // default
12-
Alignment.End => "end",
10+
DataLabelAlignment.Start => "start",
11+
DataLabelAlignment.Center => "center", // default
12+
DataLabelAlignment.End => "end",
1313
_ => null
1414
};
1515

16-
public static string? ToAnchorString(this Anchor anchor) =>
16+
public static string? ToChartDatasetDataLabelAnchorString(this DataLabelAnchor anchor) =>
1717
anchor switch
1818
{
19-
Anchor.Start => "start",
20-
Anchor.Center or Anchor.None => "center", // default
21-
Anchor.End => "end",
22-
_ => null
23-
};
24-
25-
public static string? ToChartDatasetDataLabelAlignmentString(this Alignment alignment) =>
26-
alignment switch
27-
{
28-
Alignment.Start => "start",
29-
Alignment.Center or Alignment.None => "center", // default
30-
Alignment.End => "end",
31-
_ => null
32-
};
33-
34-
public static string? ToChartDatasetDataLabelAnchorString(this Anchor anchor) =>
35-
anchor switch
36-
{
37-
Anchor.Start => "start",
38-
Anchor.Center or Anchor.None => "center", // default
39-
Anchor.End => "end",
19+
DataLabelAnchor.Start => "start",
20+
DataLabelAnchor.Center => "center", // default
21+
DataLabelAnchor.End => "end",
4022
_ => null
4123
};
4224

BlazorExpress.ChartJS/Models/ChartDataset/ChartDatasetDataLabels.cs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class ChartDatasetDataLabels
88
{
99
#region Fields and Constants
1010

11-
private Alignment alignment;
11+
private DataLabelAlignment alignment;
1212

13-
private Anchor anchor;
13+
private DataLabelAnchor anchor;
1414

1515
#endregion
1616

@@ -19,75 +19,103 @@ public class ChartDatasetDataLabels
1919
/// <summary>
2020
/// Gets or sets the data labels alignment.
2121
/// <para>
22-
/// Default value is <see cref="Alignment.Center"/>.
22+
/// Default value is <see cref="DataLabelAlignment.Center"/>.
2323
/// </para>
2424
/// </summary>
2525
[AddedVersion("1.0.0")]
26-
[DefaultValue(Alignment.Center)]
26+
[DefaultValue(DataLabelAlignment.Center)]
2727
[Description("Gets or sets the data labels alignment.")]
2828
[JsonIgnore]
29-
public Alignment Alignment
29+
public DataLabelAlignment Alignment
3030
{
3131
get => alignment;
3232
set
3333
{
3434
alignment = value;
35-
DataLabelsAlignment = value.ToAlignmentString();
35+
DataLabelsAlignmentAsString = value.ToChartDatasetDataLabelAlignmentString();
3636
}
3737
}
3838

3939
/// <summary>
4040
/// Gets or sets the data labels anchor.
4141
/// <para>
42-
/// Default value is <see cref="Anchor.None"/>.
42+
/// Default value is <see cref="DataLabelAnchor.None"/>.
4343
/// </para>
4444
/// </summary>
4545
[AddedVersion("1.0.0")]
46-
[DefaultValue(Anchor.None)]
46+
[DefaultValue(DataLabelAnchor.Center)]
4747
[Description("Gets or sets the data labels anchor.")]
4848
[JsonIgnore]
49-
public Anchor Anchor
49+
public DataLabelAnchor Anchor
5050
{
5151
get => anchor;
5252
set
5353
{
5454
anchor = value;
55-
DataLabelsAnchor = value.ToAnchorString();
55+
DataLabelsAnchorAsString = value.ToChartDatasetDataLabelAnchorString();
5656
}
5757
}
5858

59-
//public string? BackgroundColor { get; set; }
59+
/// <summary>
60+
/// Gets or sets the data label background color.
61+
/// <para>
62+
/// Default value is <see langword="null"/>.
63+
/// </para>
64+
/// </summary>
65+
[AddedVersion("1.0.0")]
66+
[DefaultValue(null)]
67+
[Description("Gets or sets the data label background color.")]
68+
public string? BackgroundColor { get; set; }
69+
70+
71+
/// <summary>
72+
/// Gets or sets the data label border color.
73+
/// <para>
74+
/// Default value is <see langword="null"/>.
75+
/// </para>
76+
/// </summary>
77+
[AddedVersion("1.0.0")]
78+
[DefaultValue(null)]
79+
[Description("Gets or sets the data label border color.")]
80+
public string? BorderColor { get; set; }
6081

82+
/// <summary>
83+
/// Gets or sets the width of the border.
84+
/// <para>
85+
/// Default value is 2.
86+
/// </para>
87+
/// </summary>
88+
[AddedVersion("1.0.0")]
89+
[DefaultValue(2)]
90+
[Description("Gets or sets the width of the border.")]
6191
public double BorderWidth { get; set; } = 2;
6292

6393
/// <summary>
64-
/// Gets or sets the data labels alignment.
65-
/// Possible values: start, center, and end.
94+
/// Gets the data labels alignment as a string.
6695
/// <para>
6796
/// Default value is <see langword="null"/>.
6897
/// </para>
6998
/// </summary>
7099
[AddedVersion("1.0.0")]
71100
[DefaultValue(null)]
72-
[Description("Gets or sets the data labels alignment. Possible values: start, center, and end.")]
101+
[Description("Gets the data labels alignment as a string.")]
73102
[ParameterTypeName("string?")]
74103
[JsonPropertyName("align")]
75104
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
76-
public string? DataLabelsAlignment { get; private set; }
105+
public string? DataLabelsAlignmentAsString { get; private set; }
77106

78107
/// <summary>
79-
/// Gets or sets the data labels anchor.
80-
/// Possible values: start, center, and end.
108+
/// Gets the data labels anchor as a string.
81109
/// <para>
82110
/// Default value is <see langword="null"/>.
83111
/// </para>
84112
/// </summary>
85113
[AddedVersion("1.0.0")]
86114
[DefaultValue(null)]
87-
[Description("Gets or sets the data labels anchor. Possible values: start, center, and end.")]
115+
[Description("Gets the data labels anchor as a string.")]
88116
[ParameterTypeName("string?")]
89117
[JsonPropertyName("anchor")]
90-
public string? DataLabelsAnchor { get; private set; }
118+
public string? DataLabelsAnchorAsString { get; private set; }
91119

92120
#endregion
93121
}

0 commit comments

Comments
 (0)