Thirteen kinds of chart, in one crate, fusuma-charts (the charts feature of fusuma). Every one is drawn on the toolkit's own canvas, so it runs on every renderer and every host: the browser you are looking at, a desktop window, a phone, a board with a screen. Colours come from the theme, so a chart follows the design (Fusuma or Cyberdeck), light and dark, and the palette the person chose. Tap or drag on a chart to read the value under your finger; with the keyboard, Tab to a chart, then Left and Right step along the points and Escape clears the choice.
The crate has no dependency besides the toolkit. It needs std (paths are built in vectors), so a microcontroller build leaves it out, the way it leaves out vector.
Choosing a chart
| You want to show | Use | Notes |
|---|---|---|
| A value changing over time, or two series against each other | Line | Straight or smooth, a legend when there are two or more series |
| The same, with the weight of the area under it | Area | Filled under each line, one axis from zero |
| Groups compared side by side | Bars | One group per point, one bar per series, the values over the bars when there are few |
| A ranking | Ranking | Bars lying down with the names at the left and the value at the end of each |
| How numbers are spread | Histogram | Counted into bins, with the edges under the bars |
| The parts that make up a total, over several steps | Stacked bars | Negative values stack below the axis |
| A trend in a table cell or a card | Sparkline | No axes: the line, a light fill and a dot at the end |
| Shares of a whole, the total mattering | Donut | A legend and the percentage of each slice, beside the ring when there is room |
| Shares of a whole, the shares mattering | Pie | The share written in each slice; a chosen slice moves out |
| Several things compared on the same measures | Radar | A shape for each series over a spoke for each measure |
| One value against its limit | Gauge | A 270 degree arc with ticks; amber and red past the thresholds you name |
| Two numbers per point | Scatter | Both axes get round numbers |
| Intensity over two dimensions | Heat map | A grid shaded between the card and the brand colour |
Put one on the screen
A chart is plain data kept in a static. A ChartBox is the widget that draws charts from a list of them, and a view puts one in a layout with widget, naming the chart by its number.
use fusuma::charts::{Chart, ChartBox, Fixed, Kind, Series, Show};
use fusuma::{Length, widget};
static TEMP: [f32; 7] = [14.0, 16.0, 19.0, 22.0, 21.0, 18.0, 15.0];
static RAIN: [f32; 7] = [3.0, 0.0, 1.0, 0.0, 6.0, 9.0, 2.0];
static WEEK: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
static SERIES: [Series<'static>; 2] = [Series::new("Temperature", &TEMP), Series::new("Rain", &RAIN)];
static CHARTS: Fixed = Fixed(&[Chart::new(Kind::Line, &SERIES)
.labels(&WEEK)
.smooth(true)
.title("Temperature and rain this week")]);
static CHART: ChartBox = ChartBox::new(&CHARTS);
// In a view: the chart takes the width it is given and 220 pixels of height.
widget::<ChartBox, Msg>(&CHART, Show::of(0))
.width(Length::Fill(1))
.height(Length::Fixed(220))
.on_change(Msg::Picked) // the index of the point the person choseFixed is a list of charts that never changes. For data that does, implement Source, which hands the widget chart id each time it draws, and give Show a new rev when the data changed so the widget draws again:
use fusuma::charts::{Chart, Kind, Series, Source};
use std::sync::Mutex;
struct Cpu(Mutex<Vec<f32>>);
impl Source for Cpu {
fn with(&self, _id: u16, f: &mut dyn FnMut(&Chart<'_>)) {
let samples = self.0.lock().unwrap();
let series = [Series::new("CPU", &samples)];
f(&Chart::new(Kind::Sparkline, &series).smooth(true));
}
}The chart reads its numbers where they are: nothing is copied into the widget tree, and the tree stores two words for it (the number of the chart and its revision).
Line
Series are drawn left to right with a round marker on each point (up to 16 points; more draws the line alone), a grid at round numbers, names under the axis thinned out so they never run into each other, and a legend at the top when there is more than one series. .smooth(true) draws curves that pass through the points and never swing past them, so a smooth line never shows a value that is not in the data. A tap shows a rule through the point and a tooltip with every series' value.
Chart::new(Kind::Line, &SERIES).labels(&WEEK).smooth(true).unit(" °C")Area
The same lines with the area under each filled, in the series' colour at low opacity. The axis starts at zero, so the filled height means something. It is the chart for one quantity over time (energy used, requests per minute) and for two that are compared (generation against use, as on the gallery page above).
Chart::new(Kind::Area, &ENERGY).labels(&MONTHS).unit(" kWh").smooth(true)Bars
A group of bars for each point, one bar for each series, with rounded tops. The axis starts at zero. A tap or a drag picks the group; the others dim so the one you are reading stands out.
Chart::new(Kind::Bars, ®IONS).labels(&QUARTERS).unit(" k")Stacked bars
One column for each point, made of the series. The tooltip lists the parts and the total. Negative values stack below the zero line.
Chart::new(Kind::Stacked, ®IONS).labels(&QUARTERS).unit(" k")Ranking
Bars lying down, one row for each point: the name at the left, the value written past the end of the bar. It is the chart for names that are too long to sit under a column, and for anything that is read as a list from the biggest down.
Chart::new(Kind::Rows, &COMMITS).labels(&["Ada", "Grace", "Linus", "Margaret", "Dennis"])Histogram
The first series is raw numbers, not counts: they are counted into bins (.bins(10), or one more than the number of bits in how many there are, between 4 and 20) and each bin is a bar, touching its neighbours, with the count over it and the edges of the bins under it. .range(lo, hi) cuts the numbers that are counted.
Chart::new(Kind::Histogram, &LATENCY).bins(10).unit(" ms")Sparkline
No axes, no labels: a line, a faint fill under it and a dot on the last value. Meant for a place where a number sits next to its trend, so it is small by default (120 by 36 pixels) and takes whatever room the layout gives it.
Chart::new(Kind::Sparkline, &VISITS).smooth(true)Donut
The first series is the shares; .labels names them. A ring with a small gap between the slices, the total in the middle (or the chosen slice's value and name), and, when the chart is wider than it is tall, a legend beside it with the percentage of each. A tap on a slice grows it.
Chart::new(Kind::Donut, &SPENDING).labels(&["Housing", "Food", "Travel", "Fun"]).unit(" €")Pie
The same data as a donut, as a whole disc: the share is written in each slice that is big enough to hold it (6 percent and over), and a tap pushes the chosen slice out of the disc and names it in a tooltip. A donut says "how much in all"; a pie says "how it divides".
Chart::new(Kind::Pie, &LANGUAGES).labels(&["Rust", "Go", "C++", "Zig", "Other"]).unit("%")Radar
One spoke for each point, labelled with .labels, and one shape for each series, so two things can be compared on six measures at a glance. The rings are the axis's round numbers; a tap picks the spoke and the tooltip lists every series' value on it. Use it for a handful of measures that share a scale (3 to about 8), not for time.
Chart::new(Kind::Radar, &MODELS).labels(&["Speed", "Power", "Range", "Comfort", "Safety", "Value"]).range(0.0, 10.0)Gauge
One value on a 270 degree arc, from 0 to 100 unless .range(min, max) or .max(..) says otherwise, with the value in the middle, the series' name under it, ticks round the outside and the ends written at the foot of the arc. A dot marks the end of the value so a small one still shows. .thresholds(warn, danger) turns the arc amber from one value and red from the other.
Chart::new(Kind::Gauge, &LOAD).unit("%").thresholds(70.0, 90.0)
Chart::new(Kind::Gauge, &TEMP_NOW).range(10.0, 30.0).unit(" °C")Scatter
Each series holds pairs, x0, y0, x1, y1, .... Both axes get round numbers and a grid. A tap chooses the nearest dot within reach (about 28 pixels, a fingertip) and the tooltip says both numbers.
static GROWTH: [f32; 8] = [1.0, 2.0, 3.0, 5.0, 6.0, 4.0, 8.0, 9.0];
Chart::new(Kind::Scatter, &[Series::new("Plants", &GROWTH)])Heat map
A row for each series, a column for each point, every cell shaded from the card's colour towards the brand colour by its value (or between .range(min, max)). Empty cells keep a visible background so the grid reads as a grid. The value is written in each cell that is big enough, and a tap names the cell.
Chart::new(Kind::Heat, &ACTIVITY).labels(&WEEK)How it is built
Three plain pieces, none of which needs the others:
- The model (
Chart,Series,Kind): borrowed slices, labels, a range, a unit.value_axis()widens the data to round numbers (steps of 1, 2 or 5 times a power of ten), starting from zero for the kinds that need it. - The geometry (
plot,select_at,slices): where the plot area, the points and the bars are in a chart of a given size. The drawing and the touch both use it, so the thing under your finger is the thing that is highlighted. - The drawing (
draw,Ink): a function over the toolkit'sCanvas. A host that has its own canvas calls it directly; the widget calls it with the theme's colours.
All text is 12 pixels, so what is measured to leave room for the labels is what is drawn.
What it does not do yet
- No animation when a chart appears or its data changes.
- Dense lines are thinned for a chip: where the renderer can only cross a row 64 times, an area with more than 40 points is filled through the highest point of each stretch. A phone or a desktop draws all of them.
- No text-mode form: on a terminal or a serial console a chart is not drawn (it is still described to a screen reader and the keyboard works).
- Linear axes only: no logarithmic scale, no second axis, no zoom or pan.
- Nothing streams in the gallery: a
Sourcecan, but the page uses fixed data.