PieCharts - eclipse/swtchart GitHub Wiki
Pie Charts
Description
Pie charts are often used to display percentage data.
Types
- Pie Chart
- Doughnut Chart
- Multilevel Pie Chart
- Multilevel Doughnut Chart
Example
public Chart createChart(Composite parent) {
double[] values = {337309, 131646, 128948, 100123, 81708, 70478, 58226, 47806, 4067, 265783};
String[] labels = {"USA", "Spain", "Italy", "Germany", "China", "France", "Iran", "UK", "India", "Other"};
Chart chart = new Chart(parent, SWT.NONE);
chart.getTitle().setText("Doughnut Chart");
ICircularSeries<?> circularSeries = (ICircularSeries<?>)chart.getSeriesSet().createSeries(SeriesType.DOUGHNUT, "pie series");
circularSeries.setSeries(labels, values);
Color color = Display.getDefault().getSystemColor(SWT.COLOR_DARK_RED);
circularSeries.setColor("India", color);
//
return chart;
}
ReadyMade APIs
Basic
org.eclipse.swtchart
Pie Charts and Doughnut Charts in the SWTChart bundle support building single level and MultiLevel Charts.
Initializing
Chart chart = new Chart(parent, SWT.NONE);
chart.getTitle().setText("Title");
The only point where the APIs for Pie Chart and Doughnut Chart differ is this.
Pie Charts
ICircularSeries<?> circularSeries = (ICircularSeries<?>)chart.getSeriesSet().createSeries(SeriesType.PIE, "series title here");
Doughnut Charts
ICircularSeries<?> circularSeries = (ICircularSeries<?>)chart.getSeriesSet().createSeries(SeriesType.DOUGHNUT, "series title here");
Setting Data
Set the base series
circularSeries.setSeries(String[] labels, double[] values);
Error is thrown if labels.length != values.length
Set the children of any element.
circularSeries.getNodeById("Element Label").addChildren(String[] childrenLabels, double childrenValues);
Set settings
Change color of a node
circularSeries.setColor("Label of Node", Color color);
Highlight a node
multiLevelPie.setHighlightedNode(multiLevelPie.getNodeById("Label"));
Set the highlight settings
circularSeries.setHighlightLineWidth(int width);
circularSeries.setHighlightColor(Color color);
Border settings
circularSeries.setBorderStyle(int borderStyle);
circularSeries.setBorderWidth(int borderWidth);
circularSeries.setBorderColor(Color color);
Use the same data for different circular chart
circularSeries.setDataModel(IdNodeDataModel data);
circularSeries.getModel();
Get the node from the position
circularSeries.getPieSliceFromPosition(int x, int y);
Extensions
org.eclipse.swtchart.extensions
Initialize
For both Pie Chart and Doughnut Chart,
PieChart circularChart = new PieChart(Composite parent, int style);
Set Data
ICircularSeriesData circularSeriesData = new CircularSeriesData();
circularSeriesData.setSeries(String[] labels, double[] values);
circularSeriesData.getNodeById("Element Label").addChildren(String[] childrenLabels, double childrenValues);
Set the settings, before applying the data to the chart
circularChart.addSeriesData(circularSeriesData);
Set Series Settings
ICircularSettings circularSettings = circularSeriesData.getSettings();
Set SeriesType
circularSettings.setSeriesType(SeriesType type);
setting to render chart dynamically
circularSettings.setRedrawOnClick(boolean setRedrawOnClick);
// set true to render chart dynamically. If false, will highlight the node on click.
when false
on clicking on Asia
when true
on clicking Asia
setting to maximise the space utilised by the charts
circularSettings.setFillEntireSpace(boolean fillEntireSpace);
//set true to enable
Set Chart Settings
IChartSettings chartSettings = getChartSettings();
Set tooltip
Displays data of the node on hover.
chartSettings.setShowLegendMarker(boolean showLegendMarker);
Parallel Pie Charts
Initialization
ParallelPieCharts(Composite parent, SeriesType type, boolean redraw);
- SeriesType is SeriesType.PIE for Pie Charts, SeriesType.Doughnut for Doughnut Charts
- The third Parameter is true if dynamically the charts need to be redrawn, else they will be highlighted in sync.
Data Series
Each chart is drawn separately, but are in sync with each other. The APIs cover all the functionalities that individual Pie Charts can offer.
Single Level parallel Pies
addPieChartSeries(String labels[], double val[][]);
- The first argument denotes the label of each pie slice of a chart.
- The size of the second argument is same as that of the first, and each of the element is the list of values the label string in the corresponding first argument will take in the charts. (Same as in Linux Pie Chart Tools)
Multi-level parallel pies
addChild(String parentId, String childId, double[] vals);
- The first argument is the label of the Pie Slice to which we are adding the Child
- The second argument is the label of the node we wish to introduce into the charts
- The third argument is the list of values that the node takes in all the charts.
Setting Chart Titles
setChartTitles(String[] titles);
- Sets the titles of the charts