Ir al contenido
Español

Cómo funciona

A complete widget card: title row, hero number, bars and labels. Bars sit in a CSS grid and every height is a percentage, so the server render is already correct. Each bar is a clipped column whose fill slides up with transform, so round ends stay round and a data change moves a bar from its old height to the new one. Resting bars use the soft colour; the selected or hovered one turns solid with a light gradient, its label becomes an ink pill and the hero counts to its value.

Úsalo para

  • A weekly or monthly total where the latest period is the story and earlier ones give context.
  • Daily values inside weeks, drawn as a bucket of thin bars per week.
  • Days measured against a budget or goal, with a threshold line that makes the days over it stand out.
  • A compact revenue or progress card with a few columns beside the number.

Busca otra opción cuando

  • Long series (over about 30 points) or trends: use a line or area chart.
  • Several series compared side by side: one card has one colour; use one card per series or a stacked bar chart.
  • Rankings with long names: use ranked horizontal bars.

Uso

import { BarChart } from './BarChart.jsx'

export function CyclingCard() {
  return (
    <BarChart
      title="Cycling"
      qualifier="Last 8 weeks"
      tone={4}
      caption={(label, i) => (i === 7 ? 'This week' : `Week ${label}`)}
      unit="km"
      categories={['1', '2', '3', '4', '5', '6', '7', '8']}
      data={[42, 18, 64, 82, 36, 33, 96, 86]}
      average
      onAction={() => {}}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
titlestring—Card title, 15px semibold.
qualifierstring—Grey words after the title, such as “Last 8 weeks”.
iconReactNode—An 18px icon shown in a 32px square tinted with the card colour.
onAction() => void—Shows a round button at the right of the title row and calls this on press.
actionIcon'open' | 'more''open'The button glyph: a chevron (open details) or three dots (more options).
actionLabelstring—Accessible name of the button; defaults to text.open or text.more.
actionReactNode—Your own element in the title row instead of the built-in button.
captionstring | (label, index) => string—Line above the hero number. A function receives the current label and index, so it can follow the selection (“Week 5”).
valuenumber—Fixed hero number. Without it the hero shows the current bar (the mean of a bucket of thin bars) and counts to the hovered one.
unitstring—Unit beside the hero number, 15px grey.
chip{ label: string, value: string }—A small chip at the right of the hero such as “OVER · 7 days”; only the value takes the card colour.
noteReactNode—A sentence under the hero; <b> inside it takes the card colour.
categoriesstring[]—Axis categories, one per bar or bucket. Without them there is no axis row. `labels` is the older name and still works (deprecated: in blocks `labels` means text overrides).
datanumber[] | number[][]—Numbers draw one column per label; arrays draw a bucket of thin bars per label (the days of a week). Non-numbers count as 0.
selectednumber—Index of the selected bar, solid with an ink pill label. Defaults to the last one; when set, the chart is controlled.
onSelect(index) => void—Called when a bar is clicked, tapped or chosen with the arrow keys.
averageboolean | numberfalsetrue draws a line at the mean of the bars, a number draws it there. Its label counts up with the bars.
thresholdnumber—Draws a line in the card colour; bars at or above it are solid, the rest neutral. Bars default to 4px wide.
min / maxnumber0 / automaticValue at the baseline and at the top of the plot. Max defaults to 5% above the tallest bar or line.
layout'stacked' | 'side''stacked'stacked puts the hero above the bars; side puts it on the left with a few columns on the right.
tone1-81Which --ds-chart-N colour the card uses; resting bars take its -soft partner.
heightnumber120 (side: 150)Plot height in px, without the axis labels.
barWidthnumber—Column width in px (at most 58% of its slot). Default 40, or 4 with a threshold.
format'number' | 'percent' | 'currency' | 'duration' | (n) => string'number'How values read: a number with the data's decimals (one-decimal data keeps one decimal, also in the average), a percent (42 reads 42%), money in `currency`, a duration in seconds (4m 30s), or your function.
locale / currencystring / string'en-US' / 'USD'Language of the number format and the ISO code used by format='currency'.
formatValue(n) => string—Your own formatter, ahead of `format`. While counting it receives in-between values rounded to the target's decimals.
textobject—Replaces built-in words (average, limit, open, more, chart, describe, dataTitle, period, value); shallow-merged over the English defaults.
labelstring—Accessible name of the plot and caption of the hidden table; defaults to the title.
replayKeyany—Change it to play the entrance again from zero.
...restHTMLAttributes<HTMLDivElement>—Other attributes, such as className and style, go to the card.

Accesibilidad

  • The plot is a focusable group; Left and Right move the selection, Home and End jump to the ends, and each move is announced through a live region.
  • A visually hidden table lists every label and value, so the tooltip is never the only way to read the data.
  • The selected bar is marked by its ink pill label and the hero number, not by colour alone.
  • Bars have no hover-only content that a keyboard cannot reach: keyboard focus shows the same tooltip.
  • With reduced motion the card appears in its final state with no rise and no counting.

Movimiento

When 30% of the card is on screen, bars rise from the baseline over 900ms on cubic-bezier(0.22, 1, 0.36, 1), 12-30ms apart; the average line draws left to right and its label and the hero count up on the same curve. A data change moves bars from old to new heights. Hover swaps the solid bar in 160ms, the tooltip follows in 200ms and the axis pill springs in over 240ms.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

BarChart.jsxbar-chart.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Gráficos
Trilateración viene con Pro. La vista previa aparece al desbloquearlo.