Synertech Web/ui v0.57.5 Site Storybook

Documentation / Dashboards

ChartContainer

Client@synertech/uiOpen in Storybook

The frame every chart of the kit is drawn in. It carries everything that is the same from one chart to the next: the measured width, the caption (title + description), the legend placement, the actions slot, the loading, empty and error states, and the data table button. The chart itself keeps only its geometry.

The plot area is a render prop: a chart cannot lay itself out before the container has been measured. children receives { width, height, measured }, measured being false while the width is still the fallback value (server render, first paint). Use ChartContainer to draw a mark the kit does not offer, with the same frame and the same accessibility as the other panels.

The three states are ranked and mutually exclusive: loading wins over error, which wins over empty. An error is never an empty chart: only the error state offers a way out, through onRetry. The figure is named by its title (or ariaLabel). Client component ("use client").

Examples

A custom chart

The child function receives the measured width and the requested height (height, 240 px by default); the title and description name the figure.

Open in a tab
import { ChartAxis, ChartContainer, ChartGrid, getSeriesColor, type ChartTick } from "@synertech/ui";

const visits = [
  { month: "Jan", web: 18, app: 12 },
  { month: "Fév", web: 21, app: 13 },
  { month: "Mar", web: 20, app: 15 },
  { month: "Avr", web: 26, app: 17 },
  { month: "Mai", web: 29, app: 19 },
];

function Columns({ width, height }: { width: number; height: number }) {
  const margin = { top: 8, right: 8, bottom: 24, left: 32 };
  const innerWidth = Math.max(0, width - margin.left - margin.right);
  const innerHeight = height - margin.top - margin.bottom;
  const max = 30;
  const slot = innerWidth / visits.length;
  const bar = Math.min(16, slot / 3);
  const y = (value: number) => innerHeight - (value / max) * innerHeight;

  const yTicks: ChartTick[] = [0, 10, 20, 30].map((value) => ({
    value,
    offset: y(value),
    label: `${value} k`,
  }));
  const xTicks: ChartTick[] = visits.map((row, index) => ({
    value: row.month,
    offset: slot * (index + 0.5),
    label: row.month,
  }));

  return (
    <svg width={width} height={height} role="img" aria-label="Visites mensuelles par canal">
      <g transform={`translate(${margin.left}, ${margin.top})`}>
        <ChartGrid
          width={innerWidth}
          height={innerHeight}
          horizontal={yTicks.map((t) => t.offset)}
        />
        {visits.map((row, index) =>
          [row.web, row.app].map((value, s) => (
            <rect
              key={`${row.month}-${s}`}
              x={slot * (index + 0.5) - bar - 1 + s * (bar + 2)}
              y={y(value)}
              width={bar}
              height={innerHeight - y(value)}
              fill={getSeriesColor(s)}
            />
          ))
        )}
        <ChartAxis orientation="left" ticks={yTicks} length={innerHeight} showLine={false} />
        <g transform={`translate(0, ${innerHeight})`}>
          <ChartAxis orientation="bottom" ticks={xTicks} length={innerWidth} />
        </g>
      </g>
    </svg>
  );
}

export function Basic() {
  return (
    <ChartContainer title="Visites mensuelles" description="En milliers" height={220}>
      {({ width, height }) => <Columns width={width} height={height} />}
    </ChartContainer>
  );
}

Legend and actions

legend accepts any node, usually a ChartLegend built with toLegendItems; legendPosition places it. actions aligns to the right of the title.

Open in a tab
import { Button, ChartAxis, ChartContainer, ChartGrid, ChartLegend, getSeriesColor, toLegendItems, type ChartSeries, type ChartTick } from "@synertech/ui";

const series: ChartSeries[] = [
  { key: "web", label: "Web" },
  { key: "app", label: "Application" },
];

const visits = [
  { month: "Jan", web: 18, app: 12 },
  { month: "Fév", web: 21, app: 13 },
  { month: "Mar", web: 20, app: 15 },
  { month: "Avr", web: 26, app: 17 },
  { month: "Mai", web: 29, app: 19 },
];

function Columns({ width, height }: { width: number; height: number }) {
  const margin = { top: 8, right: 8, bottom: 24, left: 32 };
  const innerWidth = Math.max(0, width - margin.left - margin.right);
  const innerHeight = height - margin.top - margin.bottom;
  const max = 30;
  const slot = innerWidth / visits.length;
  const bar = Math.min(16, slot / 3);
  const y = (value: number) => innerHeight - (value / max) * innerHeight;

  const yTicks: ChartTick[] = [0, 10, 20, 30].map((value) => ({
    value,
    offset: y(value),
    label: `${value} k`,
  }));
  const xTicks: ChartTick[] = visits.map((row, index) => ({
    value: row.month,
    offset: slot * (index + 0.5),
    label: row.month,
  }));

  return (
    <svg width={width} height={height} role="img" aria-label="Visites mensuelles par canal">
      <g transform={`translate(${margin.left}, ${margin.top})`}>
        <ChartGrid
          width={innerWidth}
          height={innerHeight}
          horizontal={yTicks.map((t) => t.offset)}
        />
        {visits.map((row, index) =>
          [row.web, row.app].map((value, s) => (
            <rect
              key={`${row.month}-${s}`}
              x={slot * (index + 0.5) - bar - 1 + s * (bar + 2)}
              y={y(value)}
              width={bar}
              height={innerHeight - y(value)}
              fill={getSeriesColor(s)}
            />
          ))
        )}
        <ChartAxis orientation="left" ticks={yTicks} length={innerHeight} showLine={false} />
        <g transform={`translate(0, ${innerHeight})`}>
          <ChartAxis orientation="bottom" ticks={xTicks} length={innerWidth} />
        </g>
      </g>
    </svg>
  );
}

export function LegendAndActions() {
  return (
    <ChartContainer
      title="Visites par canal"
      height={220}
      legend={<ChartLegend items={toLegendItems(series)} marker="rect" />}
      legendPosition="top"
      actions={
        <Button size="sm" onClick={() => console.log("Actualiser")}>
          Actualiser
        </Button>
      }
    >
      {({ width, height }) => <Columns width={width} height={height} />}
    </ChartContainer>
  );
}

Twin data table

table receives the table, often a ChartTableView; tableView="toggle" puts it behind a button, true leaves it open.

Open in a tab
import { ChartAxis, ChartContainer, ChartGrid, ChartTableView, getSeriesColor, type ChartSeries, type ChartTick } from "@synertech/ui";

const visits = [
  { month: "Jan", web: 18, app: 12 },
  { month: "Fév", web: 21, app: 13 },
  { month: "Mar", web: 20, app: 15 },
  { month: "Avr", web: 26, app: 17 },
  { month: "Mai", web: 29, app: 19 },
];

const series: ChartSeries[] = [
  { key: "web", label: "Web" },
  { key: "app", label: "Application" },
];

function Columns({ width, height }: { width: number; height: number }) {
  const margin = { top: 8, right: 8, bottom: 24, left: 32 };
  const innerWidth = Math.max(0, width - margin.left - margin.right);
  const innerHeight = height - margin.top - margin.bottom;
  const max = 30;
  const slot = innerWidth / visits.length;
  const bar = Math.min(16, slot / 3);
  const y = (value: number) => innerHeight - (value / max) * innerHeight;

  const yTicks: ChartTick[] = [0, 10, 20, 30].map((value) => ({
    value,
    offset: y(value),
    label: `${value} k`,
  }));
  const xTicks: ChartTick[] = visits.map((row, index) => ({
    value: row.month,
    offset: slot * (index + 0.5),
    label: row.month,
  }));

  return (
    <svg width={width} height={height} role="img" aria-label="Visites mensuelles par canal">
      <g transform={`translate(${margin.left}, ${margin.top})`}>
        <ChartGrid
          width={innerWidth}
          height={innerHeight}
          horizontal={yTicks.map((t) => t.offset)}
        />
        {visits.map((row, index) =>
          [row.web, row.app].map((value, s) => (
            <rect
              key={`${row.month}-${s}`}
              x={slot * (index + 0.5) - bar - 1 + s * (bar + 2)}
              y={y(value)}
              width={bar}
              height={innerHeight - y(value)}
              fill={getSeriesColor(s)}
            />
          ))
        )}
        <ChartAxis orientation="left" ticks={yTicks} length={innerHeight} showLine={false} />
        <g transform={`translate(0, ${innerHeight})`}>
          <ChartAxis orientation="bottom" ticks={xTicks} length={innerWidth} />
        </g>
      </g>
    </svg>
  );
}

export function WithTable() {
  return (
    <ChartContainer
      title="Visites mensuelles"
      height={220}
      tableView="toggle"
      table={
        <ChartTableView
          data={visits}
          series={series}
          xKey="month"
          xLabel="Mois"
          formatValue={(value) => `${value} k`}
          caption="Visites mensuelles par canal, en milliers"
        />
      }
    >
      {({ width, height }) => <Columns width={width} height={height} />}
    </ChartContainer>
  );
}

Loading, empty and error

The three states replace the plot area. A reload keeps the previous plot dimmed; a retry replaces the error message with the spinner.

Open in a tab
import { useState } from "react";
import { Button, ChartAxis, ChartContainer, EmptyState, ChartGrid, getSeriesColor, type ChartTick } from "@synertech/ui";

const visits = [
  { month: "Jan", web: 18, app: 12 },
  { month: "Fév", web: 21, app: 13 },
  { month: "Mar", web: 20, app: 15 },
  { month: "Avr", web: 26, app: 17 },
  { month: "Mai", web: 29, app: 19 },
];

function Columns({ width, height }: { width: number; height: number }) {
  const margin = { top: 8, right: 8, bottom: 24, left: 32 };
  const innerWidth = Math.max(0, width - margin.left - margin.right);
  const innerHeight = height - margin.top - margin.bottom;
  const max = 30;
  const slot = innerWidth / visits.length;
  const bar = Math.min(16, slot / 3);
  const y = (value: number) => innerHeight - (value / max) * innerHeight;

  const yTicks: ChartTick[] = [0, 10, 20, 30].map((value) => ({
    value,
    offset: y(value),
    label: `${value} k`,
  }));
  const xTicks: ChartTick[] = visits.map((row, index) => ({
    value: row.month,
    offset: slot * (index + 0.5),
    label: row.month,
  }));

  return (
    <svg width={width} height={height} role="img" aria-label="Visites mensuelles par canal">
      <g transform={`translate(${margin.left}, ${margin.top})`}>
        <ChartGrid
          width={innerWidth}
          height={innerHeight}
          horizontal={yTicks.map((t) => t.offset)}
        />
        {visits.map((row, index) =>
          [row.web, row.app].map((value, s) => (
            <rect
              key={`${row.month}-${s}`}
              x={slot * (index + 0.5) - bar - 1 + s * (bar + 2)}
              y={y(value)}
              width={bar}
              height={innerHeight - y(value)}
              fill={getSeriesColor(s)}
            />
          ))
        )}
        <ChartAxis orientation="left" ticks={yTicks} length={innerHeight} showLine={false} />
        <g transform={`translate(0, ${innerHeight})`}>
          <ChartAxis orientation="bottom" ticks={xTicks} length={innerWidth} />
        </g>
      </g>
    </svg>
  );
}

export function States() {
  const [state, setState] = useState<"loading" | "empty" | "error">("error");

  return (
    <div className="flex flex-col gap-3">
      <div className="flex gap-2">
        {(["loading", "empty", "error"] as const).map((value) => (
          <Button key={value} size="sm" onClick={() => setState(value)}>
            {value === "loading" ? "Chargement" : value === "empty" ? "Vide" : "Erreur"}
          </Button>
        ))}
      </div>
      <ChartContainer
        title="Visites mensuelles"
        height={200}
        loading={state === "loading"}
        empty
        emptyState={
          <EmptyState
            size="sm"
            title="Aucune visite sur la période"
            description="Choisissez une période plus longue."
          />
        }
        error={state === "error"}
        onRetry={() => setState("loading")}
      >
        {({ width, height }) => <Columns width={width} height={height} />}
      </ChartContainer>
    </div>
  );
}

API reference

Components

ChartContainer

ComponentClient

The frame every chart of the kit is drawn in.

It owns what is identical from one chart to the next — measured width, caption, legend slot, the loading, empty and error states, and the data table toggle — so that a chart component is left with its geometry and nothing else.

The plot area is a render prop: a chart cannot lay itself out before the container has been measured.

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

Props — ChartContainerProps

PropTypeDefaultDescription
children• ReactNode | ((args: ChartContainerRenderArgs) => ReactNode) —
actions ReactNode —

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

ariaLabel string —

Names the figure when it has no visible title.

description ReactNode —
empty boolean false

The chart has nothing to draw.

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.

Deliberately separate from empty: "there is nothing to show" and "we failed to fetch it" are two different statements, and a panel that says the first after a failed request is saying something false. Only this one offers a way out, through onRetry.

A truthy loading wins over it, so a retry replaces the message with the spinner rather than leaving the reader wondering whether anything happened.

errorHintLabel string —
errorLabel string —
errorState ReactNode —

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

exportSource ChartExportSource —

The table a ChartExportButton in actions writes when it is given no data of its own: the rows the data table lists, in its order, with the series the legend shows. Every chart of the kit sets it.

height number 240

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

hideTableLabel string —
legend ReactNode —
legendPosition ChartLegendPosition "bottom"
loading boolean false

Dims the plot while fresh data loads. The previous render is held rather than replaced by a spinner, so nothing jumps; the spinner only shows on a first load, when there is nothing to hold.

loadingLabel string —

Accessible name of the loading spinner.

noDataLabel string —
onRetry () => void —

Draws the retry button, and is called when it is pressed.

retryLabel string —
showTableLabel string —
size ChartSize "md"
table ReactNode —

The data table twin, usually a ChartTableView.

tableView boolean | "toggle" false

true keeps the table open, "toggle" puts it behind a button.

title ReactNode —
width number —

Fixes the width instead of measuring the container.

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

Types

Geometry handed to the render prop once the container has been measured.

FieldTypeDefaultDescription
height• number —
measured• boolean —

false while width is still the fallback (server render, first paint).

width• number —