Pie Chart
Overview
A pie chart is a circular chart looking like a pie divided into slices (sectors). Slices show the percentage each value contributes to a total, the area of each slice being proportional to the quantity it represents, and the circle representing 100%. A pie chart with a blank circular area in the center is called a doughnut chart.
Pie charts are used very widely with small sets of data to compare categories. They cannot be multiple-series and should not be used when there are more than just a few categories.
This article explains how to create a basic Pie chart as well as configure settings that are specific to the type. You can also see the table below to get a brief overview of the Pie chart's characteristics:
Modules | Core + Pie and Doughnut / Base |
API | |
---|---|
Class | anychart.charts.Pie |
DATA | |
Data Fields | x, value |
Multiple Series | N/A |
OPTIONS | |
Stacked | N/A |
Vertical | N/A |
3D | 3D Pie |
Error Bars | N/A |
SUPPORTED CHART PLOTS | |
Polar | N/A |
Radar | N/A |
Scatter | N/A |
Stock | N/A |
RELATED TYPES | |
Doughnut | |
SEE ALSO | |
Chartopedia: Pie Chart | |
General Settings |
Modules
The Pie chart requires adding the Core and Pie and Doughnut modules:
<script src="https://cdn.anychart.com/releases/8.13.0/js/anychart-core.min.js"></script>
<script src="https://cdn.anychart.com/releases/8.13.0/js/anychart-pie.min.js"></script>
Alternatively, you can use the Base module, which includes, among other things, the two modules mentioned above:
<script src="https://cdn.anychart.com/releases/8.13.0/js/anychart-base.min.js"></script>
Learn more: Modules.
Quick Start
To create a Pie chart, use the anychart.pie() chart constructor, like in the following sample:
// create data
var data = [
{x: "A", value: 637166},
{x: "B", value: 721630},
{x: "C", value: 148662},
{x: "D", value: 78662},
{x: "E", value: 90000}
];
// create a chart and set the data
chart = anychart.pie(data);
// set the container id
chart.container("container");
// initiate drawing the chart
chart.draw();
General Settings
In AnyChart there are many settings that are configured in the same way for all chart types, including the Pie chart (for example, legend and interactivity settings).
Read the overview of general settings: General Settings.
Special Settings
Appearance
Slices
The appearance settings of slices can be configured in three states: normal, hover, and selected. Use the normal(), hovered(), and selected() methods.
Combine them with the following methods:
- fill() to set the fill
- hatchFill() to set the hatch fill
- stroke() to set the stroke
Also, you can use some other methods from anychart.core.StateSettings.
In the sample below, there a Pie chart with appearance settings configured:
// configure the visual settings of the chart
chart.normal().fill("#669999", 0.5);
chart.hovered().fill("#666699", 0.5);
chart.selected().fill("#666699", 0.7);
chart.normal().hatchFill("forward-diagonal", "#669999");
chart.hovered().hatchFill(null);
chart.selected().hatchFill(null);
chart.normal().stroke("#669999", 2);
chart.hovered().stroke("#669999", 2);
Outlines
Outlines are special elements for highlighting slices, looking like arcs around them.
By default, outlines are enabled only in the hover and selected states, but they can be enabled and configured in the normal state as well. To adjust them, use the normal(), hovered(), and selected() methods. Alternatively, you can use outline(), which affects all states at once.
Combine these methods with the following ones:
- enabled() to enable/disable the outline
- fill() to set the fill
- offset() to set the offset
- stroke() to set the stroke
- width() to set the width
The easiest way to enable or disable outlines in all states is calling outline() with true
or false
as a parameter:
chart.outline(false);
This sample shows a Pie chart with outlines configured:
// configure outlines
chart.normal().outline().enabled(true);
chart.normal().outline().width("5%");
chart.hovered().outline().width("10%");
chart.selected().outline().width("3");
chart.selected().outline().fill("#455a64");
chart.selected().outline().stroke(null);
chart.selected().outline().offset(2);
Individual Points
It is possible to configure the appearance of each chart point individually - use extra data fields corresponding to the methods mentioned in the Slices and Outlines sections:
// create data
var data = [
{x: "Pacman", value: 400,
normal: {
fill: "#ffff00",
hatchFill: "percent50"
},
hovered: {
fill: "#ffff00",
hatchFill: "percent50",
outline: {
enabled: true,
width: 6,
fill: "#404040",
stroke: null
}
},
selected: {
outline: {
enabled: true,
width: 6,
fill: "#404040",
stroke: null
}
}
},
{x: "Not Pacman", value: 130,
normal: {fill: "#404040"},
hovered: {
fill: "#404040",
outline: {
enabled: false
}
},
selected: {outline: {enabled: false}}
}
];
// create a chart and set the data
chart = anychart.pie(data);
Aqua Style
There is a special visual setting available for the Pie chart - Aqua Style. To apply it, use the fill() method with the "aquastyle"
parameter:
// enable aqua style
chart.fill("aquastyle");
Start Angle
You can set the angle where the first slice is placed - use the startAngle() method. The angle is 0° by default.
In the sample below, the start angle of the first chart is not configured, and for the second chart it is set to 90°:
// set the start angle
chart.startAngle(90);
Sorting Order
By default, slices are sorted in descending order according to their values. You can sort them in ascending order or disable sorting.
To set the sorting mode, call the sort() method with one of the parameters listed in anychart.enums.Sort:
"desc"
(default)"asc"
"none"
// set the sorting mode chart.sort("asc");
Exploded Slices
By default, there are no spaces between the slices of a Pie chart, and when a user clicks on a slice, it "explodes" moving away from the others. Usually, explosion indicates that a slice is selected.
You can set the range of explosion (in pixels or as a percentage) by using the explode() method in different states:
// set the explosion range in selected state
chart.selected().explode("3%");
Be default slices explode by 7% when selected.
To make some slices exploded (selected) by default, call select() with an array of their indexes as a parameter:
// explode the fourth and fifth slices
chart.select([3, 4]);
Another way to select a slice is to add a special field to your data, which should be set as an object:
var data = [
{x: "A", value: 637166},
{x: "B", value: 721630},
{x: "C", value: 148662, state:"selected"},
{x: "D", value: 78662},
{x: "E", value: 90000}
];
In this sample, there is a Pie chart with three slices selected by default and the explosion range set to 3% in selected state and to 1% when you move the mouse over them:
Radius
To set the radius of a Pie chart, call the radius() method and specify either a value or a percentage of the chart's bounds.
In the following sample, the radius of the first Pie chart is not configured, and the radius of the second one is set to 30%:
// set the radius
pie2.radius("30%")
The innerRadius() method allows you to set the inner radius of a Pie chart (which is 0 by default), thus turning it into a Doughnut chart. Read more in the Doughnut Chart article.
Labels
Labels are text or image elements that can be placed anywhere on any chart (you can enable them on a whole series or in a single point). For text labels, font settings and text formatters are available.
Outer Labels
By default, labels are placed on the Pie chart. However, you can place them outside of the chart by using the position() method with the "outside"
parameter:
// set the position of labels
chart.labels().position("outside");
To configure connectors (the lines connecting labels with slices), call the connectorStroke() method:
// configure connectors
chart.connectorStroke({color: "#595959", thickness: 2, dash:"2 2"});
Another setting available for outer labels is outsideLabelsCriticalAngle().
In this sample, there are outside labels with customized connectors:
Tooltips
A Tooltip is a text box displayed when a point on a chart is hovered over. There is a number of visual and other settings available: for example, you can edit the text by using font settings and text formatters, change the style of background, adjust the position of a tooltip, and so on.