The Pie Chart Type
In SciChart, the Pie Chart type is represented by the SciPieChartSurface class.
The PieRenderableSeries represents data in a form of a circle divided into triangular wedges, called segments. A PieSegment represents a percentage that corresponds to a particular value. This value appears drawn on every segment and can be set in code.
Note
Examples of the Pie Chart can be found in the SciChart Android Examples Suite as well as on GitHub:
The IPieSegment allows you to specify different styles to control rendering of the segments, e.g.:
Note
To learn more about Pens and Brushes and how to utilize them, please refer to the PenStyle, BrushStyle and FontStyle article.
Also, you can control whether to draw labels on segments or not via the drawLabels property.
Create a Pie Chart
To create a Pie Chart, you have to provide a PieRenderableSeriesCollection and assign it to renderableSeries property. The data source is a collection of objects that conforms to the IPieRenderableSeries protocol, which contains a list of PieSegment instances to draw.
@Override
protected void initExample(@NonNull SciPieChartSurface pieChartSurface) {
final PieRenderableSeries pieSeries = new PieRenderableSeries();
final PieSegmentCollection segmentsCollection = pieSeries.getSegmentsCollection();
segmentsCollection.add(createSegment(40, "Green", 0xff84BC3D, 0xff5B8829));
segmentsCollection.add(createSegment(10, "Red",0xffe04a2f, 0xffB7161B));
segmentsCollection.add(createSegment(20, "Blue",0xff4AB6C1, 0xff2182AD));
segmentsCollection.add(createSegment(15, "Yellow",0xff84BC3D, 0xff5B8829));
UpdateSuspender.using(pieChartSurface, () -> {
Collections.addAll(pieChartSurface.getRenderableSeries(), pieSeries);
});
}
private PieSegment createSegment(double value, String title, int gradientStart, int gradientEnd) {
final PieSegment pieSegment = new PieSegment();
pieSegment.setValue(value);
pieSegment.setTitle(title);
pieSegment.setFillStyle(new RadialGradientBrushStyle(0.5F, 0.5F, 0.5F, 0.5F, gradientStart, gradientEnd));
return pieSegment;
}
SciPieChartSurface Modifiers
The SciPieChartSurface supports modifiers like legend, selection, and tooltip.
Legend and Selection modifiers are synced if both are added. A PieSegment 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.
Pie Chart Legend
To add the PieChartLegendModifier, use the following code:
final PieChartLegendModifier legendModifier = new PieChartLegendModifier(getContext());
legendModifier.setSourceSeries(pieSeries);
legendModifier.setLegendPosition(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 17);
pieChartSurface.getChartModifiers().add(legendModifier);
Note
PieChartLegendModifier works similar to LegendModifier and has a similar API. To learn more, please visit the LegendModifier usage article.
Pie Chart Selection
To add the PieSegmentSelectionModifier, please use the following code:
final PieSegmentSelectionModifier selectionModifier = new PieSegmentSelectionModifier();
pieChartSurface.getChartModifiers().add(selectionModifier);
Note
PieSegmentSelectionModifier works similar to SeriesSelectionModifier and has a similar API. To learn more, please visit the SeriesSelectionModifier usage article.
Pie Chart Tooltip
The PieChartTooltipModifier allows inspecting segmentCollection at a touch point. To add the PieChartTooltipModifier, use the following code:
pieChartSurface.getChartModifiers().add(new PieChartTooltipModifier());
Note
PieChartTooltipModifier works similar to TooltipModifier and has a similar API. To learn more, please visit TooltipModifier Usage article.
Pie Segment Label Formatter
By default, the Pie Segment Label displays a relative percentage calculated on values of all segments in segmentCollection. This behavior can be controlled and to do so you’ll need to subclass PieSegmentLabelFormatterBase and provide your custom data in formatLabel(renderableSeries.IPieSegment pieSegment) 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:
class CustomPieSegmentLabelFormatter extends PieSegmentLabelFormatterBase {
@Override
public CharSequence formatLabel(IPieSegment pieSegment) {
return String.format("%.1f", pieSegment.getValue());
}
}
// Assume a donutSeries has been created somewhere
final DonutRenderableSeries donutSeries = new DonutRenderableSeries();
donutSeries.setPieSegmentLabelFormatter(new CustomPieSegmentLabelFormatter());
This produces the following output:
Multi Pie Donut Chart
In SciChart you can have both Pie Chart and Donut Chart placed inside the SciPieChartSurface at the same time.
Note
Examples of the Multi Pie Donut Chart can be found in the SciChart Android Examples Suite as well as on GitHub.