Skip to content

Commit 612e96b

Browse files
KB Botradkostanev
authored andcommitted
Added new kb article chart-legend-colors-not-matching-stacked-bar-chart
1 parent fc7a81a commit 612e96b

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: Legend Colors Do Not Match Data Points in Stacked Bar Chart
3+
description: Explains why the legend colors might not match the data points in a stacked bar chart and provides a solution.
4+
type: troubleshooting
5+
page_title: Ensure Legend Colors Match Data Points in Stacked Bar Charts
6+
slug: chart-kb-legend-colors-not-matching-stacked-bar-chart
7+
tags: charts, bar chart, stacked series, legend, color
8+
res_type: kb
9+
ticketid: 1684367
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Charts for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
When creating a Chart with Stacked Series, the colors of the data points render correctly on the Chart itself, but the legend colors do not match the colors of the data points. This discrepancy occurs when using the `ColorField` parameter.
26+
27+
## Cause
28+
29+
This behavior is by design. Using the `ColorField` parameter to assign a unique color to each data point within a single series is supported in **non-stacked** Charts, but not in stacked Charts.
30+
31+
Stacked Charts are designed to visualize the cumulative value of multiple series stacked atop one another. Applying individual colors to each data point in this context would compromise the visual clarity of the stack relationships and make it difficult for the legend to accurately reflect the data.
32+
33+
## Solution
34+
35+
To ensure that the legend colors match the data points in a stacked Chart, use the `Color` parameter of the `ChartSeries`. This parameter sets a uniform color for all data points (bars) within a single series and determines the color shown in the legend for that series.
36+
37+
Here is an example configuration that applies a specific color to each series in a stacked bar chart:
38+
39+
`````Razor
40+
<TelerikChart Width="100%" Height="100%">
41+
<ChartSeriesItems>
42+
@foreach (var series in _graphDataPoints)
43+
{
44+
<ChartSeries Field="@nameof(GraphDataPoint.Value)"
45+
Type="ChartSeriesType.Bar"
46+
Name="@series.Label"
47+
Color="@series.Color"
48+
Data="@([series])">
49+
<ChartSeriesStack Enabled="true" />
50+
</ChartSeries>
51+
}
52+
</ChartSeriesItems>
53+
<ChartLegend Position="ChartLegendPosition.Right" />
54+
</TelerikChart>
55+
56+
@code {
57+
public class GraphDataPoint
58+
{
59+
public required string Color { get; set; }
60+
public required int Value { get; set; }
61+
public required string Label { get; set; }
62+
}
63+
64+
private List<GraphDataPoint> _graphDataPoints { get; set; } = [
65+
new GraphDataPoint
66+
{
67+
Label = "Early Settlement Candidate",
68+
Value = 1024,
69+
Color = "#D46663"
70+
},
71+
new GraphDataPoint
72+
{
73+
Label = "Needs Discovery to Strategize",
74+
Value = 980,
75+
Color = "#F89995"
76+
},
77+
new GraphDataPoint
78+
{
79+
Label = "Potential Dispositive Candidate",
80+
Value = 1006,
81+
Color = "#FFC7C7"
82+
},
83+
new GraphDataPoint
84+
{
85+
Label = "Potential Trial Candidate",
86+
Value = 1003,
87+
Color = "#BCDCCF",
88+
},
89+
new GraphDataPoint
90+
{
91+
Label = "Settlement Candidate",
92+
Value = 987,
93+
Color = "#79C8AB"
94+
}
95+
];
96+
}
97+
`````

0 commit comments

Comments
 (0)