Charts
bestax-charts draws the charts of Recharts with Bulma's colors,
in a package of its own, @allxsmith/bestax-charts. Each chart takes its data as plain rows,
follows Theme and dark mode, writes numbers in your LocaleProvider's locale, and comes with a
legend whose entries hide and show series, a tooltip in a Bulma box, and a data table that screen
readers read.
function example() { const sales = [ { month: 'Jan', online: 1200, retail: 900 }, { month: 'Feb', online: 1900, retail: 1200 }, { month: 'Mar', online: 400, retail: 1000 }, { month: 'Apr', online: 1000, retail: 200 }, ]; return ( <BarChart label="Monthly sales" data={sales} dataKey="month" series={[ { name: 'online', label: 'Online', color: 'primary' }, { name: 'retail', label: 'Retail', color: 'info' }, ]} hasLegend /> ); }
Installation
The charts need bestax-bulma and its stylesheet, as any bestax app has, plus Recharts and
react-is, which the charts take from your app rather than bundling their own copies:
- pnpm
- npm
- yarn
- bun
pnpm add @allxsmith/bestax-charts recharts react-is
npm install @allxsmith/bestax-charts recharts react-is
yarn add @allxsmith/bestax-charts recharts react-is
bun add @allxsmith/bestax-charts recharts react-is
Install react-is at the same major as your react: Recharts reads React elements through it,
and a react-is of another major misreads them. The package has no stylesheet to import; it draws
with Bulma's classes and CSS variables.
import { BarChart } from '@allxsmith/bestax-charts';
Data and Series
A chart doesn't take a Recharts element per series. It takes its data in the shape that suits its kind:
- Rows and series, for the charts that plot measures against categories, such as
BarChart. Each row ofdatais a category,dataKeynames the key that holds it, andserieslists the keys to draw, each with alabeland acolor.
Every chart takes a label, which names it. It's required, because it's what a screen reader
announces, and showLabel puts it on screen as a caption.
Colors and Theming
A color is a Bulma color name (primary, info, grey-dark), a shade of one of Bulma's six
palette colors (primary-30, danger-dark), or any CSS color. Bulma names draw with Bulma's own
CSS variables, so a chart follows Theme and dark mode with
nothing more. A series or slice with no color takes the next of Bulma's palette colors.
function example() { const scores = [ { team: 'Red', low: 30, mid: 50, high: 20 }, { team: 'Blue', low: 20, mid: 45, high: 35 }, { team: 'Green', low: 25, mid: 40, high: 35 }, ]; return ( <BarChart label="Score bands by team" type="stacked" data={scores} dataKey="team" series={[ { name: 'low', label: 'Low', color: 'link-30' }, { name: 'mid', label: 'Mid', color: 'link-50' }, { name: 'high', label: 'High', color: 'link-70' }, ]} hasLegend height={220} /> ); }
The grid, axes and text read --bulma-chart-* CSS variables that fall back to Bulma's tokens.
BarChart's Theming section lists them.
Numbers
Values are written as bestax-bulma's useNumberFormat
writes a number, in the LocaleProvider's locale, on the axes, in the tooltip, on the labels and
in the data table. valueFormatter writes them your way instead, and unit is written after each
one.
function example() { const revenue = [ { quarter: 'Q1', revenue: 48200.5 }, { quarter: 'Q2', revenue: 53900.25 }, { quarter: 'Q3', revenue: 61400.75 }, ]; return ( <LocaleProvider locale="de-DE"> <BarChart label="Umsatz pro Quartal" data={revenue} dataKey="quarter" series={[{ name: 'revenue', label: 'Umsatz', color: 'success' }]} unit=" €" showLabels showTable /> </LocaleProvider> ); }
Accessibility
A chart is a picture of numbers, so bestax-charts makes sure the numbers are there too:
- Each chart is a
<figure>named by itslabel, anddescriptionadds a sentence on what it shows, read with the name. - A data table of every value renders beside the drawing, visually hidden with Bulma's
is-sr-only, so a screen reader reads what the drawing shows.showTableputs it on screen. - Recharts' keyboard layer stays on: the drawing is a tab stop, and the arrow keys move the tooltip through the data. Each chart's API page says what the keys reach for that chart.
- The legend's entries are toggle buttons (
aria-pressed) that hide and show series, and a hidden one's entry shows a hollow swatch and greyed text, so its state isn't told by color alone.
Colors alone tell series apart in a drawing. Where that matters, write the values on the marks
with showLabels, keep the legend, or point readers to the table.
Sizing and Server Rendering
A chart fills its container's width and is 300 pixels tall unless you set width and height,
which take pixels or a percentage of the container. A chart sized in percent follows its
container as the browser measures it.
Recharts draws in the browser. A server render, or a page a static site generator writes, holds each chart's figure, caption, legend and data table, so the numbers are in the HTML from the start, and the drawing appears once the page hydrates.
Going Further with Recharts
Each chart passes props through to the Recharts parts it draws, after its own: chartProps to the
Recharts chart itself, and props named for each part, such as xAxisProps, tooltipProps and
barProps. Anything you pass as children is drawn inside the Recharts chart, such as a Brush,
a ReferenceArea, or SVG <defs> for a pattern a color names with url(#id).
The Charts
BarChart
Values as bars, one group per category: side by side, stacked, as shares of a whole or as a waterfall. View full documentation.
function example() { const cashFlow = [ { item: 'Opening', amount: 1000, standalone: true }, { item: 'Sales', amount: 600 }, { item: 'Payroll', amount: -700, color: 'danger' }, { item: 'Closing', amount: 900, standalone: true, color: 'success' }, ]; return ( <BarChart label="Cash flow" type="waterfall" data={cashFlow} dataKey="item" series={[{ name: 'amount', label: 'Amount', color: 'info' }]} showLabels height={240} /> ); }