Documentation / Dashboards
ChartScaleLegend
The legend of a colour ramp. It is mandatory as soon as a colour encodes a magnitude: without it, a heatmap shows a pattern and states no value. It is what turns “this cell is darker” into “this cell is worth 4,200”.
It takes the same ChartColorScale as the marks, built by createSequentialScale (a magnitude, from light to dark) or createDivergingScale (a deviation on either side of a reference, blue against orange). A continuous ramp is drawn as a gradient and graduated according to ticks; a ramp cut into classes (steps) shows one block per class and labels their bounds. Do not confuse it with ChartLegend, which names series: the categorical colours say which one, the ramps say how much.
Laid out in CSS, with no measurement and no hook: the component stays compatible with server rendering.
Examples
Sequential ramp
label says what the colour measures, with its unit; the numbers under the bar follow format.
import { ChartScaleLegend, createSequentialScale } from "@synertech/ui";
const number = new Intl.NumberFormat("fr-FR");
export function Sequential() {
const scale = createSequentialScale([0, 1_200]);
return (
<ChartScaleLegend
scale={scale}
label="Requêtes par heure"
format={(value) => number.format(value)}
/>
);
}Stepped ramp
With steps, the ramp is cut into classes and the legend labels their bounds; ticks is then ignored.
import { ChartScaleLegend, createSequentialScale } from "@synertech/ui";
export function Stepped() {
const scale = createSequentialScale([0, 100], { steps: 5 });
return <ChartScaleLegend scale={scale} label="Taux d’occupation (%)" />;
}Diverging ramp
Neutral at the centre (center, 0 by default) and symmetrical: a deviation of +5 and one of −5 have the same intensity. The ramp says which side, never whether it is good news.
import { ChartScaleLegend, createDivergingScale } from "@synertech/ui";
const number = new Intl.NumberFormat("fr-FR");
export function Diverging() {
const scale = createDivergingScale([-12, 8]);
return (
<ChartScaleLegend
scale={scale}
label="Écart au budget (%)"
ticks={5}
format={(value) => `${value > 0 ? "+" : ""}${number.format(value)} %`}
/>
);
}With a custom visualisation
The same scale paints the cells and the legend: scale(value) gives the colour, scale.onDark(value) says whether text placed on it should be light.
import { ChartScaleLegend, createSequentialScale, type ChartColorScale } from "@synertech/ui";
const load = [
{ day: "Lun", value: 320 },
{ day: "Mar", value: 540 },
{ day: "Mer", value: 610 },
{ day: "Jeu", value: 880 },
{ day: "Ven", value: 1_240 },
{ day: "Sam", value: 410 },
{ day: "Dim", value: 150 },
];
const number = new Intl.NumberFormat("fr-FR");
function Cell({ scale, day, value }: { scale: ChartColorScale; day: string; value: number }) {
return (
<div
className={`flex h-14 flex-col items-center justify-center rounded text-xs ${
// The pair flips with the theme, in the same step the dark ramps do.
scale.onDark(value) ? "text-gray-50 dark:text-gray-900" : "text-gray-900 dark:text-gray-50"
}`}
style={{ backgroundColor: scale(value) }}
>
<span className="font-medium">{day}</span>
<span className="[font-variant-numeric:tabular-nums]">{number.format(value)}</span>
</div>
);
}
export function CustomHeatStrip() {
const scale = createSequentialScale([0, 1_250]);
return (
<div className="flex flex-col gap-3">
<div className="grid grid-cols-7 gap-1">
{load.map((entry) => (
<Cell key={entry.day} scale={scale} {...entry} />
))}
</div>
<ChartScaleLegend
scale={scale}
label="Commandes par jour"
size="sm"
thickness={8}
ticks={4}
format={(value) => number.format(value)}
/>
</div>
);
}API reference
Components
The legend of a colour ramp.
Mandatory as soon as a colour encodes a magnitude: without it a heat map shows a pattern and states no value. It is the piece that turns "this cell is darker" into "this cell is 4 200".
The bar is laid out in CSS rather than measured, so it needs no container observation and renders on the server.
import { ChartScaleLegend } from "@synertech/ui";Props — ChartScaleLegendProps
| Prop | Type | Default | Description |
|---|---|---|---|
scale• |
ChartColorScale |
— | The ramp the chart paints with — usually the one it built for its marks. |
format |
(value: number) => string |
— | Formats the numbers under the bar. |
label |
ReactNode |
— | What the colour encodes. Say the measure and its unit, not "legend". |
locale |
string |
— | BCP 47 tag the numbers and the dates are written in. Defaults to the language of the runtime. This component renders on the server and reads no context: the |
size |
ChartSize |
"md" |
|
thickness |
number |
10 |
Thickness of the colour bar. |
ticks |
number |
3 |
How many numbers to place. Ignored by a stepped ramp, which labels its classes. |
+ 278 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).