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

Chapter 12 Charts

Bar chart

Amounts to compare, as lengths from zero. Side by side, stacked, or as shares of a whole, upright or sideways. Each series has a texture as well as a colour, and one button shows the same numbers as a table.

Use it in Angular
import { BarChartComponent } 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

One series

Bars start at zero, so their lengths compare. Press Tab and use the arrow keys to read each one.

Orders by month
Angular
const months = [
  { month: 'Jan', orders: 42 },
  { month: 'Feb', orders: 58 },
  { month: 'Mar', orders: 76 },
];
const orders = [{ key: 'orders', label: 'Orders' }];

<ui-bar-chart
  label="Orders by month"
  [rows]="months"
  x="month"
  [series]="orders"
/>

Side by side

Two or three series per place. More than that is hard to read: stack them.

Orders by channel
  • Web
  • Branch
Angular
const two = [
  { key: 'web', label: 'Web' },
  { key: 'branch', label: 'Branch' },
];

<ui-bar-chart
  label="Orders by channel"
  [rows]="months"
  x="month"
  [series]="two"
/>

Stacked

Parts that add up. The first part sits on the base line, so only it compares exactly.

Orders by channel, stacked
  • Web
  • Branch
  • Phone
Angular
const channels = [
  { key: 'web', label: 'Web' },
  { key: 'branch', label: 'Branch' },
  { key: 'phone', label: 'Phone' },
];

<ui-bar-chart
  label="Orders by channel, stacked"
  [rows]="months"
  x="month"
  [series]="channels"
  layout="stacked"
/>

Shares of a whole

Every bar is 100%. The tooltip gives the number and the share.

Share of orders by channel
  • Web
  • Branch
  • Phone
Angular
<ui-bar-chart
  label="Share of orders by channel"
  [rows]="months"
  x="month"
  [series]="channels"
  layout="percent"
/>

Sideways

For long names: they read in full.

Why people order again
Angular
const reasons = [{ reason: 'Clear price', count: 97 }];
const count = [{ key: 'count', label: 'Answers' }];

<ui-bar-chart
  label="Why people order again"
  [rows]="reasons"
  x="reason"
  [series]="count"
  horizontal
/>

Sorted

Biggest first, or smallest first.

Why people order again, smallest first
Angular
<ui-bar-chart
  label="Why people order again, smallest first"
  [rows]="reasons"
  x="reason"
  [series]="count"
  horizontal
  sort="ascending"
  valueLabels
/>

Values on the bars

When the exact number matters and there are few bars.

Orders, with values
Angular
<ui-bar-chart
  label="Orders, with values"
  [rows]="months"
  x="month"
  [series]="orders"
  valueLabels
/>

Totals on the stacks

The total of each stack, above it.

Orders, with totals
  • Web
  • Branch
  • Phone
Angular
<ui-bar-chart
  label="Orders, with totals"
  [rows]="months"
  x="month"
  [series]="channels"
  layout="stacked"
  valueLabels
/>

Sideways and stacked

Orders by channel, sideways
  • Web
  • Branch
  • Phone
Angular
<ui-bar-chart
  label="Orders by channel, sideways"
  [rows]="months"
  x="month"
  [series]="channels"
  layout="stacked"
  horizontal
  valueLabels
  [height]="320"
/>

Sideways shares

Share by channel, sideways
  • Web
  • Branch
  • Phone
Angular
<ui-bar-chart
  label="Share by channel, sideways"
  [rows]="months"
  x="month"
  [series]="channels"
  layout="percent"
  horizontal
  [height]="320"
/>

A target

A level to reach, named on the chart.

Orders against the target
Angular
const target = [{ value: 80, label: 'Target 80' }];

<ui-bar-chart
  label="Orders against the target"
  [rows]="months"
  x="month"
  [series]="orders"
  [references]="target"
/>

One bar against the rest

highlight takes the place to bring forward.

Orders, May
Angular
<ui-bar-chart
  label="Orders, May"
  [rows]="months"
  x="month"
  [series]="orders"
  highlight="May"
  valueLabels
/>

Below zero

Bars hang from the zero line.

Profit by month
Angular
<ui-bar-chart
  label="Profit by month"
  [rows]="profit"
  x="month"
  [series]="profitSeries"
  [formatY]="compact"
  valueLabels
/>

Dates

Written in the language of the page.

Orders by day
Angular
<ui-bar-chart
  label="Orders by day"
  [rows]="weeks"
  x="date"
  [series]="orders"
/>

Parts that switch on and off

The names become buttons.

Orders, pick the channels
Angular
<ui-bar-chart
  label="Orders, pick the channels"
  [rows]="months"
  x="month"
  [series]="channels"
  layout="stacked"
  toggle
/>

Named axes

Orders, with named axes
Angular
<ui-bar-chart
  label="Orders, with named axes"
  [rows]="months"
  x="month"
  [series]="orders"
  xLabel="Month"
  yLabel="Orders"
/>

Without textures

Only where something else tells the bars apart, such as a name on each.

Orders by channel, plain
  • Web
  • Branch
Angular
<ui-bar-chart
  label="Orders by channel, plain"
  [rows]="months"
  x="month"
  [series]="two"
  [patterns]="false"
/>

Open a value

Click a bar, or press Enter on it, to use its row.

Orders, open a month

Nothing picked yet.

Angular
<ui-bar-chart
  label="Orders, open a month"
  [rows]="months"
  x="month"
  [series]="orders"
  (rowSelect)="open($event)"
/>

Loading

Keeps its place while the rows arrive.

Orders, loading

Loading

Angular
<ui-bar-chart
  label="Orders, loading"
  [rows]="none"
  x="month"
  [series]="orders"
  loading
  [height]="160"
/>

Nothing yet

Says so in words.

Orders, none yet

No orders yet

Angular
<ui-bar-chart
  label="Orders, none yet"
  [rows]="none"
  x="month"
  [series]="orders"
  emptyMessage="No orders yet"
  [height]="160"
/>

In Arabic

The first month is on the right, and so is the first bar of each group.

الطلبات حسب القناة
  • الموقع
  • الفرع
  • الهاتف
Angular
<ui-bar-chart
  dir="rtl"
  label="الطلبات حسب القناة"
  [rows]="monthsArabic"
  x="month"
  [series]="channelsArabic"
  locale="ar-EG"
/>

In Arabic, sideways

Bars grow from the right.

الطلبات كل شهر
Angular
<ui-bar-chart
  dir="rtl"
  label="الطلبات كل شهر"
  [rows]="monthsArabic"
  x="month"
  [series]="ordersArabic"
  locale="ar-EG"
  horizontal
  valueLabels
/>

When to use something else

If you needUse
How a value changes over timeLine chart
Parts of one whole, up to fivePie chart
Exact values to read and sortData table
One level inside a known rangeMeter

Inputs

NameTypeDefaultWhat it does
labelstring — Required. Names the chart, above it and for screen readers.
rowsobject[] — Required. The data.
xstring — Required. The row field with the place: text, a number or a Date (read in UTC).
seriesChartSeries[] — Required. The values: key, label, and optionally color (1 to 4 or muted).
layout'grouped' | 'stacked' | 'percent''grouped'Side by side, on top of each other, or as shares of a whole.
horizontalbooleanfalseBars run along the reading direction.
sort'ascending' | 'descending' — By the total of each row.
valueLabelsbooleanfalseWrites each value, or the total of each stack. Not for bars side by side.
highlightstring — What to bring forward; the rest turns grey.
referencesChartReference[][]Levels to compare with: value and label.
xLabelstring — Names the axis of places, and the first table column.
yLabelstring — Names the axis of values.
yMinnumber — Where the values start. Defaults to zero or lower.
yMaxnumber — Where the values end.
formatX(x) => string — Writes a place.
formatY(y: number) => string — Writes a value, in the axis, the tooltip and the table.
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.
  • Bars differ by texture as well as colour, and a gap separates the parts of a stack.