Skip to content

Series and bars ​

The xy chart (lines, areas and bars) takes a few fields that change how each series is drawn.

FieldWhereDoes
stacka serieswith stacked, bars of the same stack pile up; other stacks stand beside them
color, colorsa seriesa colour, or a function of each value (from code only)
dotsa seriesfalse hides the points, { shape, size } changes them
radiusthe chartrounded corners of the bars, in pixels (4 by default, 0 for square bars)
barWidththe chartshare of a category taken by its bars, from 0 to 1 (0.72 by default)

Stack groups ​

With stacked: true, every bar series piles into one bar per category. Give series a stack name and each name gets a pile of its own, side by side in the category: two totals and their parts, compared at once.

js
{
  type: 'xy',
  categories: ['Q1', 'Q2', 'Q3', 'Q4'],
  stacked: true,
  legend: true,
  series: [
    { name: 'Coffee (Shop)', kind: 'bar', stack: 'Shop', data: [30, 34, 28, 41] },
    { name: 'Tea (Shop)', kind: 'bar', stack: 'Shop', data: [18, 22, 25, 20] },
    { name: 'Coffee (Web)', kind: 'bar', stack: 'Web', data: [12, 19, 26, 33] },
    { name: 'Tea (Web)', kind: 'bar', stack: 'Web', data: [9, 11, 14, 18] },
  ],
}
  • Series without a stack share one pile, as before.
  • stacked: 'percent' fills 100 % with each stack on its own.
  • totals: true writes one total by stack at the end of horizontal bars, and lists them all in the tooltip (Total Shop, Total Web).
  • The keyboard says the stack with the name of the series (Coffee (Shop), Q1: 30) when there are several stacks.
  • stack does nothing on bars that are not stacked. CSV and Markdown exports keep one column for each series.

Colours from values ​

color and colors take a function as well as a text: (value, index, category) => colour. It is called for each value that is not empty; index is the place in data (zoomed or thinned charts give the same index), category the text of the category (a date as 2024-03-01).

ts
series: [{
  name: 'Result',
  kind: 'bar',
  data: [12, -4, 8, -9, 15],
  color: (value) => (value < 0 ? '#dc2626' : '#16a34a'),
}]
  • A list in colors wins over the function, point by point.
  • The legend, the line of a line series and the exports take the first colour worked out.
  • Pie and donut charts take color: (value, index, name) => colour for the slices with no colour of their own; scatter charts take color: (y, index, label) => colour.

From code only

A function cannot be written in JSON. Specs written as JSON (the editor, the MCP server, the spec attribute of <nervure-chart>, Symfony and Laravel templates) keep plain colour texts: work out the colours before, into colors.

Point shapes ​

dots takes { shape, size }: circle, square, triangle, diamond, cross, star or rect-rot (a square on its corner), and a radius in pixels (4 by default). The legend shows the shape. Shapes tell series apart without colour: on paper in grey, or for readers who do not see every colour.

js
series: [
  { name: 'North', data: [42, 48, 45, 53], dots: { shape: 'square', size: 5 } },
  { name: 'South', data: [35, 39, 44, 41], dots: { shape: 'triangle', size: 5 } },
]

The same shapes work on other charts:

  • scatter: shape on the chart, or on one point (data: [{ x, y, label, shape: 'star' }]); points painted on a canvas (render) stay circles;
  • radar: dots: true or { shape, size } on a series puts a point on each corner.

On a line cut into segments, each point takes the colour of its segment; dashed lines and dashed segments have no points, except a point alone between two gaps.

Points keep what screen readers say and what the keyboard reads: the shape is only drawn.

Bars ​

js
{ type: 'xy', radius: 0, barWidth: 0.9, categories, series }

radius rounds the end of each bar (the end of each pile when stacked); barWidth is the share of the category that the bars of one category take together, grouped or stacked.