Skip to content

Anatomy of a Pie Chart

In the course of this tutorial, we’ll explain how, and build an actual working Pie chart.

Before we can do anything, we need to create a Pie chart object, which in this case an instance of a PieChart class.

A Pie chart, at the very least needs the following things:

  1. Chart instance;
  2. Data;
  3. At least one Series.

This tutorial will walk through all of these.

Needless to say, before you can do anything, you need to make sure all required modules (in TypeScript), or files (in JavaScript) are imported. Please refer to our Getting started articles for more details:

For a Pie chart, we’ll need to import core (main module) and charts modules.

import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";

We’ll be importing and referring these as am4core and am4charts in the course of this article, and overally in our demos and tutorials.

For that we’re going to be using am4core.create() function. (or am4core.createFromConfig() if you are using JSON-based config approach)

let chart = am4core.create("chartdiv", am4charts.PieChart);

Just like for most chart types, the data for a Pie chart is an array of objects. Each of those objects will represent a Slice and must at least contain one numeric value (Slice value) and a category (Slice title).

We assign the data to the data to chart’s data property:

chart.data = [{
"country": "Lithuania",
"litres": 501.9
}, {
"country": "Czech Republic",
"litres": 301.9
}, {
"country": "Ireland",
"litres": 201.1
}, {
"country": "Germany",
"litres": 165.8
}, {
"country": "Australia",
"litres": 139.9
}, {
"country": "Austria",
"litres": 128.3
}, {
"country": "UK",
"litres": 99
}, {
"country": "Belgium",
"litres": 60
}, {
"country": "The Netherlands",
"litres": 50
}];

We can also make the chart load external data using dataSource.

chart.dataSource.url = "pie_chart_data.json";

Please refer to our “External data” article, for more details.

In amCharts 4 universe, Pie chart is a serial chart.

A series of Slices is represented by an instance of PieSeries class.

In most serial charts, Pie chart included, all Series are included in a List accessible via chart’s series property.

To create a new Series, we just going to create a new instance of `PieSeries` and push it into `chart.series`:

let pieSeries = chart.series.push(new am4charts.PieSeries());

Now that we have a Series, we need to set it up.

At the very least, we need to specify the fields in data, this Series will get its values from. (remember a PieSeries needs a numeric value and a string title/category for each Slice)

That’s where Series property dataFields comes into play:

pieSeries.dataFields.value = "litres";
pieSeries.dataFields.category = "country";

As you can see, Slice’s numeric value is represented by a data field “value”, while string title with a “category”.

Now that we have all the required elements, let’s put it all together:

// Import modules
import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
// Create chart instance
let chart = am4core.create("chartdiv", am4charts.PieChart);
// Add data
chart.data = [{
"country": "Lithuania",
"litres": 501.9
}, {
"country": "Czech Republic",
"litres": 301.9
}, {
"country": "Ireland",
"litres": 201.1
}, {
"country": "Germany",
"litres": 165.8
}, {
"country": "Australia",
"litres": 139.9
}, {
"country": "Austria",
"litres": 128.3
}, {
"country": "UK",
"litres": 99
}, {
"country": "Belgium",
"litres": 60
}, {
"country": "The Netherlands",
"litres": 50
}];
// Add and configure Series
let pieSeries = chart.series.push(new am4charts.PieSeries());
pieSeries.dataFields.value = "litres";
pieSeries.dataFields.category = "country";

Here’s a live version of the chart we’ve just created: (implemented in JavaScript)

Open in CodePen

There are so much more settings we can apply to chart, Series and the slices.

For example, we can make Pie chart into a Donut chart by setting chart’s innerRadius property:

chart.innerRadius = am4core.percent(40);

While we’re at it, let’s make each slice have white outline borders. To do that, we’ll need to access PieSeries’ slices property, or more precisely its template property.

PieSerie’s slices.template holds a dummy object of the Slice class, which will be used as a template (hence the name) when creating actual Slices. It has all the same properties as the Slice class (because it is an instance of the Slice class, duh). Anything you set on a template will make it onto actual slices.

OK, so let’s add a dark border around each slice to make them more prominent:

pieSeries.slices.template.stroke = am4core.color("#4a2abb");
pieSeries.slices.template.strokeWidth = 2;
pieSeries.slices.template.strokeOpacity = 1;

And voilà!

Open in CodePen

You can also bind virtually any property of the slice to a field in data using what we call “property fields”.

Read more about property fields, or if you’re short on time, property fields is basically an object defining which property (key) should draw its value in data.

Suppose we have a “color” key in our data:

[{
"country": "Lithuania",
"litres": 501,
"color": am4core.color("#ED1C24")
}, {
"country": "Czechia",
"litres": 301,
"color": am4core.color("#235789")
}, {
"country": "Ireland",
"litres": 201,
"color": am4core.color("#F1D302")
}, {
"country": "Germany",
"litres": 165,
"color": am4core.color("#020100")
}]

Now we can use propertyFields of the slice template to bind its fill property a “color” field in data:

pieSeries.slices.template.propertyFields.fill = "color";

Let’s see how this looks on a live chart:

Open in CodePen

It’s possible to disable various elements like ticks, labels and rollover tooltips.

To disable the former, use templates of the respectively named ticks and labels list templates:

pieSeries.labels.template.disabled = true;
pieSeries.ticks.template.disabled = true;

To disable the tooltip which appears on slice hover, simply reset tooltipText on its template:

pieSeries.slices.template.tooltipText = "";

Here’s a working example with all of the above:

Open in CodePen

In short, states is a powerful amCharts 4 concept allowing application of property values to elements when certain actions happen.

Like, for example, an element is hovered.

Pie series’ slices has already three states pre-created for us:

  1. "default" state, which is applied when slice is in its initial position.
  2. "hover" state, which is applied when you hover or touch the slice.
  3. "active" state, which is applied when you click or tap the slice so it pulls out.

Each of those states, apply values to certain slice properties to modify its appearance.

For example, "hover" state, when applied, sets slice’s scale to 1.1 making it grow slightly.

When you click a slice and "active" state is applied, it sets slice’s shiftRadius property, which makes slice look pulled out.

To modify how slices behave in each of those scenarios, you need to modify the respective states’ properties values.

For example, if we don’t want the slice to grow on hover, we’d do this:

let hs = pieSeries.slices.template.states.getKey("hover");
hs.properties.scale = 1;

Similarly, if we don’t want slice to pull out on click, we’d need to modify "active" state:

let as = pieSeries.slices.template.states.getKey("active");
as.properties.shiftRadius = 0;

We can also, modify any of the slices’ properties that way just as well.

For example, if we want to disable growing on hover, but would rather modify fill opacity of the slice, we can do this:

pieSeries.slices.template.fillOpacity = 1;
let hs = pieSeries.slices.template.states.getKey("hover");
hs.properties.scale = 1;
hs.properties.fillOpacity = 0.5;
Open in CodePen

One more thing before you go. There’s also a specially-named theme: "hoverActive". It is not created by default, so you would need to create it. If created, it will be applied in those situations when an “active” slice is hovered.

Even if it’s not required, adding a legend might improve user experience. It can provide visual reference for the user, as well as allow toggling certain Slices.

To add a legend, we simply create an instance of Legend class and assign it to chart’s legend property. It’s as simple as that.

chart.legend = new am4charts.Legend();

amCharts 4 makes it easy to apply themes to am4charts. Let’s make our chart more lively by enabling “Animated” theme.

To do so, we’ll need to remember to import the theme itself as well as set the system to use it.

// Import modules
import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
import am4themes_animated from "@amcharts/amcharts4/themes/animated";
// Set theme
am4core.useTheme(am4themes_animated);
// Create chart instance
let chart = am4core.create("chartdiv", am4charts.PieChart);
// ...
Open in CodePen

(click RERUN or legend items to see how much more nicer interactions feel like now)

A Pie chart can have more than one PieSeries. To create nested Pie/Donut charts, we simply add multiple PieSeries. (making sure they have their dataFields configured properly to point at correct properties in the data)

We’re not going to bloat this article with a code example. We’re just going to slam you with a live demo:

Open in CodePen

By default Pie chart displays all values - in slice labels, rollover tooltips, legend - as percent.

If you’d rather like to display them as absolute values, or otherwise modify default information, you will need to adjust various settings across your chart.

REQUIRED READING The further info in this chapter relies heavily on the concept of binding text placeholder to data. Make sure you read about Text placeholders before proceeding.

Slice label content is regulated by a sample template object of the Pie series, accessible by pieSeries.labels.template.

So, if we’d like to change what is displayed in it we will need to modify label.text property of it:

pieSeries.labels.template.text = "{category}: {value.value}";

What goes into tooltip when you hover a Pie slice is regulated by slice template’s tooltipText property.

If you’d like to change it, that’s the property you set:

pieSeries.slices.template.tooltipText = "{category}: {value.value}";

Finally, if you’d like to set what value is displayed in the legend, you’ll need to set text for its value labels template:

chart.legend.valueLabels.template.text = "{value.value}";

Normally, a Pie chart will take up 80% of the chart container’s width or height - whichever is smaller.

The extra 20% are for accommodating possible labels and slice pull outs.

Like anything else it is changeable. For that we have chart’s radius property.

It can be either fixed pixel value, or relative value in percents (relative to the space available to the chart).

Say, we have labels disabled and would want our chart reclaim the available space instead. We’d just need to set radius to 100% percent:

chart.radius = am4core.percent(100);

A 3D Pie in amCharts 4 is a separate chart type represented by a class PieChart3D. It is almost identical to PieChart, save for a few extra properties that deal with the 3D appearance.

To oversimplify, to convert any regular Pie chart to a 3D Pie chart, slap a “3D” to any class and/or module:

Regular Pie chart 3D Pie chart
am4charts.PieChart am4charts.PieChart3D
am4charts.PieSeries am4charts.PieSeries3D
am4charts.Legend am4charts.Legend (Legend is not 3D; it’s shared across multiple chart types)
Open in CodePen

With the help if SliceGrouper plugin, you can make Pie Chart automatically group small slices into “Other” group.

Follow the link above for more information.

You might also benefit from looking at the following Pie chart related tutorials: