chart #
Charts in SVG and HTML for OCaml, drawn when the page is built.
chart draws bar charts, column charts, line plots, Sankey diagrams and
treemaps as static HTML and SVG. The page needs no JavaScript to be read.
Every chart comes with an HTML table of the same numbers, so a reader who
cannot see the drawing still gets every value. A chart too wide for a phone
also comes in a second layout for a narrow screen.
An optional script, built from the chart.brr library, adds tooltips and
highlights a path through a Sankey diagram. It can also give the charts of
a page new numbers without drawing them again from scratch.
Installation #
chart needs OCaml 5.1 or later. The package is not in the public
opam-repository; add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install chart
Usage #
A bar chart #
Chart_html builds the HTML of a chart as a Tw_html.t of the tw
package. A chart takes a Figure.t per number, which is the number and the
text the page prints for it. Labels are printed as given, in the language
of the page:
open Chart_html
let spending =
let fig text v = Figure.v ~text v in
bars ~caption:"Spending by function, 2024"
[
bar ~slot:1 ~name:"Old age" (fig "€396bn" 396.);
bar ~slot:1 ~name:"Health" (fig "€262bn" 262.);
]
slot is the number of one of the palette's series colours. A bar with no
slot is drawn in a neutral colour, for the row the others are compared
with. Bars start at zero. When a figure is negative, its bar is drawn to
the left of a line at zero.
The page must hold Palette.css in a <style> element, and tooltip once
in its body. The few words a chart writes by itself, and the way it writes
a percentage, come from a Page.t: Page.en by default, or Page.fr for
French.
Layouts without HTML #
The chart library computes positions and draws nothing. Chart.Scale
picks round ticks for an axis, and says where a bar starts and how long it
is, in per cent of its row:
# Chart.Scale.v ~lo:3. ~hi:47. 5;;
- : Chart.Scale.t =
{Chart.Scale.lo = 0.; hi = 50.; ticks = [0.; 10.; 20.; 30.; 40.; 50.]}
# Chart.Scale.extent [ 30.; -10. ];;
- : float * float = (-10., 30.)
# Chart.Scale.bar ~lo:(-10.) ~hi:30. 30.;;
- : float * float = (25., 75.)
Chart.Treemap.layout cuts a box into one cell per item, with areas in
proportion to their values:
# Chart.Treemap.layout ~width:100. ~height:50. ~value:snd [ ("a", 3.); ("b", 1.) ]
|> List.map (fun (c : _ Chart.Treemap.cell) -> (fst c.item, c.w, c.h));;
- : (string * float * float) list = [("a", 75., 50.); ("b", 25., 50.)]
Figures that link to their source #
A caller that keeps a source for each number can make the figure a link to
it. node is the figure as text or as a table cell, and at is the
attributes of the bar, dot or line that stands for it:
let sourced ~id ~text v =
Figure.v ~text
~at:(fun () -> [ Tw_html.At.v "data-claim" id ])
~node:(fun ~titled:_ ->
Tw_html.a ~at:[ Tw_html.At.href ("#" ^ id) ] [ Tw_html.txt text ])
v
at and node are called once for each place the figure appears, so the
caller can also record there which figures a page shows.
Charts that change in the browser #
A page that computes in the browser, such as a calculator, keeps the charts
it was built with. Its script gives each chart new figures, and chart.brr
writes them into the elements already in the page. The script links chart
and chart.brr only, so neither chart.html nor the CSS engine of tw
ends up in the JavaScript.
The page is built with a row for everything it may show later. A figure the
page has no value for yet is absent. Its row is in the page but hidden,
and takes no part in the scale:
let result =
bars ~caption:"Tax due"
[
bar ~slot:1 ~name:"Income tax" (Figure.v ~text:"€1,200" 1200.);
bar ~slot:1 ~name:"Surtax" (Figure.v ~absent:true ~text:"" 0.);
]
In the browser, Chart_brr.Bars.set el rows gives new values to the rows
of the chart in the element el, in order. Each row is Some of a name
and a figure, or None to hide the row. With
Bars.set el [ Some (Bars.row ~name:"Income tax" (Figure.v ~text:"€1,450" 1450.)); Some (Bars.row ~name:"Surtax" (Figure.v ~text:"€90" 90.)) ],
both rows are shown.
Both bars are scaled to the longer one, and both figures are written in
the rows and in the table. A name already in its row is kept, with its
links. Chart_brr.Paired.set and Chart_brr.Tile.set do the same for
paired bars and for a single large number.
A line plot is drawn again from its layout, which the same code computes
when the page is built and in the browser. Chart_brr.Lines.set el layout
replaces the lines of the plot, those of its layout for a narrow screen,
and the rows of its table.
Colours #
The series colours 1 to 5 stay distinct for readers with protanopia,
deuteranopia and tritanopia, in the light and in the dark theme. Every
series colour has a contrast of at least 3:1 against the page and the panel
in both themes.
A high-contrast theme puts text, every series and the grid at 4.5:1 or more
against a white or a black page. It applies when the root element has
data-theme="contrast", or when the system asks for it with
prefers-contrast: more.
A line plot also tells its series apart without colour. Each series has its
own dash (solid, dashed or dotted) and its own marker at the end of the
line (a circle, a square, a diamond, a triangle or a triangle pointing
down), and the key shows both.
A second line with the same slot is dashed and dotted, with a hollow
marker. A line with no slot is solid, in the neutral colour, and ends on a
bar.
API #
The package has three libraries:
chart(lib/chart.mli) computes layouts.
Chart.Sankeyplaces a flow diagram in columns and finds a path through
it,Chart.Treemapis the squarified treemap,Chart.Scalegives an
axis round ticks,Chart.Linesplaces a line plot, andChart.Text
measures a label.chart.html(lib/html/chart_html.mli)
renders charts. It hasbars,paired(two bars per row on one scale),
columns(one column per week or month),parts(one bar cut into
shares),dumbbell(a before and an after per row),lines,tile
(one large number) andChart_html.Flow(a Sankey diagram).
Chart_html.Paletteholds the colours, as CSS custom properties in a
light, a dark and a high-contrast theme.chart.brr(lib/brr/chart_brr.mli) runs in
the browser.Chart_brr.listen ()shows tooltips, and highlights a path
through a flow diagram on hover and keeps it on click.Chart_brr.Bars,
Paired,TileandLinesgive a chart new figures.
Related work #
- D3 and d3-sankey
lay charts out in the browser, from data the page must ship, and move
nodes by iteration. Here the layout runs once when the page is built, and
nodes sit in the columns and the order the author gives. - Observable Plot and
Vega-Lite describe a chart as a
grammar of marks and need a script to draw it. This package has a fixed
set of charts. - The treemap is the layout of Bruls, Huizing and van Wijk, "Squarified
Treemaps" (2000).
Licence #
ISC. See LICENSE.md.