Currently RadChart provides 27 2D Chart types for SL/WPF. You can easily customise the existing chart types to create a more complex series like the box plot(also known as box-and-whisker diagram and widely spread in descriptive statistics).
This blog post will demonstrate how to create Box Plot and Scatter Error bars by reusing the default Styles of the CandleStick and Scatter series and adding custom CandleStick / CandleStickSeriesDefinition (ScatterPoint/ ScatterSeriesDefinition) pair of classes.
The BoxPlot have five number summaries: the smallest observation (sample minimum), lower quartile(Q1), median (Q2), upper quartile(Q3), and largest observation (sample maximum).For the purpose the Open, Close, High and Low values of the CandleStick will be used with retemplating the Series Definition to add the additional Median Line visual. You will also need to create custom CandleStick / CandleStickSeriesDefinition pair of classes (to calculate the necessary coordinates for the additional Line).
1. Add the additional Line visual(s) in the default template of the CandleStick Chart type:
- A new part with Name “PART_CustomLine” is added for the Median Line (percentile) that is to be shown:
2. Create new CustomCandleStick class that inherits the CandleStick class and set the CustomLine as set in the template as Parametric Line. A few more methods are added in order to calculate the necessary coordinates for the additional Line visual, so that it can be positioned on the Plot Area:
- void UpdateParametricLinesParameters() – overridden from the CandleStick class
- void UpdateParametricLinesCoordinates(Size constraint) – overridden form the CandleStick class
- DataRange CalculateRange(double value)
3. Create new class that inherits the CandleStickSeriesDefinition class and override the CreateChartItem() method so that the CustomCandleStick becomes a brand new Series Definition.
The full source code for creating Box Plot with the approach can be downloaded from here.
Since we will need two crossing Lines to achieve the Error Bar Chart we can add them in the default Scatter Series Style like this:
The first one is Vertical and the second one is Horizontal Line (with X1 and X2 coordinates hardcoded for better positioning). Since the Scatter Series doesn’t have MinValueY, MaxValueY or StartPointY properties needed for drawing the Lines they should be registered as dependency properties. This is done as follows:
- Create new ScatterErrorBar class that inherits the ScatterPoint class and register the dependency properties there. A few more methods are added in order to accommodate the necessary coordinate calculations for the additional visual elements that will be added to the template:
1. void CreatePoints(Size arrangedBounds) – overridden from the ScatterPoint class
2. double CalculateMinValueY(Size arrangedBounds)
3. double CalculateMaxValueY(Size arrangedBounds)
4. DataRange CalculateRange(double value)
- Create new ScatterErrorSeriesDefinition by inheriting the ScatterSeriesDefinition class and override the CreateChartItem() method that returns the ScatterErrorBar class. This way you create brand new Series Definition -ScatterErrorBar that is used as TargetType in the Style mentioned above. You can create a ScatterErrorBar Chart by setting YValue for the Scatterpoint and MinValue and MaxValue that will be used to position the crossing Lines.
The full source code for ErrorBar can be downloaded from here!
The result is shown below:
Telerik XAML Team