Adding Different Colors to the Bars of the Chart
Environment
Product | Progress® Telerik® UI for .NET MAUI Chart |
Product Version | 2.0.0 |
Description
How can I add a palette to the Telerik UI for .NET MAUI Bar Chart to show each bar in a different color?
Solution
To implement a Chart palette, add a Palette
entry in XAML. In general, you have to create a custom Chart palette in the ViewModel
.
The suggested approach uses the code from the Telerik UI for .NET MAUI documentation for the Chart Bar Series and adds the necessary components to achieve the desired result.
To achieve the desired scenario:
1. Add the Color
property to the business model:
2. In the Collection
, set values for the barColor
property of type Color
:
3. After creating the business model and the collection of data, create a class in the ViewModel
that changes the values of the Chart Palette
.
4. Create a Bindable
property to store the Colors
:
5. Define the properties which get and set the values in the collection:
6. To get the desired color, add a method which changes the old palette to a new one:
7. After having the OnColorsSourceChanged
method, you need another one which will create the new palette entries based on the data from SeriesData
collection:
8. Add the following code.
9. Attach the ChartUtils
class to the Chart in XAML.
Set the
PaletteMode
toDataPoint
properties. Otherwise, the palettes will fail to apply to the Chart.
The following image shows the end result of the suggested implementation.