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:
Name | Type | Scriptable | Description |
---|---|---|---|
backgroundColor | String[] - IsColor[] - Pattern[] - Gradient[] | Yes | The fill color/pattern of the arcs in the dataset. |
borderAlign | BorderAlign | Yes | The borders alignment of arcs. |
borderColor | String[] - IsColor[] - Gradient[] | Yes | The border color of the arcs in the dataset. |
borderRadius | int[] - ArcBorderRadius[] | Yes | The arc border radius (in pixels). |
borderWidth | int[] | Yes | The border width of the arcs in the dataset. |
circumference | double | - | Sweep to allow arcs to cover. For default value, see doughnuts options. |
clip | boolean - double - Clip | - | How to clip relative to chart area. Positive value allows overflow, negative value clips that many pixels inside chart area. |
hoverBackgroundColor | String[] - IsColor[] - Pattern[] - Gradient[] | Yes | The fill color/pattern of the arcs when hovered. |
hoverBorderColor | String[] - IsColor[] - Gradient[] | Yes | The stroke color of the arcs when hovered. |
hoverBorderWidth | int[] | Yes | The stroke width of the arcs when hovered. |
hoverOffset | int | Yes | The offset of the arc (in pixels) when hovered. |
label | String | - | The label for the dataset. |
offset | int | Yes | The offset of the arc (in pixels). |
normalized | boolean | - | If true , you provide data with indices that are unique, sorted, and consistent across data sets and provide the normalized. |
rotation | double | - | Starting angle to draw arcs from. For default value, see doughnuts options. |
parsing | boolean | - | 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. |
spacing | int | - | Fixed arc offset (in pixels). Similar to offset but applies to all arcs. |
weight | double | - | 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:
Name | Defaults | Description |
---|---|---|
clip | Undefined.DOUBLE | How to clip relative to chart area. |
label | null | The label for the dataset. |
normalized | false | If true , you provide data with indices that are unique, sorted, and consistent across data sets and provide the normalized. |
parsing | true | 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. |
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:
Name | Description |
---|---|
backgroundColor | The fill color of the arcs in the dataset. |
borderAlign | The borders alignment of arcs. |
borderColor | The border color for arcs. |
borderRadius | The arc border radius (in pixels). |
borderWidth | The width of the arc border in pixels. |
offset | The offset of the arc (in pixels). |
spacing | Fixed arc offset (in pixels). Similar to offset but applies to all arcs. |
weight | The 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:
Name | Description |
---|---|
hoverBackgroundColor | The fill color of the arcs in the dataset when hovered. |
hoverBorderColor | The border color for arcs when hovered. |
hoverBorderWidth | The width of the arc border in pixels when hovered. |
hoverOffset | The 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:
Name | Callback | Returned types |
---|---|---|
backgroundColor | ColorCallback<DatasetContext> | String - IsColor - Pattern - Gradient |
borderAlign | BorderAlignCallback | BorderAlign |
borderColor | ColorCallback<DatasetContext> | String - IsColor - Gradient |
borderRadius | BorderRadiusCallback | int - ArcBorderRadius |
borderWidth | WidthCallback<DatasetContext> | int |
hoverBackgroundColor | ColorCallback<DatasetContext> | String - IsColor - Pattern - Gradient |
hoverBorderColor | ColorCallback<DatasetContext> | String - IsColor - Gradient |
hoverBorderWidth | WidthCallback<DatasetContext> | int |
hoverOffset | OffsetCallback<DatasetContext> | int |
offset | OffsetCallback<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:
Name | Type | Default | Description |
---|---|---|---|
circumference | double | 360 | Sweep to allow arcs to cover. |
cutoutPercentage | String | 50% | The percentage of the chart that is cut out of the middle. It's a string, ending with '%', as percentage of the chart radius. |
cutout | double | Undefined.DOUBLE | The portion of the chart that is cut out of the middle, in pixels. |
radiusPercentage | String | 100% | the outer radius of the chart. It's a string, ending with '%', as percentage of the maximum radius. |
radius | double | Undefined.DOUBLE | The outer radius of the chart, in pixels. |
rotation | double | 0 | Starting angle to draw arcs from. |