Bar chart
Compare values across categories with vertical or horizontal bars, grouped or stacked.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- 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
Stacked
Horizontal
Negative values
Large numbers and empty data
Props
BarChartProps
| Prop | Type | Default | Description |
|---|---|---|---|
title required | string | – | Accessible name and table caption. Required. |
data | ChartDatum[] | – | One series: `[{label: 'Mon', value: 3}]`. |
series | ChartSeries[] | – | Several series: `[{name: 'Visits', values: [3, 5]}]`, with `labels` for the categories. |
labels | string[] | – | Category labels for the x axis (or y axis when horizontal), one per value in `series`. |
seriesName | string | '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`. |
stacked | boolean | false | Stack series on top of each other instead of side by side. |
showValues | boolean | false | Print each value at the end of its bar (the total for stacked bars). |
height | number | – | 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.
| Attribute | Stylesheet |
|---|---|
[data-active] | chart.css |
[data-align="end"] | chart.css |
[data-align="start"] | chart.css |
[data-active] | bar-chart.css |
CSS classes
| Class | Stylesheet |
|---|---|
.chart | chart.css |
.chart-area | chart.css |
.chart-empty | chart.css |
.chart-hit | chart.css |
.chart-hits | chart.css |
.chart-legend | chart.css |
.chart-legend-detail | chart.css |
.chart-line | chart.css |
.chart-marks | chart.css |
.chart-outline | chart.css |
.chart-pat | chart.css |
.chart-pat-dot | chart.css |
.chart-plot | chart.css |
.chart-rule | chart.css |
.chart-s0 | chart.css |
.chart-s1 | chart.css |
.chart-s2 | chart.css |
.chart-s3 | chart.css |
.chart-s4 | chart.css |
.chart-svg | chart.css |
.chart-swatch | chart.css |
.chart-tick | chart.css |
.chart-tip | chart.css |
.chart-tip-row | chart.css |
.chart-tip-title | chart.css |
.chart-tip-value | chart.css |
.chart-value | chart.css |
.chart-zero | chart.css |
.chart-bar | bar-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
| Keys | Action |
|---|---|
| Arrow keys | Move between them and Escape hides the tooltip |