title | description | type | page_title | slug | position | tags | ticketid | res_type |
---|---|---|---|---|---|---|---|---|
How To Data Bind PieSeries Color |
How to data bind a model property to a PieSeries slice color. |
how-to |
How To Data Bind PieSeries Color |
kb-chartview-databind-pieseries-color |
0 |
ChartView, PieSeries, Data binding, Styling, MVVM |
1397704 |
kb |
Product Version | 2019.1.220 |
Product | RadChartView for WPF |
How to data bind a model property to a PieSeries
or DoughnutSeries
slice color.
You can use the Series DefaultSliceStyle and bind the data item's Brush
property directly to the Fill property of the Path
(see Example 1). If the data item does not have a Brush
property, you can use an IValueConverter
that returns a Brush
object (see Example 2).
important When using this approach, make sure that the chart's Palette is not explicitly set. The
ChartPalette
value takes precedence over the DefaultSliceStyle and will ignore the bound value.
-
Define the Data Model
{{region kb-chartview-databind-pieseries-color-0}} public class MyModel { public string Title { get; set; }
public double Amount { get; set; } public Brush SliceColor { get; set; } }
{{endregion}}
The SliceColor property is of type
Brush
. This is what will be used to bind to the Fill property of thePath
element in the style.* -
Create the data items in the View Model
{{region kb-chartview-databind-pieseries-color-1}} public class ViewModel : ViewModelBase { public ViewModel() { }
public ObservableCollection<MyModel> PieData { get; set; } = new ObservableCollection<MyModel> { new MyModel { Title = "One", Amount = 70, SliceColor = new SolidColorBrush(Colors.LightBlue) }, new MyModel { Title = "Two", Amount = 20, SliceColor = new SolidColorBrush(Colors.LightSalmon) }, new MyModel { Title = "Three", Amount = 10, SliceColor = new SolidColorBrush(Colors.LightCoral) }, }; }
{{endregion}}
Note that each item has an explicit SliceColor value.*
-
Define the DefaultSliceStyle and bind the SliceColor property of the slice's DataContext to the Fill property of the
Style
.{{region kb-chartview-databind-pieseries-color-2}} <telerik:RadPieChart x:Name="chartView"> <telerik:PieSeries ItemsSource="{Binding PieData}" ValueBinding="Amount" ShowLabels="False"> telerik:PieSeries.DefaultSliceStyle <Style TargetType="Path"> </Style> </telerik:PieSeries.DefaultSliceStyle> </telerik:PieSeries> </telerik:RadPieChart> {{endregion}}
The DataContext of the
Path
is of typeTelerik.Charting.PieDataPoint
. The instance ofMyModel
is accessed through thePieDataPoint
DataItem property.*
If the data model does not have a Brush
property that can be directly bound to Path
Fill, you can use an IValueConverter
to return a Brush
value that can use used to set the Fill value.
-
Use the code from Example 1 to get started, then take the following additional steps.
-
Add a class that implements
IValueConverter
and returns an instance ofBrush
.{{region kb-chartview-databind-pieseries-color-3}} public class PieDataPointToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is MyModel data) { if (data.Amount <= 10) { return new SolidColorBrush(Colors.Green); } else if (data.Amount <= 20) { return new SolidColorBrush(Colors.Goldenrod); } else { return new SolidColorBrush(Colors.DarkRed); } }
return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
{{endregion}}
-
Add an instance of the Converter to the Resources of the
RadPieChart
{{region kb-chartview-databind-pieseries-color-4}} <telerik:RadPieChart x:Name="chartView"> telerik:RadPieChart.Resources <local:PieDataPointToBrushConverter x:Key="MyBrushConverter"/> </telerik:RadPieChart.Resources> ... </telerik:RadPieChart> {{endregion}}
-
Use the converter on the DefaultSliceStyle binding
{{region kb-chartview-databind-pieseries-color-5}} <telerik:RadPieChart x:Name="chartView"> telerik:RadPieChart.Resources <local:PieDataPointToBrushConverter x:Key="MyBrushConverter"/> </telerik:RadPieChart.Resources> <telerik:PieSeries ItemsSource="{Binding PieData}" ValueBinding="Amount" ShowLabels="False"> telerik:PieSeries.DefaultSliceStyle <Style TargetType="Path"> </Style> </telerik:PieSeries.DefaultSliceStyle> </telerik:PieSeries> </telerik:RadPieChart> {{endregion}}
- [Chart Series Overview]({%slug radchartview-series-chartseries%})
- [PieSeries]({%slug radchartview-series-pieseries%})
- [DoughnutSeries]({%slug radchartview-series-doughnutseries%})