FunnelChart
Overview
The FunnelChart component draws the stages of a process as a funnel, each stage as wide as its value, such as the visitors who reach each step of a sign-up.
It wraps Recharts' FunnelChart and comes
from the @allxsmith/bestax-charts package, which the
Charts guide shows how to install. You give it data, the
stages from the top down, each with a name, a value and an optional color, and it draws
them with Bulma's colors, a label on each stage, a legend whose entries hide and show stages, a
tooltip in a Bulma box, and a data table screen readers read.
Import
import { FunnelChart } from '@allxsmith/bestax-charts';
Usage
Basic Usage
Each stage of data has a name and a value. The widest stage is the largest value, and each
stage narrows to the width of the next. label names the chart; it's required, because it's
what a screen reader announces.
function example() { const signups = [ { name: 'Visited', value: 12400, color: 'primary' }, { name: 'Viewed pricing', value: 5300, color: 'link' }, { name: 'Started trial', value: 2100, color: 'info' }, { name: 'Subscribed', value: 640, color: 'success' }, ]; return <FunnelChart label="Sign-up funnel" data={signups} hasLegend />; }
Labels
Each stage's value is written on it, in its Bulma color's -invert color, which stays readable
on it. labelsPosition="right" or "left" writes the values beside the funnel instead, and the
chart leaves them room on that side. showLabels={false} writes none.
function example() { const signups = [ { name: 'Visited', value: 12400, color: 'primary' }, { name: 'Viewed pricing', value: 5300, color: 'link' }, { name: 'Started trial', value: 2100, color: 'info' }, { name: 'Subscribed', value: 640, color: 'success' }, ]; return ( <Columns> <Column> <FunnelChart label="Sign-up funnel, labels right" showLabel data={signups} labelsPosition="right" height={240} /> </Column> <Column> <FunnelChart label="Sign-up funnel, no labels" showLabel data={signups} showLabels={false} height={240} /> </Column> </Columns> ); }
Reversed
isReversed draws the funnel upside down, the first stage at the bottom, so it narrows upward.
function example() { const levels = [ { name: 'Members', value: 9000, color: 'info-30' }, { name: 'Contributors', value: 2400, color: 'info-45' }, { name: 'Maintainers', value: 300, color: 'info-60' }, ]; return <FunnelChart label="Community levels" data={levels} isReversed />; }
Legend and Hidden Stages
hasLegend draws a legend below the chart, or above it with legendPosition="top". Each entry
is a toggle button that hides or shows its stage, and the stages shown close up. A hidden stage
stays in the data table. The chart keeps that state itself, starting from
defaultHiddenSeries, or you hold it with hiddenSeries and onHiddenSeriesChange.
function example() { const signups = [ { name: 'Visited', value: 12400, color: 'primary' }, { name: 'Viewed pricing', value: 5300, color: 'link' }, { name: 'Started trial', value: 2100, color: 'info' }, { name: 'Subscribed', value: 640, color: 'success' }, ]; const [hidden, setHidden] = useState(['Visited']); return ( <> <Buttons mb="3"> <Button size="small" onClick={() => setHidden([])}> Show all </Button> </Buttons> <FunnelChart label="Sign-up funnel" data={signups} 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, in the tooltip, on the
labels and in the data table, and unit is written after each one.
function example() { const pipeline = [ { name: 'Leads', value: 482000 }, { name: 'Qualified', value: 265000 }, { name: 'Proposal', value: 141000 }, { name: 'Won', value: 61000 }, ]; const dollars = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact', }); return ( <FunnelChart label="Sales pipeline" data={pipeline} valueFormatter={value => dollars.format(value)} /> ); }
Colors
A stage's 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 stage with no color takes the next of Bulma's palette colors. A label on a CSS color takes the
chart's text color, so check that it reads, or write the labels beside the funnel.
function example() { const signups = [ { name: 'Visited', value: 12400, color: 'success-30' }, { name: 'Viewed pricing', value: 5300, color: 'success-45' }, { name: 'Started trial', value: 2100, color: 'success-60' }, { name: 'Subscribed', value: 640, color: 'success-75' }, ]; return <FunnelChart label="Sign-up funnel" data={signups} />; }
Showing the Data Table
The data table is always there for screen readers: a row per stage, with its value and its share
of the first stage, the conversion a funnel shows. showTable puts it on screen too, below the chart, and showLabel
shows the chart's label above it as a caption. labels renames the table's columns, and the
share in the tooltip, for a page in another language.
function example() { const signups = [ { name: 'Visites', value: 12400 }, { name: 'Essais', value: 2100 }, { name: 'Abonnements', value: 640 }, ]; return ( <FunnelChart label="Entonnoir d'inscription" showLabel showTable data={signups} labels={{ name: 'Étape', value: 'Personnes', share: 'Part' }} height={220} /> ); }
Recharts Props and Children
Each Recharts part the chart draws takes its own props after the chart's: funnelProps for
Recharts' Funnel (here, a last stage drawn at its own width rather than to a point),
chartProps for its FunnelChart, and tooltipProps. children adds more Recharts elements,
such as <defs> for a pattern a stage names with url(#id).
function example() { const signups = [ { name: 'Visited', value: 12400, color: 'url(#visited-stripes)' }, { name: 'Started trial', value: 2100, color: 'info' }, { name: 'Subscribed', value: 640, color: 'success' }, ]; return ( <FunnelChart label="Sign-up funnel" data={signups} labelsPosition="right" funnelProps={{ lastShapeType: 'rectangle' }} hasLegend > <defs> <pattern id="visited-stripes" width="8" height="8" patternUnits="userSpaceOnUse" patternTransform="rotate(45)" > <rect width="8" height="8" fill="var(--bulma-primary)" /> <line x1="0" y1="0" x2="0" y2="8" stroke="var(--bulma-scheme-main)" strokeWidth="3" /> </pattern> </defs> </FunnelChart> ); }
Size
The chart fills its container's width and is 300 pixels tall. width and height take pixels
or a percentage of the container. A chart sized in pixels draws on the server as well; one sized
in percent draws once the browser has measured its container, and its legend and data table
render on the server either way.
<FunnelChart label="Two stages" data={[ { name: 'Visited', value: 1200 }, { name: 'Subscribed', value: 300 }, ]} width={280} height={160} />
Theming
The chart draws its own lines and text from CSS variables that fall back to Bulma's tokens, so
it follows Theme and dark mode with nothing set. A funnel uses these, and the
BarChart Theming section lists the rest:
:root {
--bulma-chart-stroke-color: var(--bulma-scheme-main); /* stage borders */
--bulma-chart-text-color: var(--bulma-text-weak); /* labels beside stages */
}
Accessibility
- The chart is a
<figure>named bylabel. The drawing is named by the same caption, and the data table's caption is the same name.descriptionis read with the name of both. - A data table of every stage renders beside the drawing, visually hidden with Bulma's
is-sr-only, so a screen reader reads the numbers the funnel shows (WAI: complex images). Each row has the stage's name as its row header, then its value and its share of the first stage. A hidden stage stays in it.showTableputs it on screen. - The drawing is a tab stop. Once it has focus, the tooltip shows the first stage, the right and
left arrow keys move through the stages in the order of
data, and Enter hides and shows the tooltip (Recharts' keyboard layer). - Each legend entry is a
buttonwitharia-pressed. A hidden stage's 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 stage it is. - The drawing writes each stage's value but not its name, so stages are told apart by their order and their color. The legend lists them in the same order, and the tooltip and the data table name them.
- A label on a stage takes its Bulma color's
-invertcolor; on a CSS color it takes the chart's text color, so check that it reads.
Related Components
PieChart: Parts of a whole as the slices of a pie or a donut.RadialBarChart: Values as bars bent into rings.BarChart: Values as bars, one group per category.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 |
|---|---|---|---|
data | readonly ChartSlice[] | — | Required. The stages, top to bottom, each a name, a value and an optional color. Each stage is as wide as its value is against the largest, and narrows to the width of the next. |
hiddenSeries | string[] | — | The names of the stages that are hidden, when you hold that state. A hidden stage leaves the drawing and the tooltip, and the stages shown close up, but it stays in the data table. |
showLabels | boolean | true | Writes each stage's value on or beside it. A label on a stage takes the -invert color of a Bulma color, which stays readable on it; on a CSS color it takes the chart's text color, so check that it reads. |
labelsPosition | 'inside' | 'right' | 'left' | 'inside' | Where the labels sit: on each stage, or beside it on the right or the left, where the chart leaves them room. |
isReversed | boolean | false | Draws the funnel upside down, narrowing upward (Recharts' reversed). |
funnelProps | Partial<Omit<FunnelProps, 'ref'>> | — | Props passed to Recharts' Funnel, after the chart's own, such as lastShapeType="rectangle" for a last stage drawn at its own width. |
chartProps | Partial<React.ComponentProps<typeof RechartsFunnelChart>> | — | Props passed to Recharts' FunnelChart, after the chart's own, such as margin. |
hasLegend | boolean | false | Shows a legend of the slices. Each entry is a toggle button that hides or shows its slice. |
defaultHiddenSeries | string[] | — | The names of the slices hidden to begin with, when the chart holds it. |
onHiddenSeriesChange | (hiddenSeries: string[]) => void | — | Called with the names of the hidden slices when the legend changes them. |
labels | { name?: string; value?: string; share?: string } | — | The data table's column headers and the tooltip's name for a share, for a page in another language. The table lists each slice's name, its value and its share: of the whole on a PieChart, of the first stage on a FunnelChart, and of max on a RadialBarChart. |
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). |
legendPosition | 'top' | 'bottom' | 'bottom' | Where the legend sits. |
ref | React.Ref<HTMLElement> | — | Ref forwarded to the root <figure>. |
... | All standard HTML attributes and Bulma helper props | — | See Helper Props |
Types:
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.