Synertech Web/ui v0.57.5 Site Storybook

Documentation / Charts

BarChart

Client@synertech/uiOpen in Storybook

Comparison between categories, as columns or bars (orientation), grouped, stacked or 100% stacked (stack). The value axis anchors at zero: a bar is read by the length of its mark. Marks are capped at 24 px, rounded on the value end, separated by 2 px of the background colour; sort reorders the categories and the table view follows that order.

The same component covers three other readings:

  • diverging reads the bars against a reference (zero, a number, or a central series for a Likert scale); the ramp says which side and how far, never whether it is good news;
  • bins makes it a histogram: x.key becomes a measurement, counted per bucket (sort, stack and diverging are then ignored);
  • a type: "line" series is drawn over the bars. The axis stays single, and will remain so.

A "use client" component.

Examples

Grouped columns

With stack="none" (the default), the series sit side by side in each category.

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

const channels = [
  { quarter: "T1", direct: 4200, organique: 3100, payant: 1800 },
  { quarter: "T2", direct: 4600, organique: 3900, payant: 2100 },
  { quarter: "T3", direct: 4100, organique: 4400, payant: 2600 },
  { quarter: "T4", direct: 5300, organique: 5100, payant: 2400 },
];

const channelSeries = [
  { key: "direct", label: "Direct" },
  { key: "organique", label: "Organique" },
  { key: "payant", label: "Payant" },
];

export function Grouped() {
  return (
    <BarChart
      title="Sessions par canal"
      data={channels}
      x={{ key: "quarter", label: "Trimestre" }}
      series={channelSeries}
      height={280}
    />
  );
}

Stacked and 100%

stacked reads the total, percent the composition of each quarter.

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

const channels = [
  { quarter: "T1", direct: 4200, organique: 3100, payant: 1800 },
  { quarter: "T2", direct: 4600, organique: 3900, payant: 2100 },
  { quarter: "T3", direct: 4100, organique: 4400, payant: 2600 },
  { quarter: "T4", direct: 5300, organique: 5100, payant: 2400 },
];

const channelSeries = [
  { key: "direct", label: "Direct" },
  { key: "organique", label: "Organique" },
  { key: "payant", label: "Payant" },
];

export function Stacked() {
  return (
    <div className="grid gap-6 lg:grid-cols-2">
      <BarChart
        title="Total par trimestre"
        data={channels}
        x={{ key: "quarter" }}
        series={channelSeries}
        stack="stacked"
        height={240}
      />
      <BarChart
        title="Part de chaque canal"
        data={channels}
        x={{ key: "quarter" }}
        series={channelSeries}
        stack="percent"
        patterns
        height={240}
      />
    </div>
  );
}

Sorted horizontal bars

As soon as names are long, orientation="horizontal" avoids rotated labels; sort and showValues do the ranking.

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

const regions = [
  { region: "Île-de-France", sales: 221000 },
  { region: "Auvergne-Rhône-Alpes", sales: 163000 },
  { region: "Nouvelle-Aquitaine", sales: 132000 },
  { region: "Hauts-de-France", sales: 96000 },
  { region: "Bretagne", sales: 61000 },
];

export function HorizontalRanking() {
  return (
    <BarChart
      title="Ventes par région"
      data={regions}
      x={{ key: "region" }}
      series={[{ key: "sales", label: "Ventes" }]}
      y={{ format: (value) => `${Math.round(value / 1000)} k€` }}
      orientation="horizontal"
      sort="desc"
      showValues
      height={260}
    />
  );
}

Deviation from a reference

diverging moves the baseline to zero: the bars go both ways and take the diverging ramp.

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

const gaps = [
  { region: "Nord", gap: 18 },
  { region: "Est", gap: 9 },
  { region: "Centre", gap: 2 },
  { region: "Ouest", gap: -11 },
  { region: "Sud", gap: -17 },
];

export function Diverging() {
  return (
    <BarChart
      title="Écart à la cible par région"
      data={gaps}
      x={{ key: "region" }}
      series={[{ key: "gap", label: "Écart" }]}
      y={{ label: "Écart à la cible (%)", format: (value) => `${value} %` }}
      orientation="horizontal"
      diverging
      showValues
      height={240}
    />
  );
}

Histogram

bins counts the observations per bucket; the single series names the count, its key is not read from the data.

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

const durations = [
  4, 7, 9, 12, 13, 15, 17, 19, 22, 24, 28, 31, 34, 38, 44, 49, 55, 61, 68, 77, 88, 101, 117, 138,
  171, 214, 268,
].map((duration) => ({ duration }));

export function Histogram() {
  return (
    <BarChart
      title="Distribution des durées de session"
      data={durations}
      x={{ key: "duration", label: "Durée de session (s)" }}
      series={[{ key: "sessions", label: "Sessions" }]}
      bins={[0, 15, 30, 60, 120, 300]}
      tableView="toggle"
      height={260}
    />
  );
}

Bars and line

A type: "line" series is lifted out of the bars: the volume and its trend, on a single axis because both count units.

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

const monthlyVolume = [
  { month: "Jan", orders: 1240, returns: 88 },
  { month: "Fév", orders: 1380, returns: 104 },
  { month: "Mar", orders: 1120, returns: 71 },
  { month: "Avr", orders: 1610, returns: 132 },
  { month: "Mai", orders: 1840, returns: 118 },
];

export function Combined() {
  return (
    <BarChart
      title="Commandes et retours"
      description="Une seule échelle : les deux mesures comptent des unités"
      data={monthlyVolume}
      x={{ key: "month" }}
      series={[
        { key: "orders", label: "Commandes" },
        { key: "returns", label: "Retours", type: "line" },
      ]}
      y={{ label: "Unités" }}
      curve="monotone"
      onPointClick={(point) => console.log(point.seriesLabel, point.x, point.value)}
      height={260}
    />
  );
}

API reference

Components

BarChart

ComponentClient

Comparison between categories.

One component for both readings: orientation turns columns into bars, which is the right call as soon as the category names are long enough that rotating them would make them unreadable.

The value axis anchors at zero and cannot be talked out of it lightly: a bar is read for the length of its mark, and an axis starting at 40 exaggerates every difference on the chart.

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

Props — BarChartProps

PropTypeDefaultDescription
data• ChartDatum[] —
series• ChartSeries[] —
x• ChartXAxisConfig —
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.

annotations ChartAnnotation[] —

What happened, drawn where it happened: a release, a campaign, an outage.

Read on the category axis, so a vertical chart alone takes them: the horizontal one runs its categories up the side, and a moment placed there is not a moment any more. They are ignored, with a warning, when the bars are laid out horizontally.

barSize number —

Caps the thickness of a mark. Never fills the slot: the leftover is air.

bins BinSpec —

Turns the chart into a histogram: the distribution of one numeric field.

The chart then reads x.key on every row as a measurement, buckets those measurements, and draws how many fell in each bucket. series must hold a single entry, which names and colours the count — its key is the header of the count column, not a field read on the data.

true picks a bucket count from the sample size, a number asks for that many, and an array of edges cuts them by hand (uneven widths allowed). The bars then touch, separated only by the surface gap: the axis underneath is a continuous measure, and air between the bars would read as empty ranges.

Where a scatter plot shows a relation, a histogram shows a shape — which is why sort, stack and diverging are ignored here: the buckets have an order of their own and reordering them destroys the shape.

brush boolean false

Lets a pointer drag a window across the category axis.

Shift + arrows does the same from the keyboard, Escape drops it, and the window is published to the surrounding ChartSyncProvider. Ignored when sort ranks the categories: a stretch of a ranking is not a stretch of the data, and a group's window is not drawn on one either.

brushLabel string —

Accessible name of the selected window.

className string —
curve ChartCurve "linear"

Shape of the line drawn for a series marked type: "line".

Only read by a combined chart; the bars have no curve.

description ReactNode —
diverging boolean | number | string —

Reads the bars against a reference instead of against zero.

true centres on zero and a number centres on that value: the baseline moves there, bars run both ways from it, and a lone series takes the diverging ramp — the gap to a target, an index against its base.

A series KEY centres the stack on that series instead, splitting it evenly on both sides: the Likert layout, where the middle of an agreement scale has to sit on the middle of the chart.

The ramp says which side of the reference a bar falls on and how far. It never says whether that is good news — a schedule slipping is "above", and above is not good.

emphasis string —

Paints one series in its colour and greys out every other one.

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.

grid ChartGridLines "y"
height number 260

Height of the plot area in pixels. Width always follows the container.

legend boolean | ChartLegendPosition —
legendToggle boolean true
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.

margin Partial<ChartMargin> —
onBrush (range: ChartBrushRange | null) => void —

Called when a window is set, and with null when one is dropped.

onPointClick (event: ChartPointEvent) => 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.

onRetry () => void —

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

orientation BarChartOrientation "vertical"

vertical draws columns, horizontal draws bars.

Horizontal is the right call as soon as the category names are long: a rotated label is a label nobody reads.

overlay ChartOverlay —

Draws inside the plot, with the chart's own resolved geometry.

patterns boolean false

Draws each series 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, and a textured chart nobody needs textured is just a busier chart.

Ignored where the bars are painted by their own value (a lone series against a reference): the ramp measures a magnitude, and a texture can only say which series a mark belongs to.

plotLabel string —
referenceLines ChartReferenceLine[] —
showValues boolean false

Draws the value at the end of each bar, where it fits without collision.

size ChartSize "md"
sort "none" | "asc" | "desc" "none"

Orders the categories by their total. Ignored by a histogram.

stack ChartStackMode "none"

none groups the series side by side; the others stack them.

sync boolean true

false keeps the chart out of the surrounding ChartSyncProvider.

tableView boolean | "toggle" false

Renders the accessible data table: always (true) or behind a button.

title ReactNode —
tooltip boolean | ((payload: ChartTooltipPayload) => ReactNode) true

false disables the hover layer; a function replaces its content.

width number —

Fixes the width instead of measuring the container.

y ChartYAxisConfig —

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

Functions

Resets the one-shot warnings. Test helper.

import { resetBarChartWarnings } from "@synertech/ui";
resetBarChartWarnings(): void

Types

type BarChartOrientation = CartesianOrientation