Chapter 12 Charts
Pie chart
Parts of one whole, at one moment. Up to five slices: people judge angles badly, so the rest join into one. Every slice has a texture and its share in words, and one button shows every number as a table.
import { PieChartComponent } from '@okaapi-ui/angular/charts';Add charts to your app
Charts have their own import, so apps without charts download nothing. They draw with TanStack Charts, installed next to the kit.
pnpm add @tanstack/charts@1.5.0Pie
The biggest slice starts at the top. Press Tab and use the arrow keys to read each one.
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
const channels = [
{ name: 'Web', orders: 417 },
{ name: 'Branch', orders: 321 },
{ name: 'Phone', orders: 158 },
];
<ui-pie-chart
label="Orders by channel"
[rows]="channels"
name="name"
value="orders"
/>Donut
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
<ui-pie-chart
label="Orders by channel, donut"
[rows]="channels"
name="name"
value="orders"
donut
/>A number in the middle
The total, or the one thing to remember.
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
<ui-pie-chart
label="Orders by channel, with the total"
[rows]="channels"
name="name"
value="orders"
donut
center="992"
centerText="orders"
/>Half a ring
For a short, wide box.
- Yes 69%
- No 20%
- Not sure 11%
<ui-pie-chart
label="Would you order again?"
[rows]="answers"
name="name"
value="orders"
half
[height]="220"
center="69%"
centerText="said yes"
sort="none"
/>Names around it
Where there is room; on a narrow screen the names stay in the legend.
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
<ui-pie-chart
label="Orders by channel, named"
[rows]="channels"
name="name"
value="orders"
labels
[height]="320"
/>Names around a donut
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
<ui-pie-chart
label="Orders by channel, named donut"
[rows]="channels"
name="name"
value="orders"
labels
donut
center="992"
[height]="320"
/>Many small parts
After the fourth, the rest join into Other. The table still lists every one.
- Cairo 41%
- Alexandria 23%
- Giza 18%
- Mansoura 7%
- Other 11%
<ui-pie-chart
label="Orders by city"
[rows]="cities"
name="name"
value="orders"
/>Your own limit
maxSlices and otherLabel decide how many slices, and what the last is called.
- Cairo 41%
- Alexandria 23%
- Other cities 36%
<ui-pie-chart
label="Orders by city, three slices"
[rows]="cities"
name="name"
value="orders"
[maxSlices]="3"
otherLabel="Other cities"
donut
/>One slice against the rest
highlight takes its name.
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
<ui-pie-chart
label="Orders by phone"
[rows]="channels"
name="name"
value="orders"
highlight="Phone"
donut
center="16%"
centerText="by phone"
/>Your own order
sort none keeps the order of the rows, for answers with an order of their own.
- Yes 69%
- No 20%
- Not sure 11%
<ui-pie-chart
label="Answers in their order"
[rows]="answers"
name="name"
value="orders"
sort="none"
/>Your own formats
Money, in the tooltip and the table.
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
<ui-pie-chart
label="Sales by channel"
[rows]="channels"
name="name"
value="orders"
[formatValue]="money"
nameLabel="Channel"
valueLabel="Sales"
/>Slices that switch on and off
The names become buttons; the shares are of what is left.
<ui-pie-chart
label="Orders, pick the channels"
[rows]="channels"
name="name"
value="orders"
toggle
donut
/>Small ones side by side
For a few wholes. To compare exactly, use bars.
- Web 35%
- Branch 65%
- Web 47%
- Branch 53%
- Web 57%
- Branch 43%
<ui-pie-chart
label="2026"
[rows]="channels"
name="name"
value="orders"
donut
[height]="160"
/>Open a slice
Click a slice, or press Enter on it, to use its row.
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
Nothing picked yet.
<ui-pie-chart
label="Orders, open a channel"
[rows]="channels"
name="name"
value="orders"
(rowSelect)="open($event)"
/>Without textures
Only where every slice is named beside it.
- Web 42%
- Branch 32%
- Phone 16%
- Partners 10%
<ui-pie-chart
label="Orders by channel, plain"
[rows]="channels"
name="name"
value="orders"
[patterns]="false"
labels
[height]="320"
/>Loading
Keeps its place while the rows arrive.
Loading
<ui-pie-chart
label="Orders, loading"
[rows]="none"
name="name"
value="orders"
loading
[height]="160"
/>Nothing yet
Says so in words.
No orders yet
<ui-pie-chart
label="Orders, none yet"
[rows]="none"
name="name"
value="orders"
emptyMessage="No orders yet"
[height]="160"
/>In Arabic
Slices run the other way round, with Arabic digits.
- الموقع ٤٢٪
- الفرع ٣٢٪
- الهاتف ١٦٪
- الشركاء ١٠٪
<ui-pie-chart
dir="rtl"
label="الطلبات حسب القناة"
[rows]="channelsArabic"
name="name"
value="orders"
donut
center="٩٩٢"
centerText="طلبًا"
locale="ar-EG"
/>In Arabic, named
- الموقع ٤٢٪
- الفرع ٣٢٪
- الهاتف ١٦٪
- الشركاء ١٠٪
<ui-pie-chart
dir="rtl"
label="الطلبات مع الأسماء"
[rows]="channelsArabic"
name="name"
value="orders"
labels
[height]="320"
locale="ar-EG"
/>When to use something else
| If you need | Use |
|---|---|
| More than five parts, or a close comparison | Bar chart |
| How the mix changes over time | Area chart |
| One level inside a known range | Meter |
| One number and how it changed | Stat |
Inputs
| Name | Type | Default | What it does |
|---|---|---|---|
label | string | — | Required. Names the chart, above it and for screen readers. |
rows | object[] | — | Required. The data. |
name | string | — | Required. The row field with the slice's name. |
value | string | — | Required. The row field with its number, zero or more. |
donut | boolean | false | A hole in the middle. |
half | boolean | false | Half a ring. |
labels | boolean | false | Names and shares around it, where there is room. |
center | string | — | Text in the middle of a donut. |
centerText | string | — | A second, quieter line under it. |
highlight | string | — | The name of the slice to bring forward; the others turn grey. |
sort | 'value' | 'none' | 'value' | Biggest first, or the order of the rows. |
maxSlices | number | 5 | How many slices before the rest join into one. |
otherLabel | string | — | The name of the slice that joins the rest. Defaults to Other. |
formatValue | (value: number) => string | — | Writes a value, in the tooltip and the table. |
nameLabel | string | — | Names the first column of the table. |
valueLabel | string | — | Names the second. Defaults to Value. |
shareLabel | string | — | Names the third. Defaults to Share. |
description | string | — | A line of text under the name. |
hideLabel | boolean | false | Keeps the name for screen readers only. |
height | number | 280 | In pixels. The width follows the box around it. |
toggle | boolean | false | The names become buttons that switch parts on and off. |
legend | boolean | — | Shows the names and their colours. |
table | boolean | true | The button that shows the numbers as a table. |
patterns | boolean | true | Textures over the colours, so shapes differ without colour. |
loading | boolean | false | Shows that the rows are on their way. |
emptyMessage | string | — | The words when there is nothing to draw. |
locale | string | — | For numbers and dates. Defaults to the language of the page. |
dir | 'ltr' | 'rtl' | — | Defaults to the direction of the page around it. |
[(hidden)] | string[] | [] | Keys of the parts that are off. |
(rowSelect) | row | — | The row under a click, Enter or Space. |
Keyboard and screen readers
| Keys | What they do |
|---|---|
| Tab | Reach the chart, then its next control. |
| Arrow keys | Move to the next or previous value. |
| Home or End | Go to the first or last value. |
| Enter or Space | Pin the value, and open its row if the page uses it. |
| Escape | Unpin the value. |
- A named graphic. Each value is announced as you reach it.
- The same numbers are always there as a real table.
- Slices differ by texture as well as colour, and the legend writes the share of each.