Skip to content
Okaapiأوكابي
Framework for the code
Theme
Okaapi on GitHub

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.

Use it in Angular
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.

Terminal
pnpm add @tanstack/charts@1.5.0

Pie

The biggest slice starts at the top. Press Tab and use the arrow keys to read each one.

Orders by channel
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
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

Orders by channel, donut
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
<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.

Orders by channel, with the total
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
<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.

Would you order again?
  • Yes 69%
  • No 20%
  • Not sure 11%
Angular
<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.

Orders by channel, named
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
<ui-pie-chart
  label="Orders by channel, named"
  [rows]="channels"
  name="name"
  value="orders"
  labels
  [height]="320"
/>

Names around a donut

Orders by channel, named donut
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
<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.

Orders by city
  • Cairo 41%
  • Alexandria 23%
  • Giza 18%
  • Mansoura 7%
  • Other 11%
Angular
<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.

Orders by city, three slices
  • Cairo 41%
  • Alexandria 23%
  • Other cities 36%
Angular
<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.

Orders by phone
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
<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.

Answers in their order
  • Yes 69%
  • No 20%
  • Not sure 11%
Angular
<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.

Sales by channel
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
<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.

Orders, pick the channels
Angular
<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.

2024
  • Web 35%
  • Branch 65%
2025
  • Web 47%
  • Branch 53%
2026
  • Web 57%
  • Branch 43%
Angular
<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.

Orders, open a channel
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%

Nothing picked yet.

Angular
<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.

Orders by channel, plain
  • Web 42%
  • Branch 32%
  • Phone 16%
  • Partners 10%
Angular
<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.

Orders, loading

Loading

Angular
<ui-pie-chart
  label="Orders, loading"
  [rows]="none"
  name="name"
  value="orders"
  loading
  [height]="160"
/>

Nothing yet

Says so in words.

Orders, none yet

No orders yet

Angular
<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.

الطلبات حسب القناة
  • الموقع ٤٢٪؜
  • الفرع ٣٢٪؜
  • الهاتف ١٦٪؜
  • الشركاء ١٠٪؜
Angular
<ui-pie-chart
  dir="rtl"
  label="الطلبات حسب القناة"
  [rows]="channelsArabic"
  name="name"
  value="orders"
  donut
  center="٩٩٢"
  centerText="طلبًا"
  locale="ar-EG"
/>

In Arabic, named

الطلبات مع الأسماء
  • الموقع ٤٢٪؜
  • الفرع ٣٢٪؜
  • الهاتف ١٦٪؜
  • الشركاء ١٠٪؜
Angular
<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 needUse
More than five parts, or a close comparisonBar chart
How the mix changes over timeArea chart
One level inside a known rangeMeter
One number and how it changedStat

Inputs

NameTypeDefaultWhat it does
labelstring — Required. Names the chart, above it and for screen readers.
rowsobject[] — Required. The data.
namestring — Required. The row field with the slice's name.
valuestring — Required. The row field with its number, zero or more.
donutbooleanfalseA hole in the middle.
halfbooleanfalseHalf a ring.
labelsbooleanfalseNames and shares around it, where there is room.
centerstring — Text in the middle of a donut.
centerTextstring — A second, quieter line under it.
highlightstring — The name of the slice to bring forward; the others turn grey.
sort'value' | 'none''value'Biggest first, or the order of the rows.
maxSlicesnumber5How many slices before the rest join into one.
otherLabelstring — 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.
nameLabelstring — Names the first column of the table.
valueLabelstring — Names the second. Defaults to Value.
shareLabelstring — Names the third. Defaults to Share.
descriptionstring — A line of text under the name.
hideLabelbooleanfalseKeeps the name for screen readers only.
heightnumber280In pixels. The width follows the box around it.
togglebooleanfalseThe names become buttons that switch parts on and off.
legendboolean — Shows the names and their colours.
tablebooleantrueThe button that shows the numbers as a table.
patternsbooleantrueTextures over the colours, so shapes differ without colour.
loadingbooleanfalseShows that the rows are on their way.
emptyMessagestring — The words when there is nothing to draw.
localestring — 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

KeysWhat 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.