ComposedChart
Overview
The ComposedChart component draws several series against the same categories, each as bars, a line or an area, such as sales as bars with their trend as a line.
It wraps Recharts' ComposedChart and
comes from the @allxsmith/bestax-charts package, which the
Charts guide shows how to install. You give it rows of data,
the dataKey that holds each row's category, and the series to draw, each with the type it's
drawn as, and it draws them with Bulma's colors, a legend whose entries hide and show series, a
tooltip in a Bulma box, and a data table screen readers read.
Import
import { ComposedChart } from '@allxsmith/bestax-charts';
Usage
Basic Usage
Each row of data is a category, and each series names the key of the rows that holds its
values and its type: bar, line (the default) or area. label names the chart; it's
required, because it's what a screen reader announces.
function example() { const sales = [ { month: 'Jan', sales: 1200, target: 1100 }, { month: 'Feb', sales: 1900, target: 1300 }, { month: 'Mar', sales: 1400, target: 1500 }, { month: 'Apr', sales: 1700, target: 1600 }, { month: 'May', sales: 2100, target: 1800 }, ]; return ( <ComposedChart label="Sales against target" data={sales} dataKey="month" series={[ { name: 'sales', label: 'Sales', color: 'primary', type: 'bar' }, { name: 'target', label: 'Target', color: 'danger', type: 'line' }, ]} hasLegend /> ); }
Bars, Lines and Areas
Any mix of the three draws on one chart. Areas lie behind the bars, and lines and their dots on top of both, whatever order the series come in.
function example() { const sales = [ { month: 'Jan', sales: 1200, returns: 140, average: 1050 }, { month: 'Feb', sales: 1900, returns: 210, average: 1380 }, { month: 'Mar', sales: 1400, returns: 160, average: 1500 }, { month: 'Apr', sales: 1700, returns: 190, average: 1670 }, { month: 'May', sales: 2100, returns: 230, average: 1730 }, ]; return ( <ComposedChart label="Sales, returns and their average" data={sales} dataKey="month" series={[ { name: 'sales', label: 'Sales', color: 'primary', type: 'bar' }, { name: 'returns', label: 'Returns', color: 'warning', type: 'area' }, { name: 'average', label: 'Moving average', color: 'link' }, ]} hasLegend /> ); }
Stacking
Bars that share a stackId stack into one bar per category, and so do areas. Lines don't stack.
function example() { const orders = [ { month: 'Jan', online: 820, store: 380, target: 1100 }, { month: 'Feb', online: 1240, store: 660, target: 1300 }, { month: 'Mar', online: 910, store: 490, target: 1500 }, { month: 'Apr', online: 1080, store: 620, target: 1600 }, ]; return ( <ComposedChart label="Orders by channel against target" data={orders} dataKey="month" series={[ { name: 'online', label: 'Online', color: 'primary', type: 'bar', stackId: 'orders', }, { name: 'store', label: 'In store', color: 'info', type: 'bar', stackId: 'orders', }, { name: 'target', label: 'Target', color: 'danger' }, ]} showLabels hasLegend /> ); }
Lines and Areas
curveType sets how the lines and the areas' edges run between their points, as on
LineChart, and a series' own curveType wins. A series'
strokeDasharray dashes its line. hasDots={false} leaves the dots off the lines, strokeWidth
sets how thick the lines and edges are, and fillOpacity how opaque the areas are. Lines and
areas run across a gap in the data unless connectNulls={false} breaks them there.
function example() { const sales = [ { month: 'Jan', sales: 1200, returns: 140, target: 1100 }, { month: 'Feb', sales: 1900, returns: 210, target: 1300 }, { month: 'Mar', sales: 1400, returns: 160, target: 1500 }, { month: 'Apr', sales: 1700, returns: 190, target: 1600 }, ]; return ( <ComposedChart label="Sales, returns and target" data={sales} dataKey="month" series={[ { name: 'returns', label: 'Returns', color: 'warning', type: 'area' }, { name: 'sales', label: 'Sales', color: 'primary', type: 'bar' }, { name: 'target', label: 'Target', color: 'danger', curveType: 'stepAfter', strokeDasharray: '6 4', }, ]} hasDots={false} strokeWidth={3} fillOpacity={0.4} hasLegend /> ); }
Two Value Axes
hasRightYAxis draws a second value axis on the right, for the series whose yAxisId is
right, which suits a count drawn as bars beside a rate drawn as a line.
function example() { const shop = [ { month: 'Jan', orders: 320, conversion: 2.1 }, { month: 'Feb', orders: 410, conversion: 2.6 }, { month: 'Mar', orders: 380, conversion: 2.4 }, { month: 'Apr', orders: 290, conversion: 1.8 }, { month: 'May', orders: 460, conversion: 3.1 }, ]; return ( <ComposedChart label="Orders and conversion" data={shop} dataKey="month" series={[ { name: 'orders', label: 'Orders', color: 'primary', type: 'bar' }, { name: 'conversion', label: 'Conversion', color: 'success', yAxisId: 'right', }, ]} hasRightYAxis yAxisLabel="Orders" rightYAxisLabel="Conversion (%)" hasLegend /> ); }
Vertical
orientation="vertical" lays the categories down the side, with the bars and lines running
across.
function example() { const teams = [ { team: 'Design', hours: 320, budget: 300 }, { team: 'Engineering', hours: 760, budget: 800 }, { team: 'Marketing', hours: 410, budget: 350 }, { team: 'Support', hours: 290, budget: 300 }, ]; return ( <ComposedChart label="Hours against budget" orientation="vertical" grid="vertical" data={teams} dataKey="team" series={[ { name: 'hours', label: 'Hours', color: 'link', type: 'bar' }, { name: 'budget', label: 'Budget', color: 'danger' }, ]} hasLegend height={280} yAxisProps={{ width: 100 }} /> ); }
Axes, Grid and Labels
xAxisLabel and yAxisLabel title the axes, and hasXAxis and hasYAxis turn them off.
grid picks the grid lines: horizontal (the default), vertical, both or none.
showLabels writes each value beside its bar or point, inside a stacked bar.
function example() { const visits = [ { day: 'Mon', visits: 320, average: 300 }, { day: 'Tue', visits: 410, average: 340 }, { day: 'Wed', visits: 380, average: 370 }, { day: 'Thu', visits: 450, average: 390 }, ]; return ( <ComposedChart label="Visits per day" data={visits} dataKey="day" series={[ { name: 'visits', label: 'Visits', color: 'success', type: 'bar' }, { name: 'average', label: 'Average', color: 'link' }, ]} xAxisLabel="Day" yAxisLabel="Visits" grid="both" showLabels /> ); }
Reference Lines
referenceLines draws lines across the chart at a value (y) or a category (x), each with an
optional label and color.
function example() { const sales = [ { month: 'Jan', sales: 1200, average: 1150 }, { month: 'Feb', sales: 1900, average: 1400 }, { month: 'Mar', sales: 1400, average: 1500 }, { month: 'Apr', sales: 1700, average: 1580 }, ]; return ( <ComposedChart label="Sales and their average" data={sales} dataKey="month" series={[ { name: 'sales', label: 'Sales', color: 'primary', type: 'bar' }, { name: 'average', label: 'Average', color: 'link' }, ]} referenceLines={[ { y: 2000, label: 'Stretch goal', color: 'success' }, { x: 'Mar', label: 'Price change' }, ]} /> ); }
Legend and Hidden Series
hasLegend draws a legend below the chart, or above it with legendPosition="top". Each entry
is a toggle button that hides or shows its series. The chart keeps that state itself, starting
from defaultHiddenSeries, or you hold it with hiddenSeries and onHiddenSeriesChange.
function example() { const sales = [ { month: 'Jan', sales: 1200, returns: 140, target: 1100 }, { month: 'Feb', sales: 1900, returns: 210, target: 1300 }, { month: 'Mar', sales: 1400, returns: 160, target: 1500 }, ]; const [hidden, setHidden] = useState(['returns']); return ( <> <Buttons mb="3"> <Button size="small" onClick={() => setHidden([])}> Show all </Button> </Buttons> <ComposedChart label="Sales against target" data={sales} dataKey="month" series={[ { name: 'sales', label: 'Sales', color: 'primary', type: 'bar' }, { name: 'returns', label: 'Returns', color: 'warning', type: 'area' }, { name: 'target', label: 'Target', color: 'danger' }, ]} hasLegend legendPosition="top" hiddenSeries={hidden} onHiddenSeriesChange={setHidden} /> </> ); }
Formatting Values
Values are written as useNumberFormat writes a number, in the
LocaleProvider's locale. valueFormatter writes them your way instead, on the axes, in the
tooltip, on the labels and in the data table, and unit is written after each one.
function example() { const revenue = [ { quarter: 'Q1', revenue: 48200, target: 45000 }, { quarter: 'Q2', revenue: 53900, target: 50000 }, { quarter: 'Q3', revenue: 61400, target: 56000 }, ]; const dollars = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, }); return ( <ComposedChart label="Revenue against target" data={revenue} dataKey="quarter" series={[ { name: 'revenue', label: 'Revenue', color: 'success', type: 'bar' }, { name: 'target', label: 'Target', color: 'danger' }, ]} valueFormatter={value => dollars.format(value)} yAxisProps={{ width: 80 }} /> ); }
Colors
A series' color takes a Bulma color name (primary, info, grey-dark and the rest), a shade
of one of Bulma's six palette colors (primary-30, info-dark), or any CSS color. Bulma names
draw with Bulma's own CSS variables, so they follow Theme and dark mode.
A series with no color takes the next of Bulma's palette colors.
function example() { const weather = [ { month: 'Jan', rain: 78, temperature: 4 }, { month: 'Feb', rain: 62, temperature: 6 }, { month: 'Mar', rain: 55, temperature: 10 }, { month: 'Apr', rain: 48, temperature: 14 }, ]; return ( <ComposedChart label="Rain and temperature" data={weather} dataKey="month" series={[ { name: 'rain', label: 'Rain (mm)', color: 'info-60', type: 'bar' }, { name: 'temperature', label: 'Temperature (°C)', color: '#e76f51', yAxisId: 'right', }, ]} hasRightYAxis hasLegend height={240} /> ); }
Showing the Data Table
The data table is always there for screen readers. showTable puts it on screen too, below the
chart, and showLabel shows the chart's label above it as a caption.
function example() { const sales = [ { month: 'Jan', sales: 1200, target: 1100 }, { month: 'Feb', sales: 1900, target: 1300 }, ]; return ( <ComposedChart label="Sales against target" showLabel showTable data={sales} dataKey="month" series={[ { name: 'sales', label: 'Sales', type: 'bar' }, { name: 'target', label: 'Target' }, ]} height={200} /> ); }
Recharts Props and Children
Each Recharts part the chart draws takes its own props after the chart's: barProps for every
Bar (here, rounded tops), lineProps for every Line, areaProps for every Area,
chartProps for Recharts' ComposedChart, and xAxisProps, yAxisProps, rightYAxisProps and
tooltipProps. children adds more Recharts elements, such as a ReferenceArea or a Brush.
function example() { const sales = [ { month: 'Jan', sales: 1200, target: 1100 }, { month: 'Feb', sales: 1900, target: 1300 }, { month: 'Mar', sales: 1400, target: 1500 }, ]; return ( <ComposedChart label="Sales against target" data={sales} dataKey="month" series={[ { name: 'sales', label: 'Sales', color: 'primary', type: 'bar' }, { name: 'target', label: 'Target', color: 'danger' }, ]} barProps={{ radius: [6, 6, 0, 0] }} lineProps={{ strokeLinecap: 'round' }} chartProps={{ barCategoryGap: '30%' }} /> ); }
Size
The chart fills its container's width and is 300 pixels tall. height changes that, and
width and height take pixels or a percentage of the container. A chart sized in percent draws
once the browser has measured its container, and its legend and data table render on the server
either way.
<ComposedChart label="Two months" data={[ { month: 'Jan', sales: 1200, target: 1100 }, { month: 'Feb', sales: 1900, target: 1300 }, ]} dataKey="month" series={[ { name: 'sales', label: 'Sales', type: 'bar' }, { name: 'target', label: 'Target' }, ]} width={320} height={180} />
Theming
The chart draws its grid, axes, text and reference lines from the same --bulma-chart-* CSS
variables as every chart, with the same fallbacks to Bulma's tokens; see
BarChart's Theming section. The focused category is marked with a line
in the axis color, bars or not, and the ring around each dot takes --bulma-chart-stroke-color,
which falls back to --bulma-scheme-main, the page's background.
Accessibility
- The chart is a
<figure>named bylabel, and the drawing is named by the same caption.descriptiondescribes both. - A data table of every category and series renders beside the drawing, visually hidden with
Bulma's
is-sr-only, so a screen reader reads the numbers the bars, lines and areas show (WAI: complex images). Its first column is a row header, and a hidden series stays in it.showTableputs it on screen. - The drawing is a tab stop. Once it has focus, the left and right arrow keys move through the categories; the tooltip shows each one's values and a dot marks each line's and area's point there (Recharts' keyboard layer).
- Each legend entry is a
buttonwitharia-pressed. A hidden series' swatch is drawn hollow and its label greyed, so its state isn't told by color alone. - The swatches in the legend and tooltip are
aria-hidden; each entry's text says which series it is. - Series of different types are told apart by their shape as well as their color, but two series
of the same type only by color. When that matters, dash a line with its
strokeDasharray, write the values withshowLabels, or keep the series few.
Related Components
BarChart: Rows and series drawn as bars alone.LineChart: Rows and series drawn as lines alone.AreaChart: Rows and series drawn as areas alone, stacked or split at zero.Table: The Bulma table the chart's data table is drawn with.LocaleProvider: The locale the chart writes its numbers in.- Helper Props: Bulma helper props for spacing, color, etc.
Additional Resources
Props
| Prop | Type | Default | Description |
|---|---|---|---|
series | ComposedChartSeries[] | — | Required. The series to draw, in order, each as bars, a line or an area. |
curveType | 'linear' | 'monotone' | 'natural' | 'step' | 'stepBefore' | 'stepAfter' | 'bump' | 'monotone' | How every line and area's edge runs between its points, unless its series names its own. |
hasDots | boolean | true | Draws a dot on each point of the lines. The point under the pointer or keyboard focus is marked with a larger dot either way. |
connectNulls | boolean | true | Draws the lines and areas across a gap in the data, a row whose value is missing or null, rather than breaking them there. |
strokeWidth | number | 2 | How thick each line and area's edge is, in pixels. |
fillOpacity | number | 0.2 | How opaque each area's fill is, from 0 to 1. |
barProps | Partial<Omit<BarProps, 'ref'>> | — | Props passed to every Recharts Bar, after the chart's own, such as radius. |
lineProps | Partial<Omit<LineProps, 'ref'>> | — | Props passed to every Recharts Line, after the chart's own, such as dot. |
areaProps | Partial<Omit<AreaProps<ChartRow, unknown>, 'ref'>> | — | Props passed to every Recharts Area, after the chart's own, such as baseValue. |
chartProps | Partial<React.ComponentProps<typeof RechartsComposedChart>> | — | Props passed to Recharts' ComposedChart, after the chart's own, such as barGap. |
data | readonly ChartRow[] | — | Required. The rows to draw: one per category, with a key per series. |
dataKey | string | — | Required. The key of data that holds each row's category, such as month. |
orientation | 'horizontal' | 'vertical' | 'horizontal' | Which way the chart runs. vertical puts the categories down the side and the values across. |
grid | 'horizontal' | 'vertical' | 'both' | 'none' | 'horizontal' | Which grid lines to draw. |
gridProps | Partial<CartesianGridProps> | — | Props passed to Recharts' CartesianGrid, after the chart's own. |
hasXAxis | boolean | true | Draws the x axis. |
hasYAxis | boolean | true | Draws the y axis. |
xAxisLabel | string | — | A title for the x axis. |
yAxisLabel | string | — | A title for the y axis. |
xAxisProps | Partial<XAxisProps> | — | Props passed to Recharts' XAxis, after the chart's own. |
yAxisProps | Partial<YAxisProps> | — | Props passed to Recharts' YAxis, after the chart's own. |
hasRightYAxis | boolean | false | Draws a second value axis on the right, for the series whose yAxisId is right. Ignored with orientation="vertical". |
rightYAxisLabel | string | — | A title for the right y axis. |
rightYAxisProps | Partial<YAxisProps> | — | Props passed to Recharts' YAxis for the right axis, after the chart's own. |
referenceLines | { x?: string | number; y?: number; label?: string; color?: ChartColor }[] | — | Lines drawn across the chart at given values, such as a target. |
showLabels | boolean | false | Writes each value on the chart beside its mark. |
tooltipProps | Partial<TooltipProps> | — | Props passed to Recharts' Tooltip, after the chart's own. |
className | string | — | Additional CSS classes to apply. |
label | string | — | Required. What the chart shows, such as "Monthly sales". It names the chart for assistive tech, and captions its data table. Shown above the chart with showLabel. |
description | string | — | A sentence or two on what the chart says, such as the trend it shows. A screen reader reads it with the chart's name. Not shown on screen. |
showLabel | boolean | false | Shows label above the chart as its caption. |
showTable | boolean | false | Shows the chart's data table below it. The table is always there for screen readers, which read the numbers from it; this puts it on screen as well. |
hasTooltip | boolean | true | Shows a tooltip with the values under the pointer, or under the keyboard focus as the arrow keys move through the chart. |
valueFormatter | ChartValueFormatter | — | Writes each value on the axes, in the tooltip, on the labels and in the data table. Without one, values are written as useNumberFormat writes a number, in the LocaleProvider's locale. |
unit | string | — | Written after every value, such as % or kg. |
width | ChartSize | '100%' | The chart's width, in pixels or as a percentage of its container. A percentage makes the chart follow its container's size as the browser measures it. Recharts draws in the browser either way: a server render holds the figure, its caption, legend and data table, and the drawing appears once the page hydrates. |
height | ChartSize | 300 | The chart's height, in pixels or as a percentage of its container, which then needs a height of its own. |
children | React.ReactNode | — | More Recharts elements to draw inside the chart, such as a Brush, a ReferenceArea, or <defs> for a pattern a color names with url(#id). |
hasLegend | boolean | false | Shows a legend. Each entry is a toggle button that hides or shows its series. |
legendPosition | 'top' | 'bottom' | 'bottom' | Where the legend sits. |
hiddenSeries | string[] | — | The names of the series that are hidden, when you hold that state. |
defaultHiddenSeries | string[] | — | The names of the series hidden to begin with, when the chart holds it. |
onHiddenSeriesChange | (hiddenSeries: string[]) => void | — | Called with the names of the hidden series when the legend changes them. |
ref | React.Ref<HTMLElement> | — | Ref forwarded to the root <figure>. |
... | All standard HTML attributes and Bulma helper props | — | See Helper Props |
Types:
ChartColor: Bulma color |`${(typeof paletteColors)[number]}-${(typeof validColorShades)[number]}`|(string & {})— A color for a series, a slice or a mark. A Bulma color name such asprimaryorgrey-light, a shade of one of Bulma's six palette colors such asprimary-60orinfo-dark, or any CSS color, such as#3e8ed0,var(--brand)orurl(#stripes)for a pattern the chart's children define. A Bulma name draws with Bulma's own CSS variable, so it followsThemeand dark mode.ChartRow:Record<string, any>— One row of a chart'sdata: an object whose keys the chart'sdataKeyand series name.ChartSize:number|`${number}%`— A chart's width or height: pixels, or a percentage of its container.ChartValueFormatter:(value: number) => string— Writes a value for the axes, the tooltip, the labels and the data table.ComposedChartSeries: One series of a composed chart, drawn as bars, a line or an area. Its fields:type:'bar'|'line'|'area'— How the series is drawn: asbars, alineor anarea. Default'line'.curveType:'linear'|'monotone'|'natural'|'step'|'stepBefore'|'stepAfter'|'bump'— How a line or an area's edge runs between its points. Defaults to the chart'scurveType. Bars ignore it.strokeDasharray:string— Draws a line or an area's edge dashed, as an SVGstroke-dasharraysuch as5 5: the length of each dash, then of each gap. Bars ignore it.stackId:string— Bars, or areas, with the samestackIdstack one on another. Lines ignore it.yAxisId:'left'|'right'— Which value axis it's drawn against.rightneedshasRightYAxis; without it, and in a vertical chart, the series is drawn against the left axis. Default'left'.name:string— Required. The key ofdatathat holds this series' values, and the namehiddenSeriesholds. No two series share one.label:string— What the legend, the tooltip and the data table call it. Defaults toname.color:ChartColor— Its color. Without one, it takes the next of Bulma's palette colors.