Playground

Bar chart

Compare values across categories with vertical or horizontal bars, grouped or stacked.

  • Built on native elements
  • The chart is role=img with the required title as its name.
  • A visually hidden table lists every value, one row per category, for screen readers.
  • One focusable stop per category with a spoken label; Arrow keys move between them and Escape hides the tooltip.
  • Styles in one file: chart.css

Import

import '@monoline/css';
import {BarChart} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add bar-chart

Usage

import {BarChart} from '@monoline/react';

const week = [{label: 'Mon', value: 12}, {label: 'Tue', value: 19}, {label: 'Wed', value: 9}, {label: 'Thu', value: 24}, {label: 'Fri', value: 17}, {label: 'Sat', value: 6}, {label: 'Sun', value: 4}];

<BarChart title="Signups per weekday" data={week} showValues />

Examples

Vertical with value labels

Grouped series

Series differ by fill pattern as well as colour.

Stacked

Horizontal

Negative values

Large numbers and empty data

Props

BarChartProps

PropTypeDefaultDescription
title requiredstring–Accessible name and table caption. Required.
dataChartDatum[]–One series: `[{label: 'Mon', value: 3}]`.
seriesChartSeries[]–Several series: `[{name: 'Visits', values: [3, 5]}]`, with `labels` for the categories.
labelsstring[]–Category labels for the x axis (or y axis when horizontal), one per value in `series`.
seriesNamestring'Value'Name of the single series when `data` is used. Default "Value".
orientation"vertical" | "horizontal"'vertical'`vertical` draws columns, `horizontal` draws rows (better for long labels). Default `vertical`.
stackedbooleanfalseStack series on top of each other instead of side by side.
showValuesbooleanfalsePrint each value at the end of its bar (the total for stacked bars).
heightnumber–Chart height in px. Horizontal charts size themselves to the number of rows when omitted. Default 280.
valueFormat((value: number) => string)–Format a value for labels, tooltips and the table.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[data-active]chart.css
[data-align="end"]chart.css
[data-align="start"]chart.css
[data-active]bar-chart.css

CSS classes

ClassStylesheet
.chartchart.css
.chart-areachart.css
.chart-emptychart.css
.chart-hitchart.css
.chart-hitschart.css
.chart-legendchart.css
.chart-legend-detailchart.css
.chart-linechart.css
.chart-markschart.css
.chart-outlinechart.css
.chart-patchart.css
.chart-pat-dotchart.css
.chart-plotchart.css
.chart-rulechart.css
.chart-s0chart.css
.chart-s1chart.css
.chart-s2chart.css
.chart-s3chart.css
.chart-s4chart.css
.chart-svgchart.css
.chart-swatchchart.css
.chart-tickchart.css
.chart-tipchart.css
.chart-tip-rowchart.css
.chart-tip-titlechart.css
.chart-tip-valuechart.css
.chart-valuechart.css
.chart-zerochart.css
.chart-barbar-chart.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.chart {
  border-color: var(--accent);
}
.chart[data-active] {
  background: var(--wash);
}

Accessibility

  • The chart is role=img with the required title as its name.
  • A visually hidden table lists every value, one row per category, for screen readers.
  • One focusable stop per category with a spoken label; Arrow keys move between them and Escape hides the tooltip.
  • The value tooltip opens on hover and on keyboard focus.
  • Series differ by fill pattern as well as colour, and the legend names them.
  • No entrance animation under prefers-reduced-motion.

Keyboard

KeysAction
Arrow keysMove between them and Escape hides the tooltip