React SDK
Custom Charting Library
A report drawn with shadcn, TanStack Charts, Tremor, MUI X Charts, Recharts, Highcharts, Chart.js, D3, AG Charts or Nivo.
The stack you choose picks the code on this page. Options marked coming soon can't be chosen yet.
Copies the Quill CLI's install command: curl -fsSL https://quill.co/install.sh | shView as MarkdownThe whole page, with the code for the chosen stack.
import { useReport } from "@quillsql/react";
// Shared by every example below.
export function useChart(reportId: string) {
const { chart, chartLoading, chartType } = useReport(reportId);
const x = chart?.xAxisField;
const ys = chart?.yAxisFields.map((y) => y.field) ?? [];
const rows = (chart?.rows ?? []).map((row) => ({ ...row, ...Object.fromEntries(ys.map((y) => [y, Number(row[y])])) }));
return { rows, x, ys, chartType, loading: chartLoading };
}import { Area, AreaChart, Bar, BarChart, CartesianGrid, Line, LineChart, Pie, PieChart, XAxis, YAxis } from "recharts";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent } from "@/components/ui/chart";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { useChart } from "./shared";
// Quill chartType → Recharts chart, series and options.
const CHART_TYPES = {
column: { Chart: BarChart, Series: Bar },
bar: { Chart: BarChart, Series: Bar, horizontal: true },
stacked: { Chart: BarChart, Series: Bar, stacked: true },
"stacked bar": { Chart: BarChart, Series: Bar, horizontal: true, stacked: true },
line: { Chart: LineChart, Series: Line },
area: { Chart: AreaChart, Series: Area },
};
export function ShadcnReport({ reportId, name }: { reportId: string; name: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
// Each series is a --color-<field> variable, set by ChartContainer.
const config = Object.fromEntries(ys.map((y, i) => [y, { label: y, color: `var(--chart-${(i % 5) + 1})` }]));
switch (loading ? "loading" : chartType) {
case "loading":
return <Card><CardHeader><CardDescription>{name}</CardDescription><CardTitle>Loading…</CardTitle></CardHeader></Card>;
case "metric":
return (
<Card>
<CardHeader>
<CardDescription>{name}</CardDescription>
<CardTitle className="text-2xl font-semibold tabular-nums">{rows[0]?.[ys[0]]?.toLocaleString()}</CardTitle>
</CardHeader>
</Card>
);
case "table":
return (
<Card>
<CardHeader><CardTitle>{name}</CardTitle></CardHeader>
<CardContent>
<Table>
<TableHeader><TableRow>{[x, ...ys].map((f, j) => <TableHead key={j}>{f}</TableHead>)}</TableRow></TableHeader>
<TableBody>
{rows.map((row, i) => <TableRow key={i}>{[x, ...ys].map((f, j) => <TableCell key={j}>{row[f]?.toLocaleString()}</TableCell>)}</TableRow>)}
</TableBody>
</Table>
</CardContent>
</Card>
);
case "pie":
return (
<Card>
<CardHeader><CardTitle>{name}</CardTitle></CardHeader>
<CardContent>
<ChartContainer config={config} className="mx-auto aspect-square max-h-[250px]">
<PieChart>
<ChartTooltip content={<ChartTooltipContent hideLabel />} />
<Pie data={rows.map((r, i) => ({ ...r, fill: `var(--chart-${(i % 5) + 1})` }))} dataKey={ys[0]} nameKey={x} />
</PieChart>
</ChartContainer>
</CardContent>
</Card>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return null;
const { Chart, Series, horizontal, stacked } = type;
return (
<Card>
<CardHeader><CardTitle>{name}</CardTitle></CardHeader>
<CardContent>
<ChartContainer config={config}>
<Chart accessibilityLayer data={rows} layout={horizontal ? "vertical" : "horizontal"}>
<CartesianGrid vertical={!!horizontal} horizontal={!horizontal} />
{horizontal
? <><YAxis dataKey={x} type="category" tickLine={false} axisLine={false} width={90} /><XAxis type="number" hide /></>
: <XAxis dataKey={x} tickLine={false} axisLine={false} tickMargin={10} />}
<ChartTooltip cursor={false} content={<ChartTooltipContent />} />
{ys.length > 1 && <ChartLegend content={<ChartLegendContent />} />}
{ys.map((y) => (
<Series key={y} dataKey={y} stackId={stacked ? "a" : undefined} fill={`var(--color-${y})`} stroke={`var(--color-${y})`}
{...(Series === Bar ? { radius: stacked ? 0 : 8 } : { type: "natural", strokeWidth: 2, dot: false, fillOpacity: 0.4 })} />
))}
</Chart>
</ChartContainer>
</CardContent>
</Card>
);
}
}
}import { Chart } from "react-charts";
import { useChart } from "./shared";
// TanStack Charts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]}</td>)}</tr>)}
</tbody>
</table>
);
// Quill chartType → TanStack's elementType and axis options. No pie.
const CHART_TYPES = {
column: { elementType: "bar" },
bar: { elementType: "bar", horizontal: true },
stacked: { elementType: "bar", stacked: true },
"stacked bar": { elementType: "bar", horizontal: true, stacked: true },
line: { elementType: "line" },
area: { elementType: "area" },
};
export const TanstackReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="tanstack"><h3>{name}</h3><TanstackChart reportId={reportId} /></section>
);
function TanstackChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return (
<div style={{ height: 240 }}>
<Chart
options={{
data: ys.map((y) => ({ label: y, data: rows.map((r) => ({ x: r[x], y: r[y] })) })),
primaryAxis: { getValue: (d) => d.x, position: type.horizontal ? "left" : "bottom" },
secondaryAxes: [{ getValue: (d) => d.y, elementType: type.elementType, stacked: type.stacked, position: type.horizontal ? "bottom" : "left" }],
}}
/>
</div>
);
}
}
}import { AreaChart, BarChart, Card, DonutChart, LineChart, Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from "@tremor/react";
import { useChart } from "./shared";
// Quill chartType → Tremor chart and props.
const CHART_TYPES = {
column: { Chart: BarChart },
bar: { Chart: BarChart, layout: "vertical" },
stacked: { Chart: BarChart, stack: true },
"stacked bar": { Chart: BarChart, layout: "vertical", stack: true },
line: { Chart: LineChart },
area: { Chart: AreaChart },
};
const Title = ({ children }) => <h3 className="text-tremor-default font-medium text-tremor-content-strong">{children}</h3>;
export function TremorReport({ reportId, name }: { reportId: string; name: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading":
return <Card><Title>{name}</Title><p className="mt-2 text-tremor-default text-tremor-content">Loading…</p></Card>;
case "metric":
return (
<Card>
<p className="text-tremor-default text-tremor-content">{name}</p>
<p className="text-tremor-metric font-semibold text-tremor-content-strong">{rows[0]?.[ys[0]]?.toLocaleString()}</p>
</Card>
);
case "table":
return (
<Card>
<Title>{name}</Title>
<Table className="mt-4">
<TableHead><TableRow>{[x, ...ys].map((f, j) => <TableHeaderCell key={j}>{f}</TableHeaderCell>)}</TableRow></TableHead>
<TableBody>
{rows.map((row, i) => <TableRow key={i}>{[x, ...ys].map((f, j) => <TableCell key={j}>{row[f]?.toLocaleString()}</TableCell>)}</TableRow>)}
</TableBody>
</Table>
</Card>
);
case "pie":
return <Card><Title>{name}</Title><DonutChart className="mt-6 h-60" data={rows} index={x} category={ys[0]} variant="pie" /></Card>;
default: {
const type = CHART_TYPES[chartType];
if (!type) return null;
const { Chart, ...props } = type;
return <Card><Title>{name}</Title><Chart className="mt-6 h-60" data={rows} index={x} categories={ys} yAxisWidth={props.layout ? 90 : 56} {...props} /></Card>;
}
}
}import { Area, AreaChart, Bar, BarChart, CartesianGrid, Legend, Line, LineChart, Pie, PieChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";
import { useChart } from "./shared";
// Recharts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
// Quill chartType → Recharts chart, series and options.
const CHART_TYPES = {
column: { Chart: BarChart, Series: Bar },
bar: { Chart: BarChart, Series: Bar, horizontal: true },
stacked: { Chart: BarChart, Series: Bar, stacked: true },
"stacked bar": { Chart: BarChart, Series: Bar, horizontal: true, stacked: true },
line: { Chart: LineChart, Series: Line },
area: { Chart: AreaChart, Series: Area },
};
export const RechartsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><RechartsChart reportId={reportId} /></section>
);
function RechartsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<ResponsiveContainer width="100%" height={240}>
<PieChart>
<Tooltip />
<Pie data={rows.map((r, i) => ({ ...r, fill: COLORS[i % COLORS.length] }))} dataKey={ys[0]} nameKey={x} />
</PieChart>
</ResponsiveContainer>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
const { Chart, Series, horizontal, stacked } = type;
return (
<ResponsiveContainer width="100%" height={240}>
<Chart data={rows} layout={horizontal ? "vertical" : "horizontal"}>
<CartesianGrid strokeDasharray="3 3" vertical={!!horizontal} horizontal={!horizontal} />
{horizontal
? <><YAxis dataKey={x} type="category" width={90} /><XAxis type="number" tickFormatter={(v) => v.toLocaleString()} /></>
: <><XAxis dataKey={x} /><YAxis width={64} tickFormatter={(v) => v.toLocaleString()} /></>}
<Tooltip />
{ys.length > 1 && <Legend />}
{ys.map((y, i) => (
<Series key={y} dataKey={y} stackId={stacked ? "a" : undefined} fill={COLORS[i % COLORS.length]} stroke={COLORS[i % COLORS.length]}
{...(Series === Bar ? {} : { type: "monotone", dot: false, fillOpacity: 0.4 })} />
))}
</Chart>
</ResponsiveContainer>
);
}
}
}import Highcharts from "highcharts";
import { HighchartsReact } from "highcharts-react-official";
import { useChart } from "./shared";
// Highcharts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
// Quill chartType → Highcharts series type and stacking.
const CHART_TYPES = {
column: { type: "column" },
bar: { type: "bar" },
stacked: { type: "column", stacking: "normal" },
"stacked bar": { type: "bar", stacking: "normal" },
line: { type: "line" },
area: { type: "area" },
};
export const HighchartsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><HighchartsChart reportId={reportId} /></section>
);
function HighchartsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<HighchartsReact highcharts={Highcharts} options={{
chart: { type: "pie", height: 240 },
title: { text: undefined },
series: [{ type: "pie", name: ys[0], data: rows.map((r) => ({ name: String(r[x]), y: r[ys[0]] })) }],
}} />
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return (
<HighchartsReact highcharts={Highcharts} options={{
chart: { type: type.type, height: 240 },
title: { text: undefined },
xAxis: { categories: rows.map((r) => String(r[x])) },
yAxis: { title: { text: undefined } },
legend: { enabled: ys.length > 1 },
plotOptions: { series: { stacking: type.stacking, marker: { enabled: false } } },
series: ys.map((y) => ({ type: type.type, name: y, data: rows.map((r) => r[y]) })),
}} />
);
}
}
}import { ArcElement, BarElement, CategoryScale, Chart as ChartJS, Filler, Legend, LinearScale, LineElement, PointElement, Tooltip } from "chart.js";
import { Bar, Line, Pie } from "react-chartjs-2";
import { useChart } from "./shared";
ChartJS.register(ArcElement, BarElement, CategoryScale, Filler, Legend, LinearScale, LineElement, PointElement, Tooltip);
// Chart.js draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
// Quill chartType → react-chartjs-2 chart and options.
const CHART_TYPES = {
column: { Chart: Bar },
bar: { Chart: Bar, horizontal: true },
stacked: { Chart: Bar, stacked: true },
"stacked bar": { Chart: Bar, horizontal: true, stacked: true },
line: { Chart: Line },
area: { Chart: Line, fill: true },
};
export const ChartjsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><ChartjsChart reportId={reportId} /></section>
);
function ChartjsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
const labels = rows.map((r) => r[x]);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<div style={{ height: 240 }}>
<Pie data={{ labels, datasets: [{ data: rows.map((r) => r[ys[0]]), backgroundColor: COLORS }] }} options={{ maintainAspectRatio: false }} />
</div>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
const { Chart, horizontal, stacked, fill } = type;
const datasets = ys.map((y, i) => ({
label: y,
data: rows.map((r) => r[y]),
borderColor: COLORS[i % COLORS.length],
backgroundColor: fill ? `${COLORS[i % COLORS.length]}66` : COLORS[i % COLORS.length],
fill,
tension: 0.3,
pointRadius: 0,
}));
return (
<div style={{ height: 240 }}>
<Chart data={{ labels, datasets }} options={{
maintainAspectRatio: false,
indexAxis: horizontal ? "y" : "x",
plugins: { legend: { display: ys.length > 1 } },
scales: { x: { stacked }, y: { stacked } },
}} />
</div>
);
}
}
}import { Card, CardContent, Typography } from "@mui/material";
import { BarChart, LineChart, PieChart } from "@mui/x-charts";
import { DataGrid } from "@mui/x-data-grid";
import { useChart } from "./shared";
// Quill chartType → MUI chart and options.
const CHART_TYPES = {
column: { Chart: BarChart },
bar: { Chart: BarChart, horizontal: true },
stacked: { Chart: BarChart, stacked: true },
"stacked bar": { Chart: BarChart, horizontal: true, stacked: true },
line: { Chart: LineChart },
area: { Chart: LineChart, area: true },
};
const Panel = ({ name, children }) => (
<Card variant="outlined">
<CardContent>
<Typography component="h2" variant="subtitle2" gutterBottom>{name}</Typography>
{children}
</CardContent>
</Card>
);
export function MuiReport({ reportId, name }: { reportId: string; name: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <Panel name={name}><Typography color="text.secondary">Loading…</Typography></Panel>;
case "metric": return <Panel name={name}><Typography variant="h4" component="p">{rows[0]?.[ys[0]]?.toLocaleString()}</Typography></Panel>;
case "table":
return (
<Panel name={name}>
<DataGrid rows={rows.map((r, id) => ({ id, ...r }))} columns={[x, ...ys].map((f) => ({ field: f, headerName: f, flex: 1 }))}
density="compact" hideFooter disableColumnMenu />
</Panel>
);
case "pie":
return (
<Panel name={name}>
<PieChart series={[{ data: rows.map((r, id) => ({ id, label: r[x], value: r[ys[0]] })), innerRadius: 60, paddingAngle: 2, cornerRadius: 4 }]} height={250} />
</Panel>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return null;
const { Chart, horizontal, stacked, area } = type;
const axis = [{ scaleType: Chart === LineChart ? "point" : "band", dataKey: x, width: 90 }];
const series = ys.map((y) => ({ dataKey: y, label: y, stack: stacked ? "a" : undefined, area, showMark: false }));
return (
<Panel name={name}>
<Chart dataset={rows} series={series} height={250} grid={horizontal ? { vertical: true } : { horizontal: true }}
{...(horizontal ? { yAxis: axis, layout: "horizontal" } : { xAxis: axis, yAxis: [{ width: 60 }] })} />
</Panel>
);
}
}
}import { scaleBand, scaleLinear, scalePoint } from "d3-scale";
import { arc, area, line, pie, stack } from "d3-shape";
import { useChart } from "./shared";
// D3 computes the scales and shapes, and React draws them as SVG. The metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
const [W, H] = [360, 240];
// Quill chartType → what the chart draws.
const CHART_TYPES = {
column: { mark: "bar" },
bar: { mark: "bar", horizontal: true },
stacked: { mark: "bar", stacked: true },
"stacked bar": { mark: "bar", horizontal: true, stacked: true },
line: { mark: "line" },
area: { mark: "area" },
};
export const D3Report = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><D3Chart reportId={reportId} /></section>
);
function D3Chart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie": {
const slices = pie().value((r) => r[ys[0]]).sort(null)(rows);
const shape = arc().innerRadius(0).outerRadius(110);
return (
<svg viewBox="-120 -120 240 240" height={H} width="100%" role="img">
{slices.map((s, i) => <path key={i} d={shape(s)} fill={COLORS[i % COLORS.length]}><title>{`${rows[i][x]}: ${rows[i][ys[0]]}`}</title></path>)}
</svg>
);
}
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return <Cartesian rows={rows} x={x} ys={ys} {...type} />;
}
}
}
function Cartesian({ rows, x, ys, mark, horizontal = false, stacked = false }) {
const m = { top: 8, right: horizontal ? 24 : 8, bottom: 24, left: horizontal ? 90 : 48 };
// Each series' [from, to] for each row: on top of the series before it, or from zero.
const layers = stacked ? stack().keys(ys)(rows) : ys.map((y) => rows.map((r) => [0, r[y]]));
const cats = rows.map((r) => String(r[x]));
const band = scaleBand(cats, horizontal ? [m.top, H - m.bottom] : [m.left, W - m.right]).padding(0.2);
const point = scalePoint(cats, [m.left, W - m.right]);
const side = scaleBand(ys, [0, band.bandwidth()]).padding(0.1);
const value = scaleLinear([0, Math.max(1, ...layers.flatMap((l) => l.map((d) => d[1])))], horizontal ? [m.left, W - m.right] : [H - m.bottom, m.top]).nice();
// About four category labels along the bottom, so they don't run into each other.
const step = horizontal ? 1 : Math.ceil(cats.length / 4);
return (
<>
<svg viewBox={`0 0 ${W} ${H}`} width="100%" role="img" fontSize={10} fill="#71717a">
{value.ticks(4).map((t) => horizontal
? <g key={t}><line x1={value(t)} x2={value(t)} y1={m.top} y2={H - m.bottom} stroke="#f4f4f5" /><text x={value(t)} y={H - 6} textAnchor="middle">{t.toLocaleString()}</text></g>
: <g key={t}><line x1={m.left} x2={W - m.right} y1={value(t)} y2={value(t)} stroke="#f4f4f5" /><text x={m.left - 6} y={value(t)} dy="0.32em" textAnchor="end">{t.toLocaleString()}</text></g>)}
{layers.map((layer, i) => {
const color = COLORS[i % COLORS.length];
if (mark === "line") return <path key={i} d={line((_, j) => point(cats[j]), (d) => value(d[1]))(layer)} fill="none" stroke={color} strokeWidth={2} />;
if (mark === "area") return <path key={i} d={area((_, j) => point(cats[j]), (d) => value(d[0]), (d) => value(d[1]))(layer)} fill={color} fillOpacity={0.4} stroke={color} />;
return layer.map((d, j) => {
const at = band(cats[j]) + (stacked ? 0 : side(ys[i]));
const size = stacked ? band.bandwidth() : side.bandwidth();
const [from, to] = [value(d[0]), value(d[1])];
return horizontal
? <rect key={j} x={Math.min(from, to)} y={at} width={Math.abs(to - from)} height={size} fill={color} />
: <rect key={j} x={at} y={Math.min(from, to)} width={size} height={Math.abs(to - from)} fill={color} />;
});
})}
{cats.map((c, j) => (j % step ? null : horizontal
? <text key={c} x={m.left - 6} y={band(c) + band.bandwidth() / 2} dy="0.32em" textAnchor="end">{c}</text>
: <text key={c} x={mark === "bar" ? band(c) + band.bandwidth() / 2 : point(c)} y={H - 6} textAnchor="middle">{c}</text>))}
</svg>
{ys.length > 1 && <p className="legend">{ys.map((y, i) => <span key={y}><i style={{ background: COLORS[i % COLORS.length] }} />{y}</span>)}</p>}
</>
);
}import { AllCommunityModule, ModuleRegistry } from "ag-charts-community";
import { AgCharts } from "ag-charts-react";
import { useChart } from "./shared";
ModuleRegistry.registerModules([AllCommunityModule]);
// AG Charts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
// Quill chartType → AG Charts series options.
const CHART_TYPES = {
column: { type: "bar" },
bar: { type: "bar", direction: "horizontal" },
stacked: { type: "bar", stacked: true },
"stacked bar": { type: "bar", direction: "horizontal", stacked: true },
line: { type: "line", marker: { enabled: false } },
area: { type: "area" },
};
export const AgChartsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><AgChartsChart reportId={reportId} /></section>
);
function AgChartsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return <AgCharts style={{ height: 240 }} options={{ data: rows, series: [{ type: "pie", angleKey: ys[0], legendItemKey: x }] }} />;
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return (
<AgCharts style={{ height: 240 }} options={{
data: rows,
series: ys.map((y) => ({ ...type, xKey: x, yKey: y, yName: y })),
// The number axis: along the bottom (x) when the bars run across.
axes: { [type.direction === "horizontal" ? "x" : "y"]: { type: "number", label: { formatter: ({ value }) => value.toLocaleString() } } },
legend: { enabled: ys.length > 1 },
}} />
);
}
}
}import { ResponsiveBar } from "@nivo/bar";
import { ResponsiveLine } from "@nivo/line";
import { ResponsivePie } from "@nivo/pie";
import { useChart } from "./shared";
// Nivo draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
const NUMBER = ">-,.0f";
// Quill chartType → Nivo chart and props.
const CHART_TYPES = {
column: { Chart: ResponsiveBar, groupMode: "grouped" },
bar: { Chart: ResponsiveBar, groupMode: "grouped", layout: "horizontal" },
stacked: { Chart: ResponsiveBar, groupMode: "stacked" },
"stacked bar": { Chart: ResponsiveBar, groupMode: "stacked", layout: "horizontal" },
line: { Chart: ResponsiveLine },
area: { Chart: ResponsiveLine, enableArea: true },
};
export const NivoReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><NivoChart reportId={reportId} /></section>
);
function NivoChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
// About four labels along the bottom, so they don't run into each other.
const ticks = rows.map((r) => String(r[x])).filter((_, i, all) => i % Math.ceil(all.length / 4) === 0);
const legends = ys.length > 1 ? [{ anchor: "bottom", direction: "row", translateY: 56, itemWidth: 84, itemHeight: 14, symbolSize: 10 }] : [];
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<div style={{ height: 240 }}>
<ResponsivePie data={rows.map((r) => ({ id: String(r[x]), value: r[ys[0]] }))} colors={COLORS} valueFormat={NUMBER}
margin={{ top: 24, right: 80, bottom: 24, left: 80 }} enableArcLabels={false} arcLinkLabelsSkipAngle={8} />
</div>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
const { Chart, layout, ...props } = type;
const margin = { top: 8, right: 16, bottom: ys.length > 1 ? 64 : 32, left: layout ? 96 : 56 };
return (
<div style={{ height: ys.length > 1 ? 272 : 240 }}>
{Chart === ResponsiveBar ? (
<ResponsiveBar data={layout ? [...rows].reverse() : rows} keys={ys} indexBy={x} layout={layout ?? "vertical"} colors={COLORS}
margin={margin} padding={0.25} enableLabel={false} valueFormat={NUMBER} legends={legends.map((l) => ({ ...l, dataFrom: "keys" }))}
axisLeft={layout ? {} : { format: NUMBER, tickValues: 5 }} axisBottom={layout ? { format: NUMBER, tickValues: 5 } : { tickValues: ticks }}
gridYValues={layout ? undefined : 5} gridXValues={layout ? 5 : undefined} {...props} />
) : (
<ResponsiveLine data={ys.map((y) => ({ id: y, data: rows.map((r) => ({ x: String(r[x]), y: r[y] })) }))} colors={COLORS}
margin={margin} xScale={{ type: "point" }} yScale={{ type: "linear", min: 0 }} enablePoints={false} useMesh
yFormat={NUMBER} axisLeft={{ format: NUMBER, tickValues: 5 }} gridYValues={5} enableGridX={false} axisBottom={{ tickValues: ticks }}
legends={legends} {...props} />
)}
</div>
);
}
}
}import { useReport } from "@quillsql/react";
// Shared by every example.
export function useChart(reportId: string) {
const { chart, chartLoading, chartType } = useReport(reportId);
const x = chart?.xAxisField;
const ys = chart?.yAxisFields.map((y) => y.field) ?? [];
const rows = (chart?.rows ?? []).map((row) => ({ ...row, ...Object.fromEntries(ys.map((y) => [y, Number(row[y])])) }));
return { rows, x, ys, chartType, loading: chartLoading };
}import { Area, AreaChart, Bar, BarChart, CartesianGrid, Line, LineChart, Pie, PieChart, XAxis, YAxis } from "recharts";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent } from "@/components/ui/chart";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
// Quill chartType → Recharts chart, series and options.
const CHART_TYPES = {
column: { Chart: BarChart, Series: Bar },
bar: { Chart: BarChart, Series: Bar, horizontal: true },
stacked: { Chart: BarChart, Series: Bar, stacked: true },
"stacked bar": { Chart: BarChart, Series: Bar, horizontal: true, stacked: true },
line: { Chart: LineChart, Series: Line },
area: { Chart: AreaChart, Series: Area },
};
export function ShadcnReport({ reportId, name }: { reportId: string; name: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
// Each series is a --color-<field> variable, set by ChartContainer.
const config = Object.fromEntries(ys.map((y, i) => [y, { label: y, color: `var(--chart-${(i % 5) + 1})` }]));
switch (loading ? "loading" : chartType) {
case "loading":
return <Card><CardHeader><CardDescription>{name}</CardDescription><CardTitle>Loading…</CardTitle></CardHeader></Card>;
case "metric":
return (
<Card>
<CardHeader>
<CardDescription>{name}</CardDescription>
<CardTitle className="text-2xl font-semibold tabular-nums">{rows[0]?.[ys[0]]?.toLocaleString()}</CardTitle>
</CardHeader>
</Card>
);
case "table":
return (
<Card>
<CardHeader><CardTitle>{name}</CardTitle></CardHeader>
<CardContent>
<Table>
<TableHeader><TableRow>{[x, ...ys].map((f, j) => <TableHead key={j}>{f}</TableHead>)}</TableRow></TableHeader>
<TableBody>
{rows.map((row, i) => <TableRow key={i}>{[x, ...ys].map((f, j) => <TableCell key={j}>{row[f]?.toLocaleString()}</TableCell>)}</TableRow>)}
</TableBody>
</Table>
</CardContent>
</Card>
);
case "pie":
return (
<Card>
<CardHeader><CardTitle>{name}</CardTitle></CardHeader>
<CardContent>
<ChartContainer config={config} className="mx-auto aspect-square max-h-[250px]">
<PieChart>
<ChartTooltip content={<ChartTooltipContent hideLabel />} />
<Pie data={rows.map((r, i) => ({ ...r, fill: `var(--chart-${(i % 5) + 1})` }))} dataKey={ys[0]} nameKey={x} />
</PieChart>
</ChartContainer>
</CardContent>
</Card>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return null;
const { Chart, Series, horizontal, stacked } = type;
return (
<Card>
<CardHeader><CardTitle>{name}</CardTitle></CardHeader>
<CardContent>
<ChartContainer config={config}>
<Chart accessibilityLayer data={rows} layout={horizontal ? "vertical" : "horizontal"}>
<CartesianGrid vertical={!!horizontal} horizontal={!horizontal} />
{horizontal
? <><YAxis dataKey={x} type="category" tickLine={false} axisLine={false} width={90} /><XAxis type="number" hide /></>
: <XAxis dataKey={x} tickLine={false} axisLine={false} tickMargin={10} />}
<ChartTooltip cursor={false} content={<ChartTooltipContent />} />
{ys.length > 1 && <ChartLegend content={<ChartLegendContent />} />}
{ys.map((y) => (
<Series key={y} dataKey={y} stackId={stacked ? "a" : undefined} fill={`var(--color-${y})`} stroke={`var(--color-${y})`}
{...(Series === Bar ? { radius: stacked ? 0 : 8 } : { type: "natural", strokeWidth: 2, dot: false, fillOpacity: 0.4 })} />
))}
</Chart>
</ChartContainer>
</CardContent>
</Card>
);
}
}
}import { Chart } from "react-charts";
// TanStack Charts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]}</td>)}</tr>)}
</tbody>
</table>
);
// Quill chartType → TanStack's elementType and axis options. No pie.
const CHART_TYPES = {
column: { elementType: "bar" },
bar: { elementType: "bar", horizontal: true },
stacked: { elementType: "bar", stacked: true },
"stacked bar": { elementType: "bar", horizontal: true, stacked: true },
line: { elementType: "line" },
area: { elementType: "area" },
};
export const TanstackReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="tanstack"><h3>{name}</h3><TanstackChart reportId={reportId} /></section>
);
function TanstackChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return (
<div style={{ height: 240 }}>
<Chart
options={{
data: ys.map((y) => ({ label: y, data: rows.map((r) => ({ x: r[x], y: r[y] })) })),
primaryAxis: { getValue: (d) => d.x, position: type.horizontal ? "left" : "bottom" },
secondaryAxes: [{ getValue: (d) => d.y, elementType: type.elementType, stacked: type.stacked, position: type.horizontal ? "bottom" : "left" }],
}}
/>
</div>
);
}
}
}import { AreaChart, BarChart, Card, DonutChart, LineChart, Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow } from "@tremor/react";
// Quill chartType → Tremor chart and props.
const CHART_TYPES = {
column: { Chart: BarChart },
bar: { Chart: BarChart, layout: "vertical" },
stacked: { Chart: BarChart, stack: true },
"stacked bar": { Chart: BarChart, layout: "vertical", stack: true },
line: { Chart: LineChart },
area: { Chart: AreaChart },
};
const Title = ({ children }) => <h3 className="text-tremor-default font-medium text-tremor-content-strong">{children}</h3>;
export function TremorReport({ reportId, name }: { reportId: string; name: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading":
return <Card><Title>{name}</Title><p className="mt-2 text-tremor-default text-tremor-content">Loading…</p></Card>;
case "metric":
return (
<Card>
<p className="text-tremor-default text-tremor-content">{name}</p>
<p className="text-tremor-metric font-semibold text-tremor-content-strong">{rows[0]?.[ys[0]]?.toLocaleString()}</p>
</Card>
);
case "table":
return (
<Card>
<Title>{name}</Title>
<Table className="mt-4">
<TableHead><TableRow>{[x, ...ys].map((f, j) => <TableHeaderCell key={j}>{f}</TableHeaderCell>)}</TableRow></TableHead>
<TableBody>
{rows.map((row, i) => <TableRow key={i}>{[x, ...ys].map((f, j) => <TableCell key={j}>{row[f]?.toLocaleString()}</TableCell>)}</TableRow>)}
</TableBody>
</Table>
</Card>
);
case "pie":
return <Card><Title>{name}</Title><DonutChart className="mt-6 h-60" data={rows} index={x} category={ys[0]} variant="pie" /></Card>;
default: {
const type = CHART_TYPES[chartType];
if (!type) return null;
const { Chart, ...props } = type;
return <Card><Title>{name}</Title><Chart className="mt-6 h-60" data={rows} index={x} categories={ys} yAxisWidth={props.layout ? 90 : 56} {...props} /></Card>;
}
}
}import { Card, CardContent, Typography } from "@mui/material";
import { BarChart, LineChart, PieChart } from "@mui/x-charts";
import { DataGrid } from "@mui/x-data-grid";
// Quill chartType → MUI chart and options.
const CHART_TYPES = {
column: { Chart: BarChart },
bar: { Chart: BarChart, horizontal: true },
stacked: { Chart: BarChart, stacked: true },
"stacked bar": { Chart: BarChart, horizontal: true, stacked: true },
line: { Chart: LineChart },
area: { Chart: LineChart, area: true },
};
const Panel = ({ name, children }) => (
<Card variant="outlined">
<CardContent>
<Typography component="h2" variant="subtitle2" gutterBottom>{name}</Typography>
{children}
</CardContent>
</Card>
);
export function MuiReport({ reportId, name }: { reportId: string; name: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <Panel name={name}><Typography color="text.secondary">Loading…</Typography></Panel>;
case "metric": return <Panel name={name}><Typography variant="h4" component="p">{rows[0]?.[ys[0]]?.toLocaleString()}</Typography></Panel>;
case "table":
return (
<Panel name={name}>
<DataGrid rows={rows.map((r, id) => ({ id, ...r }))} columns={[x, ...ys].map((f) => ({ field: f, headerName: f, flex: 1 }))}
density="compact" hideFooter disableColumnMenu />
</Panel>
);
case "pie":
return (
<Panel name={name}>
<PieChart series={[{ data: rows.map((r, id) => ({ id, label: r[x], value: r[ys[0]] })), innerRadius: 60, paddingAngle: 2, cornerRadius: 4 }]} height={250} />
</Panel>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return null;
const { Chart, horizontal, stacked, area } = type;
const axis = [{ scaleType: Chart === LineChart ? "point" : "band", dataKey: x, width: 90 }];
const series = ys.map((y) => ({ dataKey: y, label: y, stack: stacked ? "a" : undefined, area, showMark: false }));
return (
<Panel name={name}>
<Chart dataset={rows} series={series} height={250} grid={horizontal ? { vertical: true } : { horizontal: true }}
{...(horizontal ? { yAxis: axis, layout: "horizontal" } : { xAxis: axis, yAxis: [{ width: 60 }] })} />
</Panel>
);
}
}
}import { Area, AreaChart, Bar, BarChart, CartesianGrid, Legend, Line, LineChart, Pie, PieChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";
// Recharts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
// Quill chartType → Recharts chart, series and options.
const CHART_TYPES = {
column: { Chart: BarChart, Series: Bar },
bar: { Chart: BarChart, Series: Bar, horizontal: true },
stacked: { Chart: BarChart, Series: Bar, stacked: true },
"stacked bar": { Chart: BarChart, Series: Bar, horizontal: true, stacked: true },
line: { Chart: LineChart, Series: Line },
area: { Chart: AreaChart, Series: Area },
};
export const RechartsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><RechartsChart reportId={reportId} /></section>
);
function RechartsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<ResponsiveContainer width="100%" height={240}>
<PieChart>
<Tooltip />
<Pie data={rows.map((r, i) => ({ ...r, fill: COLORS[i % COLORS.length] }))} dataKey={ys[0]} nameKey={x} />
</PieChart>
</ResponsiveContainer>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
const { Chart, Series, horizontal, stacked } = type;
return (
<ResponsiveContainer width="100%" height={240}>
<Chart data={rows} layout={horizontal ? "vertical" : "horizontal"}>
<CartesianGrid strokeDasharray="3 3" vertical={!!horizontal} horizontal={!horizontal} />
{horizontal
? <><YAxis dataKey={x} type="category" width={90} /><XAxis type="number" tickFormatter={(v) => v.toLocaleString()} /></>
: <><XAxis dataKey={x} /><YAxis width={64} tickFormatter={(v) => v.toLocaleString()} /></>}
<Tooltip />
{ys.length > 1 && <Legend />}
{ys.map((y, i) => (
<Series key={y} dataKey={y} stackId={stacked ? "a" : undefined} fill={COLORS[i % COLORS.length]} stroke={COLORS[i % COLORS.length]}
{...(Series === Bar ? {} : { type: "monotone", dot: false, fillOpacity: 0.4 })} />
))}
</Chart>
</ResponsiveContainer>
);
}
}
}import Highcharts from "highcharts";
import { HighchartsReact } from "highcharts-react-official";
// Highcharts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
// Quill chartType → Highcharts series type and stacking.
const CHART_TYPES = {
column: { type: "column" },
bar: { type: "bar" },
stacked: { type: "column", stacking: "normal" },
"stacked bar": { type: "bar", stacking: "normal" },
line: { type: "line" },
area: { type: "area" },
};
export const HighchartsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><HighchartsChart reportId={reportId} /></section>
);
function HighchartsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<HighchartsReact highcharts={Highcharts} options={{
chart: { type: "pie", height: 240 },
title: { text: undefined },
series: [{ type: "pie", name: ys[0], data: rows.map((r) => ({ name: String(r[x]), y: r[ys[0]] })) }],
}} />
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return (
<HighchartsReact highcharts={Highcharts} options={{
chart: { type: type.type, height: 240 },
title: { text: undefined },
xAxis: { categories: rows.map((r) => String(r[x])) },
yAxis: { title: { text: undefined } },
legend: { enabled: ys.length > 1 },
plotOptions: { series: { stacking: type.stacking, marker: { enabled: false } } },
series: ys.map((y) => ({ type: type.type, name: y, data: rows.map((r) => r[y]) })),
}} />
);
}
}
}import { ArcElement, BarElement, CategoryScale, Chart as ChartJS, Filler, Legend, LinearScale, LineElement, PointElement, Tooltip } from "chart.js";
import { Bar, Line, Pie } from "react-chartjs-2";
ChartJS.register(ArcElement, BarElement, CategoryScale, Filler, Legend, LinearScale, LineElement, PointElement, Tooltip);
// Chart.js draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
// Quill chartType → react-chartjs-2 chart and options.
const CHART_TYPES = {
column: { Chart: Bar },
bar: { Chart: Bar, horizontal: true },
stacked: { Chart: Bar, stacked: true },
"stacked bar": { Chart: Bar, horizontal: true, stacked: true },
line: { Chart: Line },
area: { Chart: Line, fill: true },
};
export const ChartjsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><ChartjsChart reportId={reportId} /></section>
);
function ChartjsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
const labels = rows.map((r) => r[x]);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<div style={{ height: 240 }}>
<Pie data={{ labels, datasets: [{ data: rows.map((r) => r[ys[0]]), backgroundColor: COLORS }] }} options={{ maintainAspectRatio: false }} />
</div>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
const { Chart, horizontal, stacked, fill } = type;
const datasets = ys.map((y, i) => ({
label: y,
data: rows.map((r) => r[y]),
borderColor: COLORS[i % COLORS.length],
backgroundColor: fill ? `${COLORS[i % COLORS.length]}66` : COLORS[i % COLORS.length],
fill,
tension: 0.3,
pointRadius: 0,
}));
return (
<div style={{ height: 240 }}>
<Chart data={{ labels, datasets }} options={{
maintainAspectRatio: false,
indexAxis: horizontal ? "y" : "x",
plugins: { legend: { display: ys.length > 1 } },
scales: { x: { stacked }, y: { stacked } },
}} />
</div>
);
}
}
}import { scaleBand, scaleLinear, scalePoint } from "d3-scale";
import { arc, area, line, pie, stack } from "d3-shape";
// D3 computes the scales and shapes, and React draws them as SVG. The metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
const [W, H] = [360, 240];
// Quill chartType → what the chart draws.
const CHART_TYPES = {
column: { mark: "bar" },
bar: { mark: "bar", horizontal: true },
stacked: { mark: "bar", stacked: true },
"stacked bar": { mark: "bar", horizontal: true, stacked: true },
line: { mark: "line" },
area: { mark: "area" },
};
export const D3Report = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><D3Chart reportId={reportId} /></section>
);
function D3Chart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie": {
const slices = pie().value((r) => r[ys[0]]).sort(null)(rows);
const shape = arc().innerRadius(0).outerRadius(110);
return (
<svg viewBox="-120 -120 240 240" height={H} width="100%" role="img">
{slices.map((s, i) => <path key={i} d={shape(s)} fill={COLORS[i % COLORS.length]}><title>{`${rows[i][x]}: ${rows[i][ys[0]]}`}</title></path>)}
</svg>
);
}
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return <Cartesian rows={rows} x={x} ys={ys} {...type} />;
}
}
}
function Cartesian({ rows, x, ys, mark, horizontal = false, stacked = false }) {
const m = { top: 8, right: horizontal ? 24 : 8, bottom: 24, left: horizontal ? 90 : 48 };
// Each series' [from, to] for each row: on top of the series before it, or from zero.
const layers = stacked ? stack().keys(ys)(rows) : ys.map((y) => rows.map((r) => [0, r[y]]));
const cats = rows.map((r) => String(r[x]));
const band = scaleBand(cats, horizontal ? [m.top, H - m.bottom] : [m.left, W - m.right]).padding(0.2);
const point = scalePoint(cats, [m.left, W - m.right]);
const side = scaleBand(ys, [0, band.bandwidth()]).padding(0.1);
const value = scaleLinear([0, Math.max(1, ...layers.flatMap((l) => l.map((d) => d[1])))], horizontal ? [m.left, W - m.right] : [H - m.bottom, m.top]).nice();
// About four category labels along the bottom, so they don't run into each other.
const step = horizontal ? 1 : Math.ceil(cats.length / 4);
return (
<>
<svg viewBox={`0 0 ${W} ${H}`} width="100%" role="img" fontSize={10} fill="#71717a">
{value.ticks(4).map((t) => horizontal
? <g key={t}><line x1={value(t)} x2={value(t)} y1={m.top} y2={H - m.bottom} stroke="#f4f4f5" /><text x={value(t)} y={H - 6} textAnchor="middle">{t.toLocaleString()}</text></g>
: <g key={t}><line x1={m.left} x2={W - m.right} y1={value(t)} y2={value(t)} stroke="#f4f4f5" /><text x={m.left - 6} y={value(t)} dy="0.32em" textAnchor="end">{t.toLocaleString()}</text></g>)}
{layers.map((layer, i) => {
const color = COLORS[i % COLORS.length];
if (mark === "line") return <path key={i} d={line((_, j) => point(cats[j]), (d) => value(d[1]))(layer)} fill="none" stroke={color} strokeWidth={2} />;
if (mark === "area") return <path key={i} d={area((_, j) => point(cats[j]), (d) => value(d[0]), (d) => value(d[1]))(layer)} fill={color} fillOpacity={0.4} stroke={color} />;
return layer.map((d, j) => {
const at = band(cats[j]) + (stacked ? 0 : side(ys[i]));
const size = stacked ? band.bandwidth() : side.bandwidth();
const [from, to] = [value(d[0]), value(d[1])];
return horizontal
? <rect key={j} x={Math.min(from, to)} y={at} width={Math.abs(to - from)} height={size} fill={color} />
: <rect key={j} x={at} y={Math.min(from, to)} width={size} height={Math.abs(to - from)} fill={color} />;
});
})}
{cats.map((c, j) => (j % step ? null : horizontal
? <text key={c} x={m.left - 6} y={band(c) + band.bandwidth() / 2} dy="0.32em" textAnchor="end">{c}</text>
: <text key={c} x={mark === "bar" ? band(c) + band.bandwidth() / 2 : point(c)} y={H - 6} textAnchor="middle">{c}</text>))}
</svg>
{ys.length > 1 && <p className="legend">{ys.map((y, i) => <span key={y}><i style={{ background: COLORS[i % COLORS.length] }} />{y}</span>)}</p>}
</>
);
}import { AllCommunityModule, ModuleRegistry } from "ag-charts-community";
import { AgCharts } from "ag-charts-react";
ModuleRegistry.registerModules([AllCommunityModule]);
// AG Charts draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
// Quill chartType → AG Charts series options.
const CHART_TYPES = {
column: { type: "bar" },
bar: { type: "bar", direction: "horizontal" },
stacked: { type: "bar", stacked: true },
"stacked bar": { type: "bar", direction: "horizontal", stacked: true },
line: { type: "line", marker: { enabled: false } },
area: { type: "area" },
};
export const AgChartsReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><AgChartsChart reportId={reportId} /></section>
);
function AgChartsChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return <AgCharts style={{ height: 240 }} options={{ data: rows, series: [{ type: "pie", angleKey: ys[0], legendItemKey: x }] }} />;
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
return (
<AgCharts style={{ height: 240 }} options={{
data: rows,
series: ys.map((y) => ({ ...type, xKey: x, yKey: y, yName: y })),
// The number axis: along the bottom (x) when the bars run across.
axes: { [type.direction === "horizontal" ? "x" : "y"]: { type: "number", label: { formatter: ({ value }) => value.toLocaleString() } } },
legend: { enabled: ys.length > 1 },
}} />
);
}
}
}import { ResponsiveBar } from "@nivo/bar";
import { ResponsiveLine } from "@nivo/line";
import { ResponsivePie } from "@nivo/pie";
// Nivo draws charts only, so the metric and table are plain HTML.
const Metric = ({ rows, ys }) => <h2>{rows[0]?.[ys[0]]?.toLocaleString()}</h2>;
const Table = ({ rows, x, ys }) => (
<table>
<tbody>
{rows.map((row, i) => <tr key={i}>{[x, ...ys].map((f) => <td key={f}>{row[f]?.toLocaleString()}</td>)}</tr>)}
</tbody>
</table>
);
const COLORS = ["#2563eb", "#16a34a", "#f59e0b", "#dc2626", "#7c3aed"];
const NUMBER = ">-,.0f";
// Quill chartType → Nivo chart and props.
const CHART_TYPES = {
column: { Chart: ResponsiveBar, groupMode: "grouped" },
bar: { Chart: ResponsiveBar, groupMode: "grouped", layout: "horizontal" },
stacked: { Chart: ResponsiveBar, groupMode: "stacked" },
"stacked bar": { Chart: ResponsiveBar, groupMode: "stacked", layout: "horizontal" },
line: { Chart: ResponsiveLine },
area: { Chart: ResponsiveLine, enableArea: true },
};
export const NivoReport = ({ reportId, name }: { reportId: string; name: string }) => (
<section className="plain"><h3>{name}</h3><NivoChart reportId={reportId} /></section>
);
function NivoChart({ reportId }: { reportId: string }) {
const { rows, x, ys, chartType, loading } = useChart(reportId);
// About four labels along the bottom, so they don't run into each other.
const ticks = rows.map((r) => String(r[x])).filter((_, i, all) => i % Math.ceil(all.length / 4) === 0);
const legends = ys.length > 1 ? [{ anchor: "bottom", direction: "row", translateY: 56, itemWidth: 84, itemHeight: 14, symbolSize: 10 }] : [];
switch (loading ? "loading" : chartType) {
case "loading": return <div>Loading…</div>;
case "metric": return <Metric rows={rows} ys={ys} />;
case "table": return <Table rows={rows} x={x} ys={ys} />;
case "pie":
return (
<div style={{ height: 240 }}>
<ResponsivePie data={rows.map((r) => ({ id: String(r[x]), value: r[ys[0]] }))} colors={COLORS} valueFormat={NUMBER}
margin={{ top: 24, right: 80, bottom: 24, left: 80 }} enableArcLabels={false} arcLinkLabelsSkipAngle={8} />
</div>
);
default: {
const type = CHART_TYPES[chartType];
if (!type) return <Table rows={rows} x={x} ys={ys} />;
const { Chart, layout, ...props } = type;
const margin = { top: 8, right: 16, bottom: ys.length > 1 ? 64 : 32, left: layout ? 96 : 56 };
return (
<div style={{ height: ys.length > 1 ? 272 : 240 }}>
{Chart === ResponsiveBar ? (
<ResponsiveBar data={layout ? [...rows].reverse() : rows} keys={ys} indexBy={x} layout={layout ?? "vertical"} colors={COLORS}
margin={margin} padding={0.25} enableLabel={false} valueFormat={NUMBER} legends={legends.map((l) => ({ ...l, dataFrom: "keys" }))}
axisLeft={layout ? {} : { format: NUMBER, tickValues: 5 }} axisBottom={layout ? { format: NUMBER, tickValues: 5 } : { tickValues: ticks }}
gridYValues={layout ? undefined : 5} gridXValues={layout ? 5 : undefined} {...props} />
) : (
<ResponsiveLine data={ys.map((y) => ({ id: y, data: rows.map((r) => ({ x: String(r[x]), y: r[y] })) }))} colors={COLORS}
margin={margin} xScale={{ type: "point" }} yScale={{ type: "linear", min: 0 }} enablePoints={false} useMesh
yFormat={NUMBER} axisLeft={{ format: NUMBER, tickValues: 5 }} gridYValues={5} enableGridX={false} axisBottom={{ tickValues: ticks }}
legends={legends} {...props} />
)}
</div>
);
}
}
}