Synertech Web/ui v0.57.5 Site Storybook

Documentation / Charts

PieChart

Client@synertech/uiOpen in Storybook

Proportions across a handful of categories, as a pie or a donut (variant="donut") with a centre figure. The colour follows the entity, not its rank: a slice keeps its hue whatever the sort (sort, "desc" by default).

Know when not to use it:

  • two slices is a StatCard;
  • close values are a BarChart: the eye compares angles poorly;
  • maxSlices (6 by default) counts the slices drawn, the grey one included: beyond it, the tail folds into that grey slice rather than taking a seventh colour.

A "use client" component: a tooltip giving the value and its share, an accessible table view, and patterns to add a texture per slice when the hue is not enough.

Examples

Pie

x.key names the slices, value names the measured field.

Open in a tab
import { PieChart } from "@synertech/ui";

const sources = [
  { source: "Direct", visits: 16400 },
  { source: "Organique", visits: 12500 },
  { source: "Référents", visits: 8700 },
  { source: "Social", visits: 6200 },
  { source: "Courriel", visits: 4300 },
];

export function Basic() {
  return (
    <PieChart
      title="Sources de trafic"
      description="30 derniers jours"
      data={sources}
      x={{ key: "source" }}
      value="visits"
      height={280}
    />
  );
}

Donut with a centre figure

centerLabel puts the total in the middle of the donut.

Open in a tab
import { PieChart } from "@synertech/ui";

const sources = [
  { source: "Direct", visits: 16400 },
  { source: "Organique", visits: 12500 },
  { source: "Référents", visits: 8700 },
  { source: "Social", visits: 6200 },
  { source: "Courriel", visits: 4300 },
];

export function Donut() {
  return (
    <PieChart
      title="Sources de trafic"
      data={sources}
      x={{ key: "source" }}
      value="visits"
      variant="donut"
      centerLabel={{ value: "48,1 k", label: "visites" }}
      height={280}
    />
  );
}

Folded tail

Beyond maxSlices slices, the grey one included, the smallest slices join it; otherLabel names it. Here, four sources and “Autres sources”.

Open in a tab
import { PieChart } from "@synertech/ui";

const sources = [
  { source: "Direct", visits: 16400 },
  { source: "Organique", visits: 12500 },
  { source: "Référents", visits: 8700 },
  { source: "Social", visits: 6200 },
  { source: "Courriel", visits: 4300 },
];

const longTail = [
  ...sources,
  { source: "Partenaires", visits: 2100 },
  { source: "Publicité", visits: 1600 },
  { source: "Affiliation", visits: 900 },
];

export function FoldedTail() {
  return (
    <PieChart
      title="Sources de trafic"
      data={longTail}
      x={{ key: "source" }}
      value="visits"
      maxSlices={5}
      otherLabel="Autres sources"
      tableView="toggle"
      height={280}
    />
  );
}

Tight space and textures

Without labels, the legend on the right carries the identity alone; patterns backs it up with a texture.

Open in a tab
import { PieChart } from "@synertech/ui";

const sources = [
  { source: "Direct", visits: 16400 },
  { source: "Organique", visits: 12500 },
  { source: "Référents", visits: 8700 },
  { source: "Social", visits: 6200 },
  { source: "Courriel", visits: 4300 },
];

export function CompactPatterned() {
  return (
    <PieChart
      title="Sessions par source"
      data={sources}
      x={{ key: "source" }}
      value="visits"
      variant="donut"
      showLabels={false}
      legend="right"
      patterns
      height={220}
    />
  );
}

Clickable slice

onSliceClick receives the key, the value and the share: enough to filter the rest of the page.

Open in a tab
import { useState } from "react";
import { PieChart } from "@synertech/ui";

const sources = [
  { source: "Direct", visits: 16400 },
  { source: "Organique", visits: 12500 },
  { source: "Référents", visits: 8700 },
  { source: "Social", visits: 6200 },
  { source: "Courriel", visits: 4300 },
];

export function SliceSelection() {
  const [selected, setSelected] = useState<string | null>(null);

  return (
    <div className="flex flex-col gap-3">
      <PieChart
        title="Sources de trafic"
        data={sources}
        x={{ key: "source" }}
        value="visits"
        variant="donut"
        onSliceClick={(slice) => setSelected(`${slice.label} · ${Math.round(slice.share * 100)} %`)}
        height={260}
      />
      <p className="text-sm text-gray-600 dark:text-gray-400">
        {selected ? `Filtre : ${selected}` : "Cliquez sur une tranche pour filtrer."}
      </p>
    </div>
  );
}

API reference

Components

PieChart

ComponentClient

Proportions across a handful of categories.

The one chart of the kit whose documentation has to say when not to use it. Two slices is a stat tile. Close values are a bar chart — the eye compares angles badly. It earns its place for part-to-whole at a glance, over six slices at most, which is why the tail folds into a single bucket rather than taking a seventh colour.

import { PieChart } from "@synertech/ui";

Props — PieChartProps

PropTypeDefaultDescription
data• ChartDatum[] —
value• string —

Field holding the value of each slice.

x• ChartXAxisConfig —

Field holding the name of each slice.

actions ReactNode —

Slot on the right of the caption: an export button, a range picker.

A filter that frames several panels belongs in a ChartFilterBar above all of them; this is for what belongs to this panel alone.

centerLabel { value: ReactNode; label?: ReactNode } —

The figure a ring holds in its middle.

description ReactNode —
emptyState ReactNode —

Replaces the "no data" state: usually an EmptyState saying why, and what to do.

error boolean false

The data could not be loaded.

Never the same thing as having none: a panel that says "no data" after a failed request states something false, and offers no way out.

errorState ReactNode —

Replaces the whole built-in error state, message and button included.

height number 260
innerRadius number —

Share of the outer radius left open. Defaults to the donut ratio.

legend boolean | ChartLegendPosition —
loading boolean false

Dims the plot while fresh data loads; a first load shows a spinner.

locale string —

BCP 47 tag the numbers and the dates are written in. Defaults to the locale of UILabelsProvider, then to the language of the runtime.

maxSlices number 6

The most slices drawn, the folded bucket included. Past it, the tail folds into one grey slice — of two rows at least: a bucket holding one row would rename it and save nothing.

onRetry () => void —

Draws the retry button in the error state, and is called when pressed.

onSliceClick (slice: { key: string; label: string; value: number; share: number }) => void —

Called with the mark that was clicked.

Enter and Space on the focused chart do the same for the mark the keyboard is on.

otherLabel string —
patterns boolean false

Draws each slice with a texture on top of its colour.

The channel a reader falls back on when the hue fails them: severe colour vision deficiency, a grayscale print, a forced-colours theme. Off by default — it is an accessibility option, not a decoration.

The folded tail keeps its flat grey: it is the one slice that is not an identity, and giving it a texture would make it look like one.

plotLabel string —
showLabels boolean true

Draws each slice's name outside the ring, on a leader line.

size ChartSize "md"
sort "none" | "asc" | "desc" "desc"
tableView boolean | "toggle" false
title ReactNode —
tooltip boolean | ((point: ChartPoint & { share: number }) => ReactNode) true
valueLabel string —

Header of the value column of the data table. x.label names the slices, never the measure.

variant "pie" | "donut" "pie"
width number —

+ 277 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).