Skip to content
ChartslibraryUI Get Pro

Composition

There are three ways to build something the catalogue doesn't have, from least to most work.

1. Compose charts side by side

Dashboard blocks are ordinary React layouts of charts. The Pro blocks (BudgetVsActualBoard, SaasMetricsBoard, PnlSummary) are written this way, and you can copy their pattern:

import { KpiRow } from "@/components/charts/kpi-row";
import { LineChart } from "@/components/charts/line-chart";
import { DivergingBarChart } from "@/components/charts/diverging-bar-chart";

export function GrowthReview({ kpis, monthly, regions }) {
  return (
    <section style={{ display: "grid", gap: 28 }}>
      <KpiRow items={kpis} />
      <div style={{ display: "grid", gap: 28, gridTemplateColumns: "repeat(auto-fit, minmax(min(100%, 420px), 1fr))" }}>
        <LineChart title="Signups" data={monthly} x="month" series={["signups"]} format="integer" />
        <DivergingBarChart title="Change by region" data={regions} x="region" y="change" format="percent" />
      </div>
    </section>
  );
}

minmax(min(100%, 420px), 1fr) keeps the grid from overflowing on phones.

2. Wrap a chart with your defaults

Thin wrappers keep product-wide conventions in one place:

import { LineChart, type LineChartProps } from "@/components/charts/line-chart";

/** Revenue charts: currency, no zero baseline, our source line. */
export function RevenueLine(props: Omit<LineChartProps, "format">) {
  return <LineChart format={{ kind: "currency", currency: "EUR" }} zero={false} source="Finance data warehouse" {...props} />;
}

3. Build a new chart on the core

Every chart follows the same recipe: validate, describe, frame, draw. Here's a complete minimal lollipop chart:

"use client";
import { ChartFrame, ChartTooltip, useActiveIndex, type FrameProps } from "./core/frame";
import { XLabels, YGrid, axisWidth } from "./core/axis";
import { checkRows, column, type Row } from "./core/data";
import { formatValue, tickFormatter, type FormatInput } from "./core/format";
import { bandScale, linearScale, valueDomain } from "./core/scale";

interface LollipopProps extends FrameProps {
  data: Row[];
  x: string;
  y: string;
  format?: FormatInput;
}

export function LollipopChart({ data, x, y, format, height = 260, ...frame }: LollipopProps) {
  const issues = checkRows(data, { xKey: x, valueKeys: [y] });
  const rows = Array.isArray(data) ? data : [];
  const values = column(rows, y).values;
  const table = {
    columns: [{ key: "x", label: x }, { key: "v", label: y, align: "right" as const, format: (v: unknown) => formatValue(v as number, format) }],
    rows: rows.map((r, i) => ({ x: r[x], v: values[i] })),
  };
  return (
    <ChartFrame {...frame} height={height} issues={issues} table={table} summary={`Lollipop chart of ${y} for ${rows.length} items.`}>
      {({ width }) => <Plot width={width} height={height} labels={rows.map((r) => String(r[x]))} values={values} format={format} />}
    </ChartFrame>
  );
}

function Plot({ width, height, labels, values, format }: { width: number; height: number; labels: string[]; values: (number | null)[]; format?: FormatInput }) {
  const { active, setActive, keyProps } = useActiveIndex(values.length);
  const { domain, ticks } = valueDomain(values.filter((v): v is number => v !== null), { includeZero: true });
  const fmt = tickFormatter(format, ticks);
  const left = axisWidth(ticks.map(fmt));
  const y = linearScale(domain, [height - 24, 10]);
  const band = bandScale(values.length, [left, width - 8]);
  const cx = (i: number) => band(i) + band.bandwidth / 2;
  return (
    <>
      <svg width={width} height={height} role="group" aria-label="Use arrow keys to read values" {...keyProps}>
        <YGrid ticks={ticks} y={y} x0={left} x1={width - 8} format={fmt} />
        <XLabels positions={labels.map((_, i) => cx(i))} labels={labels} y={height - 7} />
        {values.map((v, i) =>
          v === null ? null : (
            <g key={i} onPointerEnter={() => setActive(i)} onPointerLeave={() => setActive(null)}>
              <line x1={cx(i)} x2={cx(i)} y1={y(0)} y2={y(v)} style={{ stroke: "var(--clui-s1)" }} strokeWidth={2} />
              <circle cx={cx(i)} cy={y(v)} r={5} style={{ fill: "var(--clui-s1)", stroke: "var(--clui-surface)" }} strokeWidth={2} />
            </g>
          ),
        )}
      </svg>
      {active !== null && (
        <ChartTooltip x={cx(active)} y={y(values[active] ?? 0)} width={width} height={height} data={{ title: labels[active], rows: [{ label: "Value", value: formatValue(values[active], format, { context: "tooltip" }) }] }} />
      )}
    </>
  );
}

What the frame gives you for free: title, subtitle, legend, table view, CSV export, notes, the error state, the error boundary, the screen-reader summary and responsive width.