Skip to main content
Version: 6.2

Doughnut chart

(quoted from Doughnut chart definition in Wikipedia)

A doughnut chart is a variant of the pie chart, with a blank center allowing for additional information about the data as a whole to be included. Doughnut charts are similar to pie charts in that their aim is to illustrate proportions. This type of circular graph can support multiple statistics at once and it provides a better data intensity ratio to standard pie charts.

A doughnut charts are the same of pie charts and then divided in the segments, the arc of each segment shows the proportional value of each piece of data.

They are excellent at showing the relational proportions between data.

Programmatically, you could use a doughnut chart as following:

// creates the chart    
DoughnutChart chart = new DoughnutChart();
// adds to DOM
component.add(chart);
...
// example for Elemental2
// gets the chart instance as DOM element
Element element = chart.getChartElement().as();
// adds to DOM
DomGlobal.document.body.appendChild(element);

By UIBinder (ONLY for GWT), you could use a doughnut chart as following:

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:c="urn:import:org.pepstock.charba.client.gwt.widgets">

<g:HTMLPanel width="100%">
....
<c:DoughnutChartWidget ui:field="chart"/>
...
</g:HTMLPanel>
</ui:UiBinder>

Dataset

The doughnut chart allows to define the data and a number of properties, used to display the data, by a doughnut dataset.

Every chart has got a method to create a typed dataset accordingly with the chart type. The dataset can be also created instantiating the constructor.

// creates the chart
DoughnutChart chart = new DoughnutChart();
// creates the dataset
DoughnutDataset dataset = chart.newDataset();
// sets the option
dataset.setBackgroundColor(HtmlColor.RED);
...
// creates the dataset
DoughnutDataset datasetNew = new DoughnutDataset();
// sets the option
datasetNew.setBackgroundColor(HtmlColor.RED);
...
// sets the datasets to the chart
chart.getData().setDatasets(dataset, datasetNew);

The following are the attributes that you can set:

NameTypeScriptableDescription
backgroundColorString[] - IsColor[] - Pattern[] - Gradient[]YesThe fill color/pattern of the arcs in the dataset.
borderAlignBorderAlignYesThe borders alignment of arcs.
borderColorString[] - IsColor[] - Gradient[]YesThe border color of the arcs in the dataset.
borderJoinStyleJoinStyle[]YesArc line joint style. See MDN.
borderRadiusint[] - ArcBorderRadius[]YesThe arc border radius (in pixels).
borderWidthint[]YesThe border width of the arcs in the dataset.
circumferencedouble-Sweep to allow arcs to cover.
For default value, see doughnuts options.
clipboolean - double - Clip-How to clip relative to chart area. Positive value allows overflow, negative value clips that many pixels inside chart area.
hoverBackgroundColorString[] - IsColor[] - Pattern[] - Gradient[]YesThe fill color/pattern of the arcs when hovered.
hoverBorderColorString[] - IsColor[] - Gradient[]YesThe stroke color of the arcs when hovered.
hoverBorderJoinStyleJoinStyle[]YesArc line joint style, when hovered. See MDN.
hoverBorderWidthint[]YesThe stroke width of the arcs when hovered.
hoverOffsetintYesThe offset of the arc (in pixels) when hovered.
labelString-The label for the dataset.
offsetintYesThe offset of the arc (in pixels).
normalizedboolean-If true, you provide data with indices that are unique, sorted, and consistent across data sets and provide the normalized.
rotationdouble-Starting angle to draw arcs from.
For default value, see doughnuts options.
parsingboolean-If false, the data set parsing is disable. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
spacingint-Fixed arc offset (in pixels). Similar to offset but applies to all arcs.
weightdouble-The relative thickness of the dataset. Providing a value for weight will cause the doughnut dataset to be drawn with a thickness relative to the sum of all the dataset weight values.

General

The general options for a doughnut dataset can control behaviors not related to styling or interactions and they are the following:

NameDefaultsDescription
clipUndefined.DOUBLEHow to clip relative to chart area.
labelnullThe label for the dataset.
normalizedfalseIf true, you provide data with indices that are unique, sorted, and consistent across data sets and provide the normalized.
parsingtrueIf false, the data set parsing is disable. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
info

label options is just a pure label to add to the dataset and is NOT the label for the dataset which appears in the legend and tooltips.

Styling

The style of each arc of the dataset can be configured by the following properties:

NameDescription
backgroundColorThe fill color of the arcs in the dataset.
borderAlignThe borders alignment of arcs.
borderColorThe border color for arcs.
borderJoinStyleArc line joint style
borderRadiusThe arc border radius (in pixels).
borderWidthThe width of the arc border in pixels.
offsetThe offset of the arc (in pixels).
spacingFixed arc offset (in pixels). Similar to offset but applies to all arcs.
weightThe relative thickness of the dataset.

All above options have got the fallback to the associated Arc elements, retrievable by the following statements:

// from chart instance
Arc arc = chart.getOptions().getElements().getArc();
// sets options for all pie datasets of the chart
arc.setOffset(6);
...
// from defaults
Arc defaultArc = Defaults.get().getGlobal().getElements().getArc();
// sets options for all pie datasets of all charts
defaultArc.setOffset(6);

Interactions

The interaction with each arc can be controlled with the following properties:

NameDescription
hoverBackgroundColorThe fill color of the arcs in the dataset when hovered.
hoverBorderColorThe border color for arcs when hovered.
hoverBorderJoinStyleArc line joint style when hovered.
hoverBorderWidthThe width of the arc border in pixels when hovered.
hoverOffsetThe offset of the arc (in pixels) when hovered.

All above options have got the fallback to the associated Arc elements, retrievable by the following statements:

// from chart instance
Arc arc = chart.getOptions().getElements().getArc();
// sets options for all pie datasets of the chart
arc.setOffset(6);
...
// from defaults
Arc defaultArc = Defaults.get().getGlobal().getElements().getArc();
// sets options for all pie datasets of all charts
defaultArc.setOffset(6);

Scriptable

Scriptable options at dataset level accept a callback which is called for each of the underlying data values. See more details in Configuring charts section.

// creates chart
DoughnutChart chart = new DoughnutChart();
// creates dataset
DoughnutDataset dataset = chart.newDataset();
// sets the option by a callback
dataset.setBackgroundColor(new ColorCallback<DatasetContext>(){

@Override
public IsColor invoke(DatasetContext context){
// logic
return color;
}
});

The following options can be set by a callback:

NameCallbackReturned types
backgroundColorColorCallback<DatasetContext>String - IsColor - Pattern - Gradient
borderAlignBorderAlignCallbackBorderAlign
borderColorColorCallback<DatasetContext>String - IsColor - Gradient
borderJoinStyleJoinStyleCallback<DatasetContext>JoinStyle
borderRadiusBorderRadiusCallback<DatasetContext>int - ArcBorderRadius
borderWidthWidthCallback<DatasetContext>int
hoverBackgroundColorColorCallback<DatasetContext>String - IsColor - Pattern - Gradient
hoverBorderColorColorCallback<DatasetContext>String - IsColor - Gradient
hoverBorderJoinStyleJoinStyleCallback<DatasetContext>JoinStyle
hoverBorderWidthWidthCallback<DatasetContext>int
hoverOffsetOffsetCallback<DatasetContext>int
offsetOffsetCallback<DatasetContext>int

Data structure

The data of a dataset for a doughnut chart is specified as a an array of numbers. Each point in the data array corresponds to the label at the same index.

// sets data as an array of doubles
dataset.setData(1,2,3,4,6);

// sets data as a list of double
List<Double> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
dataset.setData(list);

To provide context of what each point means, we include an array of strings that show around each point in the chart.

...
chart.getData().setLabels("label1", "label2");
...

Options

The doughnut chart defines specific options implementation to be configured. These options are merged with the global chart configuration options to form the options passed to the chart.

// creates chart
DoughnutChart chart = new DoughnutChart();
// gets the chart options
DoughnutOptions options = chart.getOptions();
// sets options
options.setResponsive(true);
options.setCircumference(180D);

These are the options specific to doughnut charts:

NameTypeDefaultDescription
circumferencedouble360Sweep to allow arcs to cover.
cutoutPercentageString50%The percentage of the chart that is cut out of the middle. It's a string, ending with '%', as percentage of the chart radius.
cutoutdoubleUndefined.DOUBLEThe portion of the chart that is cut out of the middle, in pixels.
radiusPercentageString100%the outer radius of the chart. It's a string, ending with '%', as percentage of the maximum radius.
radiusdoubleUndefined.DOUBLEThe outer radius of the chart, in pixels.
rotationdouble0Starting angle to draw arcs from.