Documentation / Tableaux de bord
ChartContainer
Le cadre dans lequel chaque graphique du kit est dessiné. Il porte tout ce qui est identique d’un graphique à l’autre : la largeur mesurée, la légende (title + description), l’emplacement de la légende, le slot actions, les états de chargement, de vide et d’erreur, et le bouton du tableau de données. Le graphique, lui, ne garde que sa géométrie.
La zone de tracé est une render prop : un graphique ne peut pas se mettre en page avant que le conteneur ait été mesuré. children reçoit { width, height, measured }, measured valant false tant que la largeur est la valeur de repli (rendu serveur, premier affichage). Utilisez ChartContainer pour dessiner une marque que le kit n’offre pas, avec le même cadre et la même accessibilité que les autres panneaux.
Les trois états sont classés et exclusifs : loading l’emporte sur error, qui l’emporte sur empty. Une erreur n’est jamais un vide : seul l’état d’erreur propose une sortie, via onRetry. Le figure est nommé par son titre (ou ariaLabel). Composant client ("use client").
Exemples
Un graphique sur mesure
La fonction enfant reçoit la largeur mesurée et la hauteur demandée (height, 240 px par défaut) ; le titre et la description nomment le figure.
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>
);
}Légende et actions
legend accepte n’importe quel nœud, en général un ChartLegend construit avec toLegendItems ; legendPosition le place. actions s’aligne à droite du titre.
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>
);
}Tableau de données jumeau
table reçoit le tableau, souvent un ChartTableView ; tableView="toggle" le place derrière un bouton, true le laisse ouvert.
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>
);
}Chargement, vide et erreur
Les trois états remplacent la zone de tracé. Un rechargement garde l’ancien tracé estompé ; une nouvelle tentative remplace le message d’erreur par le spinner.
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>
);
}Référence d’API
Composants
Le cadre dans lequel chaque graphique du kit est dessiné.
Il porte ce qui est identique d’un graphique à l’autre — largeur mesurée, intitulé, emplacement de légende, les états de chargement, vide et d’erreur, et le bouton du tableau de données — pour qu’il ne reste à un composant de graphique que sa géométrie.
La zone de tracé est une render prop : un graphique ne peut pas se mettre en page avant que le conteneur ait été mesuré.
import { ChartContainer } from "@synertech/ui";Props — ChartContainerProps
| Prop | Type | Défaut | Description |
|---|---|---|---|
children• |
ReactNode | ((args: ChartContainerRenderArgs) => ReactNode) |
— | |
actions |
ReactNode |
— | Emplacement à droite de la légende — un sélecteur de période, un bouton d’export. |
ariaLabel |
string |
— | Nomme la figure lorsqu’elle n’a pas de titre visible. |
description |
ReactNode |
— | |
empty |
boolean |
false |
Le graphique n’a rien à dessiner. |
emptyState |
ReactNode |
— | Remplace l’état « aucune donnée » : le plus souvent un |
error |
boolean |
false |
Les données n’ont pas pu être chargées. Volontairement distinct de Un |
errorHintLabel |
string |
— | |
errorLabel |
string |
— | |
errorState |
ReactNode |
— | Remplace tout l’état d’erreur intégré, message et bouton compris. |
exportSource |
ChartExportSource |
— | Le tableau qu’écrit un |
height |
number |
240 |
Hauteur de la zone de tracé. La largeur suit toujours le conteneur. |
hideTableLabel |
string |
— | |
legend |
ReactNode |
— | |
legendPosition |
ChartLegendPosition |
"bottom" |
|
loading |
boolean |
false |
Atténue la zone de tracé pendant le chargement de nouvelles données. Le rendu précédent est conservé au lieu d’être remplacé par un spinner, pour que rien ne saute ; le spinner n’apparaît qu’au premier chargement, quand il n’y a rien à conserver. |
loadingLabel |
string |
— | Nom accessible du spinner de chargement. |
noDataLabel |
string |
— | |
onRetry |
() => void |
— | Affiche le bouton « réessayer », et est appelé quand on le presse. |
retryLabel |
string |
— | |
showTableLabel |
string |
— | |
size |
ChartSize |
"md" |
|
table |
ReactNode |
— | Le jumeau en tableau de données, généralement un |
tableView |
boolean | "toggle" |
false |
|
title |
ReactNode |
— | |
width |
number |
— | Fixe la largeur au lieu de mesurer le conteneur. |
+ 277 attributs natifs hérités de HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, gestionnaires d’événements…).
Types
ChartContainerRenderArgs
InterfaceGéométrie transmise à la render prop une fois le conteneur mesuré.
interface ChartContainerRenderArgs| Champ | Type | Défaut | Description |
|---|---|---|---|
height• |
number |
— | |
measured• |
boolean |
— |
|
width• |
number |
— |