Stacked bars in groups
Several stacks side by side in each category. Good for two totals and their parts, compared category by category.
The smallest code that works. change the code, then press Run
In your framework
import { Chart } from 'nervure/react';
const spec = {
type: 'xy',
categories: ['Q1', 'Q2', 'Q3', 'Q4'],
legend: true,
stacked: true,
series: [
{ name: 'Coffee (Shop)', kind: 'bar', stack: 'Shop', color: '#7c5cff', data: [28, 24, 16, 27] },
{ name: 'Tea (Shop)', kind: 'bar', stack: 'Shop', color: '#2a9d8f', data: [14, 14, 31, 29] },
{ name: 'Coffee (Web)', kind: 'bar', stack: 'Web', color: '#b9a8ff', data: [17, 18, 17, 17] },
{ name: 'Tea (Web)', kind: 'bar', stack: 'Web', color: '#8fd3c9', data: [43, 11, 32, 10] },
],
};
export default function Example() {
return <Chart spec={spec} style={{ height: 340 }} />;
}Or import this chart alone: what each import weighs.
Options
Beside the fields shared by every chart (title, format, heading, export…), type xy takes:
interface XYSpec extends Base {
type: 'xy';
/** Names, or dates: Date objects, ISO texts (`2024-03-01`), or timestamps with `xScale: 'time'`. */
categories: (string | number | Date)[];
/**
* `time`: categories are dates, placed by time (uneven gaps drawn to scale), with ticks by span.
* Chosen alone when every category is a Date or an ISO date; `category` keeps them evenly spaced.
*/
xScale?: 'category' | 'time';
series: Series[];
axes?: Axis[];
horizontal?: boolean;
/** `percent`: each category fills the axis, its bars as shares of 100 %. Series with a `stack` pile up by group. */
stacked?: boolean | 'percent';
/** Rounded corners of the bars, in pixels: 4 by default, 0 for square bars. */
radius?: number;
/** Share of a category taken by its bars, from 0 to 1: 0.72 by default. */
barWidth?: number;
/** Horizontal bars: first category at the top (default) or at the bottom. */
reverse?: boolean;
totals?: boolean;
legend?: boolean | LegendOptions;
labelWidth?: number;
grid?: boolean | 'dashed';
/** Title under the horizontal axis. */
xTitle?: string;
/** Angle of the category labels (vertical charts), for example -45. */
labelAngle?: number;
lines?: ReferenceLine[];
annotations?: Annotation[];
/** Shaded zones behind the series: a period, a forecast, a range of values. */
zones?: Zone[];
/** Drag on the chart to look closer, double click to come back (vertical charts). */
zoom?: boolean;
/** No axis, no legend, no margin: a sparkline. */
bare?: boolean;
/**
* Long series: keeps the lowest and highest points of each stretch, so peaks stay.
* `true`: about one point every 3 pixels; a number: the most points kept. Zooming in brings the detail back.
*/
thin?: boolean | number;
}
interface Series {
name: string;
interface Axis extends AxisScale {
side?: 'left' | 'right';
/** A line across the chart: a target, an average, a date. */
interface ReferenceLine {
value?: number;
/** A note pinned on the chart. `at` is a category (xy) or an x value (scatter). */
interface Annotation {
at: string | number | Date;
/** A shaded zone: a stretch of categories (`from`, `to`), or a range of values (`min`, `max`). */
interface Zone {
/** A category, or a date on a time axis. */
from?: string | number | Date;Style it
- Colours:
theme.palette,chart.setColor(name, colour), or--nervure-palettein your CSS. More - Marks:
[data-series="name"], parts:[data-part="axes"],legend,series. What you change is kept in exports. - Movement:
animation: falsedraws at once. More - Files:
chart.download('png'),svg,csv. More