# The chart language

> Bar, line, area, pie and doughnut charts from text: the kind, settings, rows, and how they're drawn

The language of `chart` code blocks, exactly. For a gentler introduction with examples, see [Charts](../using-kribble/charts.md).

Version 1.

A chart is a code block whose language is `chart` ([document.md](../specification/the-document-format.md)): its text says what kind of chart it is, a few settings, and the data, one row per line. Renderers that draw charts draw it; the others show the text, which reads well as it is. People edit a chart in the editor's chart panel, which writes the same text (see Writing); agents write the text.

```chart
bar
title: Signups by plan
series: Free, Team, Business

Jan: 820, 240, 60
Feb: 910, 265, 72
Mar: 1040, 301, 75
```

This page is the language. The user guide (`docs/Using Kribble/Charts.md`) explains it with examples; the two say the same thing. The reference implementations are `@kribble/chart` (TypeScript: parser, writer, renderer) and `backend/internal/chart` (Go: parser and renderer), checked against the same fixtures.

## Lines

The text is read line by line, as in the diagram language ([diagram-language.md](../specification/the-diagram-language.md)): lines are separated by `\n` (a `\r` before it is ignored); columns count UTF-16 code units from 0, lines from 1; a `#` at the start of a line or after a space or tab, followed by a space, a tab or the end of the line, starts a comment unless it's inside double quotes; lines with nothing but spaces, tabs and a comment are ignored. Indentation doesn't matter.

## The kind

The first line that isn't ignored names the kind of chart, and options after it, separated by spaces. Case doesn't matter.

| Kind | Draws |
| --- | --- |
| `bar` | A bar per row and series, going up from zero; a group of bars per row. |
| `line` | A line per series, a point per row. |
| `area` | A line per series with the area under it filled. |
| `pie` | A slice per row, the first series' values. |
| `doughnut` | A pie with a hole, and the total in it. |

`donut` is `doughnut`, a plural (`bars`) is the kind, and the word `chart` after the kind is ignored (`pie chart`).

| Option | For |  |
| --- | --- | --- |
| `stacked` | `bar`, `area` | The series of a row are stacked: one bar (or band) per row, its segments the series. |
| `horizontal` | `bar` | Bars go right; rows go down the page. For long labels and rankings. |

An unknown kind is a problem, and the chart is a bar chart; the options after it are still read. If the first line has a colon and isn't a kind, it's a problem ("a chart starts with its kind") and the line is read as a setting or a row.

## Settings

A line `key: value` whose key is one of these is a setting, wherever it is after the kind line. A later one replaces an earlier one.

| Setting | Value |  |
| --- | --- | --- |
| `title` | text | Drawn above the chart. |
| `x` | text | The title of the category axis (the rows): under it, or above the labels of a horizontal bar chart. |
| `y` | text | The title of the value axis: above it, or under a horizontal bar chart. |
| `unit` | text | Written with every value: `$`, `€`, `£`, `¥` and `₹` before the number, `%` right after it, anything else after a space (`42 min`). |
| `min`, `max` | number | Where the value axis starts and ends (it is then rounded out to its ticks). Bars and areas always include zero. Empty: automatic. |
| `series` | names | The series, one per value column, separated by commas, each with an optional style (`2025 [orange]`). |

A text value in double quotes (`title: "Q1 # final"`) loses its quotes; that's how a value holds `#`. Pies and doughnuts have no axes: `x`, `y`, `min` and `max` don't change them.

## Rows

Every other line is a row: a label, a colon and its values.

```
Q1: 120, 135
"Sales, EMEA" [green]: 40
10:00: 12
```

- **The label** is the text before the line's last colon, with runs of spaces and tabs collapsed, or text in double quotes at the start of the line (a quoted label can hold commas, colons and brackets, not a double quote). A label that is a setting's key must be quoted (`"title": 5`). A row with no label, or no colon, is a problem and is left out.
- **A style** in square brackets may follow the label (see Styles).
- **The values** are separated by commas, the first for the first series, and so on. A value left empty (`Q3: 1, , 3`, or `Thu:`) is missing: no bar, a gap in a line. Nothing after the colon is a row with no values.

A value is a decimal number: an optional `+` or `-`, digits with an optional fraction (`12`, `0.5`, `.5`, `-3.25`). It may be decorated with a currency sign before the digits (`$`, `€`, `£`, `¥`, `₹`; `-$5`) or a `%` after them: the decoration isn't part of the number, and when the text has no `unit`, the first decoration becomes the unit. Anything else (`1,200`, `1.2k`, `n/a`) is a problem and the value is missing.

**Series.** With a `series` line, a chart has that many series, and a row's values beyond them are a problem (and not drawn). Without one, it has as many as its longest row has values, unnamed. A chart draws at most 8 series; more is a problem and the rest aren't drawn. A pie or doughnut draws only the first series: a second is a problem. A slice can't be negative (a problem; it isn't drawn).

## Styles

Square brackets after a series name or a row label hold a colour: one of `blue`, `orange`, `teal`, `yellow`, `pink`, `green`, `purple`, `red`, `gray` (`grey`), or a hex colour (`#2a78d6`, `#28d`). Words are separated by spaces or commas; the last colour wins. An unknown word is a problem, naming the nearest colour when one is close enough (as in the diagram language).

A row's colour is for a slice of a pie or doughnut, or a bar of a bar chart with one series (to pick one out); on other rows it's a problem.

## Problems

Parsing never fails. What can't be read is a **problem**: its line, a column range and a message. The rest of the chart is still read and drawn, and editors show the problem on its line. The fixtures fix the messages.

## The model

What the text reads as, which the fixtures hold as JSON:

| Field |  |
| --- | --- |
| `kind` | `bar`, `line`, `area`, `pie` or `doughnut`. |
| `stacked`, `horizontal` | The options. |
| `title`, `x`, `y`, `unit` | The settings, `""` when not set (`unit` from a decoration if not set). |
| `min`, `max` | Numbers, or `null`. |
| `series` | `{ name, color }` per series; `""` for no name or the automatic colour. |
| `rows` | `{ label, color, values }`: values as written, `null` where missing. |
| `problems` | `{ line, from, to, message }`, in order. |

## Drawing

How the reference renderers draw a chart, as SVG. Other renderers may draw it their own way; these are the choices a chart's reader can count on.

- **Width.** A chart is laid out for the width it's shown at: 720 pixels (a note's column) unless asked otherwise, at least 280. Text stays 12 pixels; a narrower chart gets a new layout, not a smaller picture. Narrower than 480, a pie's legend goes under it; a horizontal bar chart's labels take at most a third of the width; axis labels that would touch show every other one (or every k-th). Editors lay a chart out for its block's width; static pages carry it laid out for 720, 480 and 360 and show the one that fits (`kc-at-<width>`, container queries in `editor-content.css`).
- **Colours.** Series take, in order, blue, orange, teal, yellow, pink, green, purple and red, unless styled; a pie's slices take them by row. The light and dark values of each are in `@kribble/chart`'s `model.ts` (`COLORS`). The order is chosen so that neighbouring series stay apart for colour-blind readers.
- **Value axis.** From the data's smallest to largest value (stacked: the sums), with zero in it, unless `min` or `max` say otherwise (bars and areas keep zero); rounded out to about five ticks of 1, 2, 2.5 or 5 times a power of ten. Tick labels are compacted alike: `0 · 5K · 10K`.
- **Values** are written for people: `1,284`, `12.5K`, `3.2M`, `1.5B`, with the unit.
- **Legend.** When two or more series are drawn, above the plot. A pie's legend is beside it: label, value and share (no value when the unit is `%`).
- **Bars** are at most 24 pixels thick, their far end rounded; a bar chart with one series and at most 12 rows (or any horizontal one) writes each value at its bar's end.
- **Lines** are 2 pixels wide, with a dot on each point when there are at most 12 rows, else on the last point only. Missing values break the line (a stacked area counts them as zero).
- **Pies** start at twelve o'clock and go clockwise. More than 8 slices: the 7 largest stay, in their order, and the rest join into one gray slice, "Other", at the end. A doughnut shows its total in the middle, unless the unit is `%`.
- **Labels** on the category axis are cut to fit with "…", or only every k-th is shown when they don't fit at all.
- **Hovering.** Every bar and slice, and every row of a line or area chart, has a `<title>` saying its values; pages that show charts turn those into tooltips.

The SVG's colours are attributes for a light page, with classes a page's stylesheet restyles: `kc-ink`, `kc-ink2`, `kc-muted` (text), `kc-grid`, `kc-axis` (lines), `kc-f-<colour>` and `kc-s-<colour>` (fills and strokes of named colours) and `kc-bg` (the gaps around slices and dots, which take the page's background). `editor-content.css` has the dark theme.

## Writing

The chart panel writes the text in canonical form: the kind line, then `title`, `x`, `y`, `unit`, `min`, `max` and `series` (each only when set), an empty line, and the rows. Numbers are written in the shortest form that reads back the same, with no decoration. A label or name is quoted when it would otherwise read as something else (a comma, colon or bracket in it, a `#`, spaces at its ends, a setting's key). Comments aren't kept.

## Markdown

A chart is a fenced code block whose info string is `chart`, holding the text, like any code block. Markdown readers that don't know charts show the text.

## Conformance

fixtures/charts has, for each case, `<name>.txt` (the text), `<name>.json` (the model, in canonical JSON: object keys sorted, indented by one space), `<name>.svg` (the drawing, 720 wide) and `<name>.360.svg` (360 wide).

- A **parser** conforms when it gives the model, byte for byte, for every fixture.
- A **renderer** conforms when it draws the same SVG byte for byte, at both widths. Both reference implementations do. Others conform to the parser and draw as they like.
