Skip to content
buttery

v1.0.0

Source on GitHub

Version 1.0.0

ChartFrame

The frame buttery's charts are drawn in: gridlines and axis labels, a crosshair or column with a tooltip, keyboard steps, a screen-reader table, and no-data and loading states.

import { ChartFrame } from "@butteryui/react";

Demo

Both peaked on Oct 13.
Orders and bags roasted
DateOrdersBags roasted
Oct 84248
Oct 93840
Oct 105155
Oct 114752
Oct 126360
Oct 137174
Oct 145866

When to use it

Use it for

  • Combining marks in one chart, such as bars for orders with a line for bags roasted
  • Drawing a chart of your own with buttery's axes, hover, keyboard and table, through seriesPath

Not for

  • One kind of mark: use LineChart, AreaChart or BarChart
  • A trend in a table row or beside a number: use Sparkline

Examples

Orders against last week

This week's orders as bars with last week's as a line over them, two marks in one frame, so each day shows its change.

Up on last week every day but Tuesday.
Orders
DayThis weekLast week
Mon4239
Tue3841
Wed5146
Thu4745
Fri6360
Sat7166
Sun5850

Orders and bags

A roastery dashboard card: orders as bars and bags roasted as a line in one frame, with a week switch and the busiest day.

Orders and roasting

Busiest day: Sat, 71 orders

Orders and bags roasted, this week
DayOrdersBags roasted
Mon4248
Tue3840
Wed5155
Thu4752
Fri6360
Sat7174
Sun5866

Refunds by week

Refunds per week: switching months shows the loading pill, and a month with no refunds yet shows the empty state.

Refunds in October
WeekRefunds
Week 13
Week 21
Week 34
Week 42

Stock against target

The three sizes, shown on a chart of green coffee on hand against its reorder level.

220px tall

Green coffee on hand
MonthOn handReorder level
Jun2,600 kg2,000 kg
Jul2,280 kg2,000 kg
Aug1,940 kg2,000 kg
Sep2,410 kg2,000 kg
Oct2,140 kg2,000 kg

Variants and states

Bars and a line in one frame

Point at a day, or focus the plot and use the arrow keys: the column, the dots and the tooltip move together.

Both peaked on Oct 13.
Orders and bags roasted
DateOrdersBags roasted
Oct 84248
Oct 93840
Oct 105155
Oct 114752
Oct 126360
Oct 137174
Oct 145866

No data and loading

With nothing to draw, a dashed baseline and a caption; while loading, a pill over the same box after 300ms.

No data for this period

Props

ChartFrame

PropTypeDefaultDescription
label

required

stringNames the chart for screen readers and captions its data table, such as "Sales".
summarystringOne line on what the chart shows, read after its name, such as "Up 12% on last week".
labels

required

readonly string[]The text for each point along the bottom, such as dates; the first and last show under the plot, and three more at lg.
series

required

readonly ChartSeries[]The series, each with one value per label.
layout"point" | "band""point"Where points sit: "point" (the default) or "band", which bars need.
size"sm" | "md" | "lg""md"Its height: "sm" 160, "md" 220 or "lg" 300px. Defaults to "md".
formatValue((value: number) => string)Writes a value for the tooltip and the table. Defaults to the number with up to two decimals.
formatTick((value: number) => string)Writes a gridline's value at the end edge. Defaults to a short form with the decimals its step needs, such as 1.25K.
lowerIsBetterbooleanfalseShows a fall against the comparison as good news (success) and a rise as bad. Defaults to false.
loadingbooleanfalseShows a LoadingPill over the empty box in place of the plot. Defaults to false.
loadingLabelstring"Loading data"What the loading pill says. Defaults to "Loading data".
announceLoadingbooleantrueWhether the loading pill's words are read out. Defaults to true; turn it off when what holds the chart is marked busy, as MetricCard is, so a page of loading charts isn't read out once per chart.
emptyLabelstring"No data for this period"What it says with no values to draw. Defaults to "No data for this period".
labelsHeadingstring"Date"Heads the labels' column in the screen-reader table. Defaults to "Date".
data-btstring"ChartFrame"The data-bt stamp, for charts built on the frame. Defaults to "ChartFrame".
childrenReactNodeThe marks: ChartFrame.Line, ChartFrame.Area and ChartFrame.Bars, drawn in order.

It also takes Omit<ComponentPropsWithRef<"div">, "children">.

ChartFrame.Line

PropTypeDefaultDescription
series

required

stringThe key of the series it draws.

ChartFrame.Area

PropTypeDefaultDescription
series

required

stringThe key of the series it draws.

ChartFrame.Bars

PropTypeDefaultDescription
seriesreadonly string[]The keys of the series it draws, side by side in each slot. Defaults to every series.

ChartSeries

One series of values over a chart's labels.

FieldTypeDefaultDescription
key

required

stringIdentifies the series among the chart's others, and marks name it.
label

required

stringIts name in the tooltip and the screen-reader table, such as "This week".
values

required

readonly (number | null)[]One value per label, in order; null where there isn't one, which leaves a gap. A value that isn't a finite number, such as NaN, is left out the same way.
colorChartColorchart-1Its color. Defaults to chart-1, chart-2 and on in order, or chart-muted for the comparison.
comparisonbooleanMarks the series the others are measured against, such as the previous period: it's drawn in chart-muted under the others, and the tooltip shows the first series' change from it.

Also exported

ExportDescription
seriesPath(values: readonly (number | null)[], placement: SeriesPlacement): stringThe SVG path buttery draws a series as: smooth through each run of values without ever bending past one, and open wherever a value is null. Use it to draw a series of your own the way the charts do.

Accessibility

The plot is one Tab stop, a slider over the points named by the chart's label and described by its summary: Left and Right (or Down and Up) step through the points, Home and End jump to the ends, and each step announces the point's label, its values and the change against the comparison. Escape hides the tooltip. A visually hidden table holds every value. With no data, the box is an image named by the label and the empty message; while loading the pill is a status, outside any busy region so it's read out; announceLoading turns it off when what holds the chart is marked busy.

KeysWhat they do
ArrowRight or ArrowUpMoves to the next point and shows its tooltip, announcing its label, values and change
ArrowLeft or ArrowDownMoves to the previous point
Home or EndJumps to the first or the last point
EscapeHides the tooltip

Motion

It doesn't move.

With reduced motion: Nothing changes.

Layout

It fills its container's width, and size sets its height: sm 160px, md 220px (the default) or lg 300px, which the no-data and loading states keep. Axis labels stay on one line, the x-axis showing the first and last labels and three more at lg; the tooltip stays inside the frame, and the whole plot is one hit area where pointing anywhere picks the nearest point.