In SciChart, the Donut Chart type is represented by the SCIPieChartSurface class.
The SCIDonutRenderableSeries is very similar to the Pie Chart, except it has a round hole in its center.
A SCIPieSegment represents a percentage that corresponds to a particular value.
This value appears drawn on every segment and can be set in code.
// Assume a surface has been created and configured somewhere
SCIPieChartSurface pieChartSurface;
pieChartSurface.holeRadius = 100;
pieChartSurface.holeRadiusSizingMode = SCISizingMode_Absolute;
// Create Donut Series and fill it with Segments
SCIDonutRenderableSeries *donutSeries = [SCIDonutRenderableSeries new];
[donutSeries.segmentsCollection add:[self segmentWithValue:40 title:@“Green” centerColor:0xff84BC3D edgeColor:0xff5B8829]];
[donutSeries.segmentsCollection add:[self segmentWithValue:10 title:@“Red” centerColor:0xffe04a2f edgeColor:0xffB7161B]];
[donutSeries.segmentsCollection add:[self segmentWithValue:20 title:@“Blue” centerColor:0xff4AB6C1 edgeColor:0xff2182AD]];
[donutSeries.segmentsCollection add:[self segmentWithValue:15 title:@“Yellow” centerColor:0xffFFFF00 edgeColor:0xfffed325]];
[pieChartSurface.renderableSeries add:donutSeries];
- (SCIPieSegment *)segmentWithValue:(double)segmentValue title:(NSString *)title centerColor:(unsigned int)centerColor edgeColor:(unsigned int)edgeColor {
SCIPieSegment *segment = [SCIPieSegment new];
segment.value = segmentValue;
segment.title = title;
segment.fillStyle = [[SCIRadialGradientBrushStyle alloc] initWithCenterColorCode:centerColor edgeColorCode:edgeColor];
return segment;
}
// Assume a surface has been created and configured somewhere
let pieChartSurface: SCIPieChartSurface
pieChartSurface.holeRadius = 100
// Create Donut Series and fill it with Segments
let donutSeries = SCIDonutRenderableSeries()
donutSeries.segmentsCollection.add(segmentWithValue(segmentValue: 40, title: “Green”, centerColor: 0xff84BC3D, edgeColor: 0xff5B8829))
donutSeries.segmentsCollection.add(segmentWithValue(segmentValue: 10, title: “Red”, centerColor: 0xffe04a2f, edgeColor: 0xffB7161B))
donutSeries.segmentsCollection.add(segmentWithValue(segmentValue: 20, title: “Blue”, centerColor: 0xff4AB6C1, edgeColor: 0xff2182AD))
donutSeries.segmentsCollection.add(segmentWithValue(segmentValue: 15, title: “Yellow”, centerColor: 0xffFFFF00, edgeColor: 0xfffed325))
pieChartSurface.renderableSeries.add(donutSeries)
func segmentWithValue(segmentValue: Double, title: String, centerColor: UInt32, edgeColor: UInt32) -> SCIPieSegment {
let segment = SCIPieSegment()
segment.value = segmentValue
segment.title = title
segment.fillStyle = SCIRadialGradientBrushStyle(centerColorCode: centerColor, edgeColorCode: edgeColor)
return segment
}
// Assume a surface has been created and configured somewhere
SCIPieChartSurface pieChartSurface;
pieChartSurface.HoleRadius = 100;
pieChartSurface.HoleRadiusSizingMode = SCISizingMode.Absolute;
// Create Donut Series and fill it with Segments
var donutSeries = new SCIDonutRenderableSeries
{
SegmentsCollection = new SCIPieSegmentCollection
{
new SCIPieSegment { Value = 40, Title = “Green”, FillStyle = new SCIRadialGradientBrushStyle(0xff84BC3D, 0xff5B8829) },
new SCIPieSegment { Value = 10, Title = “Red”, FillStyle = new SCIRadialGradientBrushStyle(0xffe04a2f, 0xffB7161B) },
new SCIPieSegment { Value = 20, Title = “Blue”, FillStyle = new SCIRadialGradientBrushStyle(0xff4AB6C1, 0xff2182AD) },
new SCIPieSegment { Value = 15, Title = “Yellow”, FillStyle = new SCIRadialGradientBrushStyle(0xffFFFF00, 0xfffed325) },
},
};
pieChartSurface.RenderableSeries.Add(donutSeries);
Changing the size of the Donut Chart
If you want to change the size of the Donut Chart you can use a combination of the following properties:
The above allows you to specify how much space the Donut Chart should use for its rendering.
If you use Absolute mode then ISCIPieRenderableSeries.height accepts size in pixels, and if you use Relative mode, it expects value from 0 to 1, which tells how much of the available space it should use for rendering (e.g. 0.5 will tell Donut series to use 50% of available space).
Legend and Selection modifiers are synced if both are added. A SCIPieSegment can be selected by clicking on either of them (via the Selection Modifier) or the corresponding item in the Legend.
This action provides visual feedback on the chart and the Legend.
By default, the Pie Segment Label displays a relative percentage calculated on values of all segments in SCIPieDonutRenderableSeriesBase.segmentsCollection. This behavior can be controlled and to do so you’ll need to subclass SCIPieSegmentLabelFormatterBase and provide your custom data in -[ISCIPieSegmentLabelFormatter formatLabelForPieSegment:] method. As an example, let’s create a label that displays a segment absolute value. Assume, we create a donutSeries and add four segments with values 40, 10, 20, and 15. Here is the code sample, how to do this:
// CustomPieSegmentLabelFormatter.h
@interface CustomPieSegmentLabelFormatter : SCIPieSegmentLabelFormatterBase
@end
// CustomPieSegmentLabelFormatter.m
@implementation CustomPieSegmentLabelFormatter
- (NSString *)formatLabelForPieSegment:(id)pieSegment {
return [NSString stringWithFormat:@“%f”, pieSegment.value];
}
@end
// Assume a donutSeries has been created somewhere
SCIDonutRenderableSeries *donutSeries = [SCIDonutRenderableSeries new];
donutSeries.pieSegmentLabelFormatter = [CustomPieSegmentLabelFormatter new];
class CustomPieSegmentLabelFormatter: SCIPieSegmentLabelFormatterBase {
override func formatLabel(for pieSegment: ISCIPieSegment!) -> String! {
return “\(pieSegment.value)”
}
}
// Assume a donutSeries has been created somewhere
let donutSeries = SCIDonutRenderableSeries()
donutSeries.pieSegmentLabelFormatter = CustomPieSegmentLabelFormatter()
class CustomPieSegmentLabelFormatter : SCIPieSegmentLabelFormatterBase
{
public override string FormatLabel(IISCIPieSegment pieSegment)
{
return pieSegment.Value.ToString();
}
}
// Assume a donutSeries has been created somewhere
var donutSeries = new SCIDonutRenderableSeries;
donutSeries.PieSegmentLabelFormatter = new CustomPieSegmentLabelFormatter();