Angular Pie Chart
An Angular pie chart built on D3, with animated transitions: a circle split into slices, each sized by its share of the total. For the slices drawn as a ring, with room for a total in the center, see the Donut Chart.
Pie
When To Use A Pie Chart
A pie chart shows how a whole splits into parts: each slice's angle is its share of the total, so it answers "what fraction is this?" at a glance. It works best when:
- the parts add up to a meaningful whole — a budget, a survey's answers, a day's traffic by source;
- there are only a few of them, about five or fewer, so every slice is big enough to see and label;
- one or two parts stand out, since the eye compares a slice against the whole far better than against a slice of nearly the same size.
Reach for another chart when those don't hold. To compare parts that are close in size, or more than a handful of them, a bar chart puts the values on a common scale. To show how the parts change over time, use a line or area chart. And for the same slices with the total written in the middle, use the donut chart, which takes the same data.
Every slice can be reached from the keyboard, and is announced by its key and value, so a pie chart doesn't depend on seeing its colors (see Accessibility).
How The Slices Are Drawn
Each item in data is one slice, with its key as the name and its value as the size. The chart adds up the values and gives each slice its share of the circle, so you pass raw numbers — counts, amounts, minutes — and never need to work out percentages first.
- Data order is drawing order. Slices aren't sorted: the first item starts at twelve o'clock and the rest follow clockwise. Sort your data largest first when you want the biggest slice at the top and the smallest tucked in beside it, or keep a fixed order (say, the same categories every month) so the colors and positions stay put from one chart to the next.
- Colors follow position. The first slice takes the theme palette's first color, the second the second, and so on — or the colors you list in colorOverride, in the same order.
- Hiding a slice closes it up. An item with
hide: truecounts as zero, so the other slices grow to fill the circle, while it keeps its place and its color for when it's shown again. - Hover and focus. Pointing at a slice, or moving to it with the arrow keys, pulls it out a little, darkens it and shows a tooltip with its key and value; a custom tooltip can show anything else you keep on the item.
import { PcacData, PcacPieChartConfig } from '@pioneer-code/pioneer-charts';
const slice = (key: string, value: number, hide = false): PcacData => ({ key, value, hide, data: [] });
const config: PcacPieChartConfig = {
height: 200,
data: [
slice('Brownie', 898), // first: starts at twelve o'clock
slice('Peanuts', 787),
slice('Candy', 600),
slice('Chocolate Chip', 300),
slice('Chips', 123, true), // hidden: counts as zero until shown
],
};With A Legend
A legend names the slices, and lets readers switch them off to compare the rest. Click an item below to hide or show its slice.
Snacks, with a legend
Click a legend item to hide or show its slice.
The legend's items and the chart's slices come from the same data, in the same order: a legend item with no colorOverride takes the theme palette's color for its position, just as the slice does. Hiding a slice sets its hide rather than removing it, so it keeps its place — and its color — and the swatches keep matching.
import { Component, computed, signal } from '@angular/core';
import { PcacLegend, PcacLegendConfig, PcacLegendConfigItem, PcacPieChart, PcacPieChartConfig } from '@pioneer-code/pioneer-charts';
@Component({
imports: [PcacLegend, PcacPieChart],
template: `
<pcac-legend [config]="legend()" (itemClicked)="onItemClicked($event)" />
<pcac-pie-chart [config]="pie()" />
`,
})
export class SnacksComponent {
readonly config = signal<PcacPieChartConfig>({ data: [ ... ], height: 200 });
/** The slices switched off from the legend, by key. */
readonly hidden = signal<ReadonlySet<string>>(new Set());
readonly legend = computed<PcacLegendConfig>(() => ({
heading: 'Snacks',
items: this.config().data.map((slice) => ({
label: String(slice.key),
checked: !this.hidden().has(String(slice.key)),
colorOverride: null,
})),
}));
readonly pie = computed<PcacPieChartConfig>(() => ({
...this.config(),
data: this.config().data.map((slice) => ({ ...slice, hide: this.hidden().has(String(slice.key)) })),
}));
onItemClicked(items: PcacLegendConfigItem[]): void {
this.hidden.set(new Set(items.filter((item) => !item.checked).map((item) => item.label)));
}
}Handling Clicks
A pie chart often sits at the top of a drill-down: click a slice to see the records behind it. (sliceClicked) emits a copy of the clicked item — from the mouse, or from Enter or Space on a focused slice — so give each item an id that leads back to your own data, rather than matching on its display name.
import { Component, inject, signal } from '@angular/core';
import { Router } from '@angular/router';
import { PcacData, PcacPieChart, PcacPieChartConfig } from '@pioneer-code/pioneer-charts';
@Component({
imports: [PcacPieChart],
template: `<pcac-pie-chart [config]="config()" (sliceClicked)="open($event)" />`,
})
export class SalesByRegionComponent {
private readonly router = inject(Router);
readonly config = signal<PcacPieChartConfig>({
height: 240,
data: [
{ id: 'na', key: 'North America', value: 412, hide: false, data: [] },
{ id: 'eu', key: 'Europe', value: 305, hide: false, data: [] },
{ id: 'apac', key: 'Asia Pacific', value: 188, hide: false, data: [] },
],
});
open(slice: PcacData): void {
this.router.navigate(['/sales/regions', slice.id]);
}
}Pie Or Donut?
Both charts take the same data and draw the same slices; the donut chart cuts a hole in the middle and can put a label in it. Choose the pie when the slices are the whole story and space is tight — a full circle gives each slice the most area for its size. Choose the donut when there's one number readers should see first, like the total or the share of a single slice, or when the chart sits in a dashboard beside other figures and the label saves a separate heading.
Markup
<pcac-pie-chart [config]="config" (sliceClicked)="onClicked($event)" />
API
import { PcacPieChart, PcacPieChartConfig } from '@pioneer-code/pioneer-charts';Configuration
| Name | Description |
|---|---|
| data | Collection of PcacData objects. |
| height | Height of chart component. Default is 200px. |
| ariaLabel | What the chart shows, for screen readers — e.g. 'Sales by month, 2026'. The chart is announced as a group with this name, and each bar, slice or point inside it by its own key and value (see Accessibility). When omitted, its type is used: 'Bar chart', 'Pie chart', 'Line chart' and so on. |
| colorOverride | Slice colors, any CSS color, used in data order instead of the theme palette: the first slice takes the first color. Repeated when there are more slices than colors. Omitted or empty, the theme palette is used. To match a legend, give each of its items the same color with colorOverride. |
Events
| Binding | Description | Payload |
|---|---|---|
| (sliceClicked)="onClicked($event)" | Emitted when a slice is clicked. It emits a copy of the item; set its id to trace it back to your record (see Data Contract). | PcacData |