Line Charts for Bricks
Two elements for showing numbers as lines: Line Chart for a full chart with axes, and Sparkline for a tiny trend that sits in a stat tile, a table cell or beside a number. Both are driven from data, animate in when scrolled to, and work with the keyboard.
Where the data comes from
- Text: paste CSV or JSON into the Data box. Dynamic data works there, so a custom field, a query value or a shortcode can supply the numbers.
- A web address: the chart fetches CSV or JSON in the visitor’s browser. The address must be on your site or allow cross-origin requests. Set Check for new data every to refresh it, and the chart updates without animating again.
Data formats
- CSV: the first row holds the series names and the first column holds the labels. Commas, semicolons and tabs are detected, and quoted fields work. Currency signs and percent signs in numbers are ignored.
- JSON list: a list of objects, such as
[{"month":"Jan","sales":10,"costs":4}]. The label column is found by name (month, date, label, x and similar) or as the first text column. Every other numeric key becomes a series. - JSON with labels:
{"labels":[...],"datasets":[{"label":"Sales","data":[...]}]}, or{"labels":[...],"series":{"Sales":[...]}}. - Just numbers:
3, 5, 4, 7is one series, which is all a sparkline needs. - If the list is nested inside the JSON, give its place in Where the list is inside the JSON, for example
data.items. - Empty cells and non-numbers leave a gap in the line.
Line Chart
- Line shape: straight, smooth (no overshoot) or steps.
- Light fill under the lines, dots on the last point, every point or none, and optional gridlines.
- Vertical axis: automatic nice numbers, optionally starting at zero, or set the lowest and highest. Values can have a prefix or suffix (£, %) and set decimals. Large numbers shorten to 1K, 1.5M on the axis.
- Names at the line ends are shown when the lines end far enough apart, and left out automatically when they would collide. The legend always carries the names for two or more series, and clicking a name hides or shows that line.
- Readout: hover, touch or use the left and right arrow keys (Home and End jump to the ends) to see every series’ value for a label. Screen readers hear the same values.
- Data table: a table of the numbers sits under the chart, for people who prefer it, for screen readers and for copying.
- Labels are evenly spaced, so dates are shown as the text you give.
Sparkline
- Style: line, line with a fill, or bars. The latest value gets a dot (or the last bar is the full colour).
- Mark lowest and highest with dots.
- Colour by direction compares the last value with the first and uses your rising and falling colours. Choose the number beside it set to Change with arrow so the direction is also shown as an arrow and a sign, never just a colour.
- Number beside it: the latest value, the change (for example ▲ +12.4%) or both.
- Hover to read any point. Screen readers get a summary: the number of values, first, last, lowest and highest.
- Use it anywhere: in a stat tile, inside a table, or in a query loop where each item’s numbers come from a custom field.
Animation
- Lines draw in from the left, with a stagger between series. The fills, dots and labels fade in. Bars grow up from the baseline. Choose draw, fade or none, and the duration.
- The animation starts when the chart is scrolled into view, or straight away if you turn that off.
- With reduced-motion turned on there is no animation.
Colours and style
- Series colours come from a built-in set of eight colours chosen to stay apart for colour-blind readers, with a darker set for dark mode. Series keep their colour when you hide another. To use your own, list them in order in Series colours.
- Lines are thin, gridlines are quiet, and text always uses the text colours, never the series colour. Dots have a thin ring in the background colour, so set Background behind the chart to match your section.
- Height, line thickness, text size and colours are all in the Style tab.
Good to know
- The chart is drawn by JavaScript from the data. Without JavaScript there is nothing to see, so keep important numbers in the page text as well.
- One vertical axis only. Two measures on very different scales are better as two charts.
- Charts redraw when their container changes size, so they work inside tabs, columns and drawers once visible.
- For developers,
window.LCFB.refresh()sets up charts added to the page later.