Skip to main content

RadialBarChart

Overview​

The RadialBarChart component draws values as bars bent into rings, one ring per value, each filled as far as its value goes toward a full ring.

It wraps Recharts' RadialBarChart and comes from the @allxsmith/bestax-charts package, which the Charts guide shows how to install. You give it data, the bars from the innermost ring out, each with a name, a value and an optional color, and it draws them with Bulma's colors, a track behind each bar, a legend whose entries hide and show bars, a tooltip in a Bulma box, and a data table screen readers read.


Import​

import { RadialBarChart } from '@allxsmith/bestax-charts';

Usage​

Basic Usage​

Each bar of data has a name and a value, and runs clockwise from the top, as RingProgress draws its ring. max is the value a full ring stands for. label names the chart; it's required, because it's what a screen reader announces.

function example() {
  const goals = [
    { name: 'Move', value: 420, color: 'danger' },
    { name: 'Exercise', value: 310, color: 'success' },
    { name: 'Stand', value: 240, color: 'info' },
  ];

  return (
    <RadialBarChart label="Daily goals" data={goals} max={500} hasLegend />
  );
}

The Scale​

Without max, the largest value is a full ring and the others are drawn against it. Hiding a bar leaves that scale as it was, so the other bars keep their length. A value past max draws a full ring, and the tooltip and the data table still give the value itself.

function example() {
  const regions = [
    { name: 'North', value: 820, color: 'primary' },
    { name: 'South', value: 540, color: 'link' },
    { name: 'West', value: 310, color: 'info' },
  ];

  return (
    <Columns>
      <Column>
        <RadialBarChart
          label="Sales by region, against the largest"
          showLabel
          data={regions}
          height={240}
        />
      </Column>
      <Column>
        <RadialBarChart
          label="Sales by region, against a target of 1,000"
          showLabel
          data={regions}
          max={1000}
          height={240}
        />
      </Column>
    </Columns>
  );
}

Rings and Angles​

barSize sets each ring's width in pixels; without it, the rings share the room between the chart's middle and its edge. hasBackground={false} leaves out the track behind each bar. startAngle and endAngle set where a bar starts and where a full one ends, in degrees counter-clockwise from three o'clock, so startAngle={180} and endAngle={0} draw half rings.

function example() {
  const goals = [
    { name: 'Move', value: 420, color: 'danger' },
    { name: 'Exercise', value: 310, color: 'success' },
    { name: 'Stand', value: 240, color: 'info' },
  ];

  return (
    <Columns>
      <Column>
        <RadialBarChart
          label="Daily goals, thin rings"
          data={goals}
          max={500}
          barSize={10}
          hasBackground={false}
          height={240}
        />
      </Column>
      <Column>
        <RadialBarChart
          label="Daily goals, half rings"
          data={goals}
          max={500}
          startAngle={180}
          endAngle={0}
          height={240}
        />
      </Column>
    </Columns>
  );
}

Labels​

showLabels writes each bar's value at its start, in its Bulma color's -invert color, which stays readable on the bar. A bar too short for its value, such as one of 0, leaves the label on the track, so it's written in the chart's strong text color there.

function example() {
  const goals = [
    { name: 'Move', value: 420, color: 'danger' },
    { name: 'Exercise', value: 310, color: 'success' },
    { name: 'Stand', value: 240, color: 'info' },
  ];

  return (
    <RadialBarChart
      label="Daily goals"
      data={goals}
      max={500}
      unit=" kcal"
      showLabels
    />
  );
}

Legend and Hidden Bars​

hasLegend draws a legend below the chart, or above it with legendPosition="top". Each entry is a toggle button that hides or shows its bar, and the rings shown take its room. A hidden bar 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 goals = [
    { name: 'Move', value: 420, color: 'danger' },
    { name: 'Exercise', value: 310, color: 'success' },
    { name: 'Stand', value: 240, color: 'info' },
  ];
  const [hidden, setHidden] = useState(['Stand']);

  return (
    <>
      <Buttons mb="3">
        <Button size="small" onClick={() => setHidden([])}>
          Show all
        </Button>
      </Buttons>
      <RadialBarChart
        label="Daily goals"
        data={goals}
        max={500}
        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 spending = [
    { name: 'Marketing', value: 18250, color: 'primary' },
    { name: 'Engineering', value: 46100, color: 'link' },
    { name: 'Support', value: 9800, color: 'warning' },
  ];
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });

  return (
    <RadialBarChart
      label="Budget spent by team"
      data={spending}
      max={50000}
      valueFormatter={value => dollars.format(value)}
      showLabels
    />
  );
}

Colors​

A bar'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 bar with no color takes the next of Bulma's palette colors.

function example() {
  const goals = [
    { name: 'Move', value: 420, color: 'danger-40' },
    { name: 'Exercise', value: 310, color: 'danger-55' },
    { name: 'Stand', value: 240, color: 'danger-70' },
  ];

  return (
    <RadialBarChart
      label="Daily goals"
      data={goals}
      max={500}
      showLabels
      hasLegend
    />
  );
}

Showing the Data Table​

The data table is always there for screen readers: a row per bar, with its value and how much of max it fills. 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 goals = [
    { name: 'Bouger', value: 420 },
    { name: 'Exercice', value: 310 },
    { name: 'Debout', value: 240 },
  ];

  return (
    <RadialBarChart
      label="Objectifs du jour"
      showLabel
      showTable
      data={goals}
      max={500}
      labels={{ name: 'Objectif', value: 'Fait', share: 'Part' }}
      height={220}
    />
  );
}

Recharts Props and Children​

Each Recharts part the chart draws takes its own props after the chart's: radialBarProps for Recharts' RadialBar (here, rounded ends), chartProps for its RadialBarChart (here, a larger hole in the middle), and tooltipProps. children adds more Recharts elements, such as <defs> for a pattern a bar names with url(#id).

function example() {
  const goals = [
    { name: 'Move', value: 420, color: 'url(#move-stripes)' },
    { name: 'Exercise', value: 310, color: 'success' },
    { name: 'Stand', value: 240, color: 'info' },
  ];

  return (
    <RadialBarChart
      label="Daily goals"
      data={goals}
      max={500}
      barSize={18}
      radialBarProps={{ cornerRadius: 9 }}
      chartProps={{ innerRadius: '45%' }}
      hasLegend
    >
      <defs>
        <pattern
          id="move-stripes"
          width="8"
          height="8"
          patternUnits="userSpaceOnUse"
          patternTransform="rotate(45)"
        >
          <rect width="8" height="8" fill="var(--bulma-danger)" />
          <line
            x1="0"
            y1="0"
            x2="0"
            y2="8"
            stroke="var(--bulma-scheme-main)"
            strokeWidth="3"
          />
        </pattern>
      </defs>
    </RadialBarChart>
  );
}

Size​

The chart fills its container's width and is 300 pixels tall, and the rings fill the room that leaves. 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.

<RadialBarChart
  label="Two goals"
  data={[
    { name: 'Move', value: 420 },
    { name: 'Stand', value: 240 },
  ]}
  max={500}
  width={220}
  height={180}
/>


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. Radial bars use these, and the BarChart Theming section lists the rest:

:root {
--bulma-chart-track-color: var(--bulma-border-weak); /* the track */
--bulma-chart-text-color: var(--bulma-text-weak); /* labels on CSS colors */
}

Accessibility​

  • The chart is a <figure> named by label. The drawing is named by the same caption, and the data table's caption is the same name. description is read with the name of both.
  • A data table of every bar renders beside the drawing, visually hidden with Bulma's is-sr-only, so a screen reader reads the numbers the rings show (WAI: complex images). Each row has the bar's name as its row header, then its value and how much of max it fills. A hidden bar stays in it. showTable puts it on screen.
  • The drawing is a tab stop. Once it has focus, the tooltip shows the innermost bar, the right and left arrow keys move out and in through the bars, and Enter hides and shows the tooltip (Recharts' keyboard layer).
  • Each legend entry is a button with aria-pressed. A hidden bar'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 bar it is.
  • The drawing doesn't name the bars, so they're told apart by their order and their color. The legend lists them from the innermost ring out, and the tooltip and the data table name them. max is shown only by the track; say what a full ring means in description when it matters.
  • A label takes its Bulma color's -invert color; on a CSS color it takes the chart's text color, so check that it reads. A label too long for its bar sits on the track instead, in the chart's strong text color.

  • PieChart: Parts of a whole as the slices of a pie or a donut.
  • FunnelChart: The stages of a process, each as wide as its value.
  • RingProgress: One value, or a few, drawn as a ring.
  • 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​

PropTypeDefaultDescription
datareadonly ChartSlice[]—Required. The bars, from the innermost ring out, each a name, a value and an optional color.
hiddenSeriesstring[]—The names of the bars that are hidden, when you hold that state. A hidden bar leaves the drawing and the tooltip, and the rings shown take its room, but it stays in the data table.
barSizenumber—The width of each ring, in pixels. Without one, the rings share the room between the chart's middle and its edge. Rings wider than their share of it overlap.
startAnglenumber90Where each bar starts, in degrees counter-clockwise from three o'clock. The default and endAngle's run the bars clockwise from the top, as RingProgress does.
endAnglenumber-270Where a bar of value max ends, in degrees counter-clockwise from three o'clock.
hasBackgroundbooleantrueDraws each bar's track, the full ring a bar of value max fills.
maxnumberthe largest value in data, hidden bars included, so hiding one leaves the others as they areThe value a full ring stands for. A larger value still draws a full ring, and one below zero an empty one. A max that isn't more than 0 is ignored.
showLabelsbooleanfalseWrites each bar's value at its start. The label takes the -invert color of a Bulma color, which stays readable on the bar; on a CSS color it takes the chart's text color, so check that it reads. A bar too short for its label, such as one of 0, leaves it on the track, in the chart's strong text color.
radialBarPropsPartial<Omit<RadialBarProps, 'ref'>>—Props passed to Recharts' RadialBar, after the chart's own, such as cornerRadius.
chartPropsPartial<React.ComponentProps<typeof RechartsRadialBarChart>>—Props passed to Recharts' RadialBarChart, after the chart's own, such as innerRadius for the size of the hole in the middle.
hasLegendbooleanfalseShows a legend of the slices. Each entry is a toggle button that hides or shows its slice.
defaultHiddenSeriesstring[]—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.
tooltipPropsPartial<TooltipProps>—Props passed to Recharts' Tooltip, after the chart's own.
classNamestring—Additional CSS classes to apply.
labelstring—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.
descriptionstring—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.
showLabelbooleanfalseShows label above the chart as its caption.
showTablebooleanfalseShows 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.
hasTooltipbooleantrueShows a tooltip with the values under the pointer, or under the keyboard focus as the arrow keys move through the chart.
valueFormatterChartValueFormatter—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.
unitstring—Written after every value, such as % or kg.
widthChartSize'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.
heightChartSize300The chart's height, in pixels or as a percentage of its container, which then needs a height of its own.
childrenReact.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.
refReact.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.