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.
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.
pnpm add @tanstack/charts@1.5.0One series
Bars start at zero, so their lengths compare. Press Tab and use the arrow keys to read each one.
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.
- Web
- Branch
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.
- Web
- Branch
- Phone
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.
- Web
- Branch
- Phone
<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.
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.
<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.
<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.
- Web
- Branch
- Phone
<ui-bar-chart
label="Orders, with totals"
[rows]="months"
x="month"
[series]="channels"
layout="stacked"
valueLabels
/>Sideways and stacked
- Web
- Branch
- Phone
<ui-bar-chart
label="Orders by channel, sideways"
[rows]="months"
x="month"
[series]="channels"
layout="stacked"
horizontal
valueLabels
[height]="320"
/>Sideways shares
- Web
- Branch
- Phone
<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.
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.
<ui-bar-chart
label="Orders, May"
[rows]="months"
x="month"
[series]="orders"
highlight="May"
valueLabels
/>Below zero
Bars hang from the zero line.
<ui-bar-chart
label="Profit by month"
[rows]="profit"
x="month"
[series]="profitSeries"
[formatY]="compact"
valueLabels
/>Dates
Written in the language of the page.
<ui-bar-chart
label="Orders by day"
[rows]="weeks"
x="date"
[series]="orders"
/>Parts that switch on and off
The names become buttons.
<ui-bar-chart
label="Orders, pick the channels"
[rows]="months"
x="month"
[series]="channels"
layout="stacked"
toggle
/>Named axes
<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.
- Web
- Branch
<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.
Nothing picked yet.
<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.
Loading
<ui-bar-chart
label="Orders, loading"
[rows]="none"
x="month"
[series]="orders"
loading
[height]="160"
/>Nothing yet
Says so in words.
No orders yet
<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.
- الموقع
- الفرع
- الهاتف
<ui-bar-chart
dir="rtl"
label="الطلبات حسب القناة"
[rows]="monthsArabic"
x="month"
[series]="channelsArabic"
locale="ar-EG"
/>In Arabic, sideways
Bars grow from the right.
<ui-bar-chart
dir="rtl"
label="الطلبات كل شهر"
[rows]="monthsArabic"
x="month"
[series]="ordersArabic"
locale="ar-EG"
horizontal
valueLabels
/>When to use something else
| If you need | Use |
|---|---|
| How a value changes over time | Line chart |
| Parts of one whole, up to five | Pie chart |
| Exact values to read and sort | Data table |
| One level inside a known range | Meter |
Inputs
| Name | Type | Default | What it does |
|---|---|---|---|
label | string | — | Required. Names the chart, above it and for screen readers. |
rows | object[] | — | Required. The data. |
x | string | — | Required. The row field with the place: text, a number or a Date (read in UTC). |
series | ChartSeries[] | — | 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. |
horizontal | boolean | false | Bars run along the reading direction. |
sort | 'ascending' | 'descending' | — | By the total of each row. |
valueLabels | boolean | false | Writes each value, or the total of each stack. Not for bars side by side. |
highlight | string | — | What to bring forward; the rest turns grey. |
references | ChartReference[] | [] | Levels to compare with: value and label. |
xLabel | string | — | Names the axis of places, and the first table column. |
yLabel | string | — | Names the axis of values. |
yMin | number | — | Where the values start. Defaults to zero or lower. |
yMax | number | — | 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. |
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.
- Bars differ by texture as well as colour, and a gap separates the parts of a stack.