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 multi-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:

API
Classanychart.charts.Pie
DATA
Data Fieldsx, value
Multiple SeriesN/A
OPTIONS
StackedN/A
VerticalN/A
3D3D Pie
Error BarsN/A
SUPPORTED CHART PLOTS
PolarN/A
RadarN/A
ScatterN/A
StockN/A
RELATED TYPES
Doughnut
SEE ALSO
Chartopedia: Pie Chart
General Settings

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();

Playground

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

All Points

The appearance settings of a Pie chart can be configured in two states: normal and hover. Use the normal() and hovered() methods.

Combine them with the following methods:

Also, you can use some other methods from anychart.core.StateSettings.

In the sample below, there a Pie chart with appearance settings configured:

chart.normal().fill("#669999", 0.5);
chart.hovered().fill("#669999", 0.3);
chart.normal().hatchFill("forward-diagonal", "#669999");
chart.hovered().hatchFill("forward-diagonal", "#669999", 3);
chart.normal().stroke("#669999", 3);
chart.hovered().stroke("#669999", 3);

Playground

Individual Points

It is possible to configure the appearance of each slice individually – use extra data fields corresponding with the methods mentioned above:

// create data
var data = [
  {x: "Pacman", value: 400,
   normal:  {
              fill: "#ffff00",
              hatchFill: "percent50"        
            },
   hovered: {
              fill: "#ffff00",
              hatchFill: "percent50"
            }
  },
  {x: "Not Pacman", value: 130,
   normal:  {fill: "#404040"},
   hovered: {fill: "#404040"}
  }
];

// create a chart and set the data
chart = anychart.pie(data);

Playground

Outline

Outline a special element that is usually used to highlight hovered and selected elements of a pie chart. It is enabled by default and looks like a thin extra arc that extends from a slice when it is hovered or selected.

Outline can be configured using the {api:anychart.charts.Pie#outline}{api} method or you can define how outline looks using states.

To disable outline effect use this code:

chart.outline(false);

Here is a sample of a pie chart with the tuned outline:

// enable outline in the normal state
chart.normal().outline().enabled(true);
chart.normal().outline().width("5%");
// make outline thick and red on hover
// and use object notation
chart.hovered().outline({width: "10%"});
// make outline thin and red on select
chart.selected().outline().width(3);
chart.selected().outline().fill("black");
chart.selected().outline().stroke(null);

Playground

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");

Playground

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
pie2.startAngle(90);

Playground

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
    pie1.sort("desc");
    pie2.sort("asc");
    pie3.sort("none");
    

Playground

Exploded Slices

By default, there is no spaces between the slices of a Pie chart, and when a user clicks a slice, it "explodes" moving away from the others and this state is considered to show that a slice is selected.

You can configure the range of explosion by using the explode() method (you can do both pixels and percents):

// set the explosion range
chart.explode("3%");

To make some slices exploded by default, use the select():

// explode the third slice
chart.select([2]);

Another way to enable explode/select 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, state: "selected"},
  {x: "C", value: 148662},
  {x: "D", value: 78662},
  {x: "E", value: 90000}
];

In this sample, there is a Pie chart with two slices exploded by default and the explosion range set to 3%:

Playground

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%")

Playground

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:

Playground

Tooltips

A Tooltip is a text box displayed when a point on a chart is hovered. 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.