Value labels
Since 0.1.0-alpha.22Labels on bars
To write each value beside its bar, set Labels on the series to Auto. A label goes past the end of the bar, or inside it when there's no room above.
Static
Preview
- 2022
- 2023
- 2024
| State | 2022 | 2023 | 2024 |
|---|---|---|---|
| Illinois | 1,040 | 1,090 | 1,137 |
| Indiana | 473 | 505 | 527 |
| Michigan | 627 | 675 | 705 |
| Ohio | 833 | 890 | 930 |
| Wisconsin | 401 | 415 | 430 |
Code
@* Gross state product for the Great Lakes states over three years, each bar labeled. *@
<Chart Items="_states" Argument="s => s.State" Title="Gross state product, Great Lakes (billions USD)">
<ChartBarSeries Value="s => s.Y2022" Name="2022" Labels="ChartLabels.Auto" LabelFormat="N0" />
<ChartBarSeries Value="s => s.Y2023" Name="2023" Labels="ChartLabels.Auto" LabelFormat="N0" />
<ChartBarSeries Value="s => s.Y2024" Name="2024" Labels="ChartLabels.Auto" LabelFormat="N0" />
</Chart>
{
private record StateProduct(string State, decimal Y2022, decimal Y2023, decimal Y2024);
private readonly StateProduct[] _states =
[
new("Illinois", 1_040, 1_090, 1_137),
new("Indiana", 473, 505, 527),
new("Michigan", 627, 675, 705),
new("Ohio", 833, 890, 930),
new("Wisconsin", 401, 415, 430),
];
}LabelFormat formats the labels apart from the axis. A label that would overlap another is left out at the widths where it would; narrow the window to see them go. To show every label anyway, set LabelOverlap to Show.
With every series labeled, the chart has no tooltips, which would repeat the labels. To keep them, set Tooltip to true.
Labels in a stack
In a stack, Auto puts each label in the middle of its segment, and leaves it out when the segment is too small to hold it. Inside puts labels inside the bar at its end, and Outside past it.
Static
Preview
- Chat
- Phone
| Week | Chat | Phone | |
|---|---|---|---|
| Oct 5 | 212 | 148 | 61 |
| Oct 12 | 198 | 171 | 58 |
| Oct 19 | 236 | 189 | 66 |
| Oct 26 | 221 | 204 | 49 |
| Nov 2 | 247 | 226 | 57 |
Code
@* Support tickets by channel each week, stacked, each segment labeled in its middle. *@
<Chart Items="_weeks" Argument="w => w.Week" Title="Support tickets by channel" Stacking="ChartStacking.Normal">
<ChartBarSeries Value="w => w.Email" Labels="ChartLabels.Auto" />
<ChartBarSeries Value="w => w.Chat" Labels="ChartLabels.Auto" />
<ChartBarSeries Value="w => w.Phone" Labels="ChartLabels.Auto" />
</Chart>
{
private record WeekTickets(string Week, int Email, int Chat, int Phone);
private readonly WeekTickets[] _weeks =
[
new("Oct 5", 212, 148, 61), new("Oct 12", 198, 171, 58), new("Oct 19", 236, 189, 66),
new("Oct 26", 221, 204, 49), new("Nov 2", 247, 226, 57),
];
}