Skip to main content

SunburstChart

Overview​

The SunburstChart component draws a tree of values as rings, each node a sector sized by its share of the whole, around its parent.

It wraps Recharts' SunburstChart and comes from the @allxsmith/bestax-charts package, which the Charts guide shows how to install. It takes the same tree of data as Treemap: the top-level nodes make the inner ring, and each node's children the ring outside it, across the same angle. It draws them with Bulma's colors, writes each name in a color readable on its sector, shows a tooltip in a Bulma box, and renders a data table screen readers read.


Import​

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

Usage​

Basic Usage​

Each node of data has a name, and either a value or children, whose values it adds up. label names the chart; it's required, because it's what a screen reader announces.

function example() {
  const budget = [
    {
      name: 'Housing',
      children: [
        { name: 'Rent', value: 1400 },
        { name: 'Utilities', value: 180 },
      ],
    },
    {
      name: 'Food',
      children: [
        { name: 'Groceries', value: 520 },
        { name: 'Dining out', value: 240 },
      ],
    },
    { name: 'Savings', value: 600 },
  ];

  return <SunburstChart label="Monthly budget" data={budget} height={360} />;
}

Deeper Trees​

Each level of the tree is a ring further out, so a deeper tree has thinner rings.

function example() {
  const sales = [
    {
      name: 'Americas',
      children: [
        {
          name: 'North',
          children: [
            { name: 'US', value: 420 },
            { name: 'Canada', value: 90 },
          ],
        },
        { name: 'South', children: [{ name: 'Brazil', value: 140 }] },
      ],
    },
    {
      name: 'Europe',
      children: [
        { name: 'West', children: [{ name: 'France', value: 160 }] },
        { name: 'North', children: [{ name: 'Sweden', value: 70 }] },
      ],
    },
  ];

  return <SunburstChart label="Sales by region" data={sales} height={420} />;
}

Rings and Angles​

innerRadius sets the hole in the middle (0 closes it) and outerRadius the outer edge, in pixels; the outer edge fills the chart unless you set it. The rings start at twelve o'clock and run clockwise. startAngle and endAngle move them, in degrees counterclockwise from three o'clock, and an endAngle below startAngle runs clockwise.

function example() {
  const budget = [
    { name: 'Rent', value: 1400 },
    { name: 'Food', value: 760 },
    { name: 'Savings', value: 600 },
  ];

  return (
    <Columns>
      <Column>
        <SunburstChart
          label="No hole"
          showLabel
          data={budget}
          innerRadius={0}
          height={240}
        />
      </Column>
      <Column>
        <SunburstChart
          label="Half a circle"
          showLabel
          data={budget}
          startAngle={180}
          endAngle={0}
          height={240}
        />
      </Column>
    </Columns>
  );
}

Names on the Sectors​

Each sector that has room for its name shows it, in the -invert color of the sector's Bulma color so it stays readable in light and dark mode. showLabels={false} leaves the names off.

function example() {
  return (
    <SunburstChart
      label="Monthly budget"
      showLabels={false}
      data={[
        { name: 'Rent', value: 1400 },
        { name: 'Food', value: 760 },
        { name: 'Savings', value: 600 },
      ]}
      height={240}
    />
  );
}

Formatting Values​

Values are written as useNumberFormat writes a number, in the LocaleProvider's locale. valueFormatter writes them your way instead, in the tooltip and the data table, and unit is written after each one. Shares are written as the locale writes a percentage.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });

  return (
    <SunburstChart
      label="Revenue by plan"
      data={[
        {
          name: 'Subscriptions',
          children: [
            { name: 'Pro', value: 48200 },
            { name: 'Team', value: 31900 },
          ],
        },
        { name: 'Add-ons', value: 9400 },
      ]}
      valueFormatter={value => dollars.format(value)}
      showTable
      height={300}
    />
  );
}

Colors​

A node's color takes a Bulma color name, a shade of one of Bulma's six palette colors (primary-30, info-dark), or any CSS color. A top-level node with no color takes the next of Bulma's palette colors, and a node below takes its parent's, so each branch draws in one color. Shades set on the children tell the rings apart.

function example() {
  const teams = [
    {
      name: 'Design',
      color: 'link-30',
      children: [
        { name: 'Research', value: 4, color: 'link-45' },
        { name: 'Visual', value: 6, color: 'link-60' },
      ],
    },
    {
      name: 'Engineering',
      color: 'success-30',
      children: [
        { name: 'Web', value: 12, color: 'success-45' },
        { name: 'Platform', value: 8, color: 'success-60' },
      ],
    },
  ];

  return <SunburstChart label="People by team" data={teams} height={320} />;
}

Showing the Data Table​

The data table is always there for screen readers: a row per node, depth first, headed by its path, with its value and its share of the whole. showTable puts it on screen too, below the chart, and showLabel shows the chart's label above it as a caption. labels translates the table's and the tooltip's headers.

function example() {
  return (
    <SunburstChart
      label="Haushalt"
      showLabel
      showTable
      data={[
        {
          name: 'Wohnen',
          children: [
            { name: 'Miete', value: 1250 },
            { name: 'Strom', value: 95 },
          ],
        },
        { name: 'Essen', value: 480 },
      ]}
      labels={{ name: 'Posten', value: 'Betrag', share: 'Anteil' }}
      unit=" €"
      height={260}
    />
  );
}

Recharts Props and Children​

chartProps goes to Recharts' SunburstChart after the chart's own props (here, ringPadding for room between the rings), and tooltipProps to its Tooltip. children adds more elements inside the drawing, such as a total written in the middle of a chart of a fixed size.

function example() {
  return (
    <SunburstChart
      label="Monthly budget"
      data={[
        {
          name: 'Housing',
          children: [
            { name: 'Rent', value: 1400 },
            { name: 'Utilities', value: 180 },
          ],
        },
        { name: 'Food', value: 760 },
        { name: 'Savings', value: 600 },
      ]}
      chartProps={{ ringPadding: 6 }}
      width={320}
      height={320}
    >
      <text
        x={160}
        y={160}
        textAnchor="middle"
        dominantBaseline="central"
        fill="var(--bulma-text)"
        fontWeight={600}
      >
        $2,940
      </text>
    </SunburstChart>
  );
}

Size​

The chart fills its container's width and is 300 pixels tall. width and height take pixels or a percentage of the container, and the rings fill the smaller of the two. Recharts' SunburstChart draws its rings once the page has loaded, at any size, so on the server the figure renders with its data table and no rings.

<SunburstChart
  label="Two items"
  data={[
    { name: 'Rent', value: 1400 },
    { name: 'Food', value: 760 },
  ]}
  width={240}
  height={240}
/>


Theming​

The sectors are parted by --bulma-chart-stroke-color, which falls back to Bulma's --bulma-scheme-main. The BarChart's Theming section lists the other variables the charts draw with.

:root {
--bulma-chart-stroke-color: var(--bulma-scheme-main); /* between sectors */
}

Accessibility​

  • The chart is a <figure> named by label, which is also its data table's caption. description is read with the name. Recharts' SunburstChart passes no attributes to its <svg>, so the element around the drawing is the picture (role="img"), named and described by the same caption and description.
  • A data table of every node renders beside the drawing, visually hidden with Bulma's is-sr-only, so a screen reader reads the numbers the sectors show (WAI: complex images). Each row is headed by the node's path, such as "Housing / Rent", and gives its value and its share of the whole. showTable puts it on screen.
  • Recharts' keyboard layer doesn't reach a sunburst's sectors, so the chart is not a tab stop and its tooltip follows the pointer only. The data table holds every value the tooltip shows.
  • The names written on the sectors take the -invert color of a Bulma color, so they read on it in light and dark mode. A name that doesn't fit its sector is left off; the data table names every node.
  • Branches are told apart by color in the drawing, and a node's ring tells its level. The names on the sectors and the paths in the data table say which branch a sector is in.

  • Treemap: The same tree of values drawn as nested tiles.
  • Sankey: Flows between nodes.
  • BarChart: Values drawn as bars.
  • 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 ChartNode[]—Required. The nodes to draw: the top-level nodes as the inner ring, and each node's children in the ring outside it, across the same angle.
innerRadiusnumber50The radius of the hole in the middle, in pixels.
outerRadiusnumberhalf the smaller of the chart's width and heightThe radius of the outer edge, in pixels.
startAnglenumber90Where the rings start, in degrees counterclockwise from three o'clock, so 90 is twelve o'clock.
endAnglenumber-270Where the rings end, in degrees as startAngle. Less than startAngle draws clockwise.
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, measured in the browser. Recharts' SunburstChart draws its rings once the page has loaded at any size, so on the server the figure renders with its data table and no rings.
showLabelsbooleantrueWrites each node's name on its sector, in a color readable on it, where the name fits.
labels{ name?: string; value?: string; share?: string }—The header text of the data table and the tooltip, to translate.
chartPropsPartial<React.ComponentProps<typeof RechartsSunburstChart>>—Props passed to Recharts' SunburstChart, after the chart's own, such as ringPadding.
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.
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).
refReact.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 as primary or grey-light, a shade of one of Bulma's six palette colors such as primary-60 or info-dark, or any CSS color, such as #3e8ed0, var(--brand) or url(#stripes) for a pattern the chart's children define. A Bulma name draws with Bulma's own CSS variable, so it follows Theme and dark mode.
  • ChartNode: One node of a tree chart's data, such as a category or one of its parts. Its fields:
    • name: string — Required. What the node is called. The chart writes it on the node, and the data table heads the node's row with it, after its ancestors' names.
    • value: number — The node's size. A node with children takes the sum of theirs instead, and a missing or negative value counts as zero.
    • color: ChartColor — Its color. Without one, a top-level node takes the next of Bulma's palette colors, and a node below takes its parent's, so each branch draws in one color.
    • children: { name: string; value?: number; color?: ChartColor; children?: ChartNode[] }[] — The nodes inside this one.
  • 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.