Binding the Color of Series Items
This topic demonstrates how to create charts, where each individual item, has its color bound to a property of the underlying data object.
There are small differences in the approaches used with the different series types. For the sake of the example the following series will be used. They cover the possible approaches.
The color binding is done via the DefaultVisualStyle
or PointTemplate
properties of the series. In case you are using one of the lightweight render options (Direct2D or Bitmap), keep in mind that bindings are not support in the DefaultVisualStyle
. Instead, use only fixed values. Also setting the PointTemplate
property will disable the lightweight render options and the chart will fallback to the default XAML rendering.
Example Data Model Definition
This section contains the data model used for the series examples in this article and also how to set it up.
Example 1: Defining the data model
Let's create some data so we can test the result.
Example 2: Populating the data collection
Bar Series
Here is a sample chart with BarSeries defined in Xaml.
Example 3: BarSeries definition
You can see the data model used for this example in the Example Data Model Definition section.
There are couple approaches which could be used to bind the color from the data object to the fill of the data point visual (the bar).
-
Use the DefaultVisualStyle property of the series. You can use this to target the default visual of the series and bind its properties to the data model object.
Using the data model from this article's example you can bind the Color property from the ChartData class.
Example 4: Setting BarSeries DefaultVisualStyle
-
Use the PointTemplate property of the series. In this case you can define a DataTemplate containing a custom visual element for the data points and bind its color property (Background or Fill for example) to the property from the data model.
Using the data model from this article's example you can bind the Color property from the ChartData class.
Example 5: Setting BarSeries PointTemplate
The DataItem in the binding's path points to the custom ChartData model.
The recommended approach for binding the color is to use the DefaultVisualStyle. This is because the PointTemplate creates an additional ContentPresenter for each data point visual which means a bit more rendering time for the framework.
You can use those approaches with most series types of the chart.
Figure 1: Different colored bars
Scatter Point Series
Here is a sample chart with ScatterPointSeries defined in XAML.
Example 3: ScatterPointSeries definition
You can see the data model used for this example in the Example Data Model Definition section.
The ScatterPointSeries can be customized using the same manner as with the BarSeries. There are two small differences which could be applied.
As all scatter series, ScatterPointSeries requires the RadCartesianChart to define two LinearAxis as a vertical and horizontal axis.
-
When using DefaultVisualStyle you will need to target a different UI element - a Path in this case. And also you will need to define the size (Width and Height) of the Path.
Example 6: Setting Point Series DefaultVisualStyle
The data context passed in the DefaultVisualStyle of the ScatterPointSeries (and several other series) is not the DataPoint object, but the context of the series. In this case the DataPoint is stored in the Tag property of the Path.
-
When using PointTemplate the most common shape you will use is an ellipse so you can define an Ellipse element in the template.
Example 7: Setting Point Series PointTemplate
You can see the data model used for this example in the Example Data Model Definition section.
Figure 2: Different colored points
Pie Series
Similar to the BarSeries the PieSeries uses a default visual style to customize the appearance of the pie slices. The Style should target a Path element and it is applied to the DefaultSliceStyle property of the series.
Example 8: Setting Pie Series default visual style
You can see the data model used for this example in the Example Data Model Definition section.