|
| 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 display the combined values of multiple series stacked together. Assigning unique colors to individual data points in this scenario can reduce the visual clarity of the stack relationships and prevent the legend from accurately representing 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 `ChartSeries` 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 | + private List<GraphDataPoint> GraphDataPoints { get; set; } = [ |
| 58 | + new GraphDataPoint |
| 59 | + { |
| 60 | + Label = "Early Settlement Candidate", |
| 61 | + Value = 1024, |
| 62 | + Color = "#D46663" |
| 63 | + }, |
| 64 | + new GraphDataPoint |
| 65 | + { |
| 66 | + Label = "Needs Discovery to Strategize", |
| 67 | + Value = 980, |
| 68 | + Color = "#F89995" |
| 69 | + }, |
| 70 | + new GraphDataPoint |
| 71 | + { |
| 72 | + Label = "Potential Dispositive Candidate", |
| 73 | + Value = 1006, |
| 74 | + Color = "#FFC7C7" |
| 75 | + }, |
| 76 | + new GraphDataPoint |
| 77 | + { |
| 78 | + Label = "Potential Trial Candidate", |
| 79 | + Value = 1003, |
| 80 | + Color = "#BCDCCF", |
| 81 | + }, |
| 82 | + new GraphDataPoint |
| 83 | + { |
| 84 | + Label = "Settlement Candidate", |
| 85 | + Value = 987, |
| 86 | + Color = "#79C8AB" |
| 87 | + } |
| 88 | + ]; |
| 89 | +
|
| 90 | + public class GraphDataPoint |
| 91 | + { |
| 92 | + public required string Color { get; set; } |
| 93 | + public required int Value { get; set; } |
| 94 | + public required string Label { get; set; } |
| 95 | + } |
| 96 | +} |
0 commit comments