Components / data

Chart

Values on one scale: across an ordered base as lines, or by category as stacked bars.

Rendered from the package · open on its own

Summary

Values on one scale: across an ordered base as lines, or by category as stacked bars.

APG patternnone
Built onnative elements
Statusself-tested
Since0.16.0
<Chart
  label="Spending by month against the baseline, highest in July"
  labels={['Jan', 'Feb', 'Mar', 'Apr']}
  series={[
    { name: 'Spending', values: [5120, 4980, 5340, 5610] },
    { name: 'Baseline', values: [5400, 5400, 5400, 5400], neutral: true },
  ]}
  reference={{ value: 5775, label: 'Budget' }}
  markers={[{ at: 'Mar', label: 'Today' }]}
  format={{ style: 'currency', currency: 'USD', maximumFractionDigits: 0 }}
/>
<Chart kind="bar" label="Budget groups by quarter" labels={quarters} series={groups} />

Anatomy

A <figure data-se="chart"> holding the plot and a table of the same values. The plot is a grid: the value-axis beside the area, and the category-axis under it. Everything in the area is placed by a percentage the markup writes, so the drawing fills its width with no measuring and no script.

  • A line is a series: an <svg> that stretches to the area, holding one polyline, and a mark on its last value. It runs from the start edge of the plot to the end edge, and each label sits under its point.
  • A bar is a bar holding one band for each series with a value there, written first to last and drawn bottom to top. It stands in the middle of its position.
  • reference is a level across the plot and marker a position up it, each with its label.
  • cursor and readout are the highlight. The readout is always in the tree and empty at rest.
  • The table sits in a sr box: read, and not drawn.
PartElementRequired
area<div>yes
band<span>yes
bar<span>yes
category-axis<div>yes
cursor<span>no
fill<polygon>no
gridline<span>yes
label<span>yes
mark<span>no
marker<span>yes
name<span>yes
plot<div>yes
position<span>yes
readout<span>yes
reference<span>no
row<span>yes
series<svg>yes
sizer<span>yes
sr<div>yes
swatch<span>yes
tick<span>yes
value<span>yes
value-axis<div>yes
<figure data-se="chart" data-kind="line" aria-label="Spending by month">
  <div data-part="plot" role="group" aria-label="Spending by month" tabindex="0">
    <div data-part="value-axis" aria-hidden="true">
      <span data-part="sizer">$6,000</span>
      <span data-part="tick" style="inset-block-end: 0%">$4,000</span>
      <span data-part="tick" style="inset-block-end: 100%">$6,000</span>
    </div>
    <div data-part="area" style="--se-_count: 4">
      <span data-part="gridline" style="inset-block-end: 0%"></span>
      <svg data-part="series" data-series="1" viewBox="0 0 100 100" preserveAspectRatio="none" aria-hidden="true">
        <polyline points="12.5,44 37.5,51 62.5,33 87.5,19.5"/>
      </svg>
      <span data-part="mark" data-series="1" style="inset-inline-start: 87.5%; inset-block-end: 80.5%"></span>
      <span data-part="readout" role="status"></span>
    </div>
    <div data-part="category-axis" aria-hidden="true"><span data-part="tick">Jan</span>…</div>
  </div>
  <div data-part="sr"><table>…</table></div>
</figure>

Properties

PropertyValuesDefaultWhat it does
kindline · barlineLines for change across an ordered base, running from one edge of the plot to the other. Bars, stacked, for amounts by category.
labelsstring[]requiredThe positions along the base, in order: months, or categories.
seriesChartSeries[]requiredThe series, in the order they are drawn. The first three take the series colors in order; the rest are muted.
reference{ value: numbernoneA level to compare against, drawn dashed across the plot and written on it with its value.
labelstring }requiredunknown
markersArray<{ at: string | numbernonePositions to point out, each drawn dashed up the plot and named at its top. at is a label or its place in the list.
labelstring }>requiredunknown
labelstringrequiredIts name and the finding, in a sentence: Spending by month, highest in July. Not drawn.
formatIntl.NumberFormatOptionsnoneHow a value is written on the axis and in the readout: the options of Intl.NumberFormat, such as money or a percentage.
axisFormatIntl.NumberFormatOptionsnoneHow a figure on the value axis is written, when it is shorter than the value: { notation: 'compact' } writes $2K beside a readout that says $2,150. format, when omitted.
localestringnoneThe language the values are written in. The reader's own, when omitted.
totalTextstringTotalThe word for a bar's total in the readout and the table.
onHighlight(position: { label: stringnoneThe highlighted position changed, or was cleared: its label and each series' value there, or null.
valuesArray<{ name: stringrequiredunknown
valuenumber }> } | null) => voidrequiredunknown

The height is a local on the root, --se-_height. It is five large controls unless a caller sets it.

States

StateExpressed as
Focused:focus-visible

Behavior

One scale. Every series is measured on the same value axis, and its figures land on round numbers: steps of 1, 2, 2.5 or 5 times a power of ten. A bar chart's scale starts at zero, because a bar's length is its value. A line chart's covers its values and the reference.

Series take the colors in order. The first takes --se-series-1, then the second and third. The colors never cycle: a series marked neutral, and every series past the third, takes the muted text color.

Bars stack. In the order written, the first on the base. The readout and the table give the total with the parts.

A pointer or the keyboard highlights a position. A pointer over the plot highlights the nearest position, a line's nearest point or the place a bar stands in, and leaving clears it. With focus on the plot, Left and Right move one position, Home and End go to the ends, and Escape clears it. Focus leaving the plot clears it too.

The readout names every series. The position's label, then each series by name with its value. It sits at the top of the plot, on the side of the cursor that has room.

It draws with no script. The axes, the series, the reference and the markers are static markup. Only the highlight is the binding.

Nothing to draw. With no labels or no series it keeps its name and draws no marks, and the plot is not a focus stop. A missing value leaves a gap in its line.

Accessibility

The values are in a table. Every value is in a table inside the figure, with each series named. A reader who cannot see the plot reads the table; nothing is told by color or by a pointer alone.

The plot is one focus stop, a group named by the label. The axes and the drawing are hidden from assistive technology: the table says the same thing in words.

The readout is a status. When the highlight moves, what it says is announced.

Name the finding. label is the chart's name and what it shows, in a sentence: Spending by month, highest in July. Not the word chart.

Keyboard

KeyResult
TabMoves focus to the plot
Right, LeftMove the highlight one position. From nothing, Right starts at the first and Left at the last
Home, EndMove the highlight to the first or last position
EscapeClears the highlight

Roles, states and properties

AttributeSet by
aria-hiddenalways true
aria-labelset at render
rolealways group, always status

Focus

One focus stop, the plot. The ring is drawn around it.

Screen reader

Not recorded as verified. Stage 4 of the readiness ladder — driving each component with VoiceOver and NVDA — is unstarted across the package, and no component should claim otherwise until docs/journal/ records the session that did it.

Contrast and target size

npm run check:contrast audits every color pair this component uses across all four skins, light and dark, and fails the build on a shortfall. Two waivers are on the record in docs/audit/DEFERRED.md.

It sets no target size of its own; anything interactive inside it brings its own.

Tested

  • test/apg/chart-legend.test.jsx
  • test/apg/chart.test.jsx
  • test/browser/chart-legend.spec.mjs
  • test/browser/chart.spec.mjs

Known gaps

_No upstream requirement is recorded as not applicable._

The arrow keys reach the plot only when it holds focus. A screen reader in its reading mode keeps the arrows for itself. The table carries the values for it.

Lines are told apart on the plot by color alone. The readout, the table and a ChartLegend each name every series. A dash or a mark shape on the line itself is not built.

Appearance

Figures and labels are --se-text-2xs, muted, with tabular figures. Gridlines are hairlines in the border color; where values fall on both sides of zero, the zero line is the muted text color. A line is a little over twice the hairline in its series color, with a dot on its last value ringed in the surface color, and the first series fills the space under it at the soft mix. A bar is just under two thirds of its position, its bands a hairline apart, with half the smallest radius on its top corners. The labels sit one step under the plot. The reference is dashed in the text color with its words above it at the end; a marker is dashed in the muted color with its name at the top. The cursor is a hairline up a line chart, and the whole position, lightly, behind a bar. The readout is a raised surface: a hairline edge, the middle radius and the floating shadow.

TokenUsed for
--se-borderborder, border-block-end, border-block-start
--se-border-widthborder, border-block-end, border-block-start, border-inline-start, box-shadow, gap, stroke-width
--se-col-minmin-inline-size
--se-control-h-lgheight
--se-dot-sizeblock-size, inline-size, margin-block-end, margin-inline-start
--se-focus-coloroutline
--se-focus-offsetmargin, outline-offset
--se-focus-widthmargin, outline
--se-leading-uiline-height, padding-block-start
--se-mix-softbackground, fill
--se-radius-mdborder-radius
--se-radius-pillborder-radius
--se-radius-smborder-radius, border-start-end-radius, border-start-start-radius
--se-series-1series
--se-series-2series
--se-series-3series
--se-shadow-floatingbox-shadow
--se-space-1inset-block-end, inset-inline-start, margin-block-end, margin-block-start, margin-inline-start, padding-block-start
--se-space-2column-gap, gap, margin-inline, padding, row-gap
--se-space-4padding-inline-end
--se-surfacebackground, box-shadow
--se-textbackground, border-block-end, color
--se-text-2xsfont-size, padding-block-start
--se-text-mutedborder-block-end-color, border-inline-start, color, series
--se-text-xsfont-size
--se-weight-mediumfont-weight

Motion

None. Lines never draw themselves in and bars never grow: data is not decoration. The readout does not fade; it follows the pointer.

Content

The label is the finding. Spending by month against the baseline, highest in July.

A series' name is the one its legend uses. One name per series, everywhere.

A reference and a marker are named in a word or two. Budget, Today.

Usage

With a ChartLegend when there is more than one series. Place it in the Header above the chart, or under it. The chart does not hold one.

In a Body, inside a card's padding. It is not flush: its figures need the room at the start.

When not to use it. For one number, Stat. For a trend with nothing to read off it, Sparkline. For looking a value up, DataTable.

Notes

It belongs in the core. The question that held it was whether a chart can exist on a page with no script. It can: every position is a share of the plot, written once, so nothing is measured and nothing is drawn again when the width changes.

A line runs edge to edge; a bar stands in the middle of its position. A line that stopped half a position short of each side left its fill ending in mid-air. Under a line each label is centered on its point, so the first and last reach half their width past the plot, and the plot keeps room at its end for that and for the dot.

The axis can say less than the readout. axisFormat writes the figures on the scale, so an axis reads $2K where the readout reads $2,150. Without it both use format.

A value of nothing writes no band. There is nothing to draw, and a band with no height would still take a gap.

The top figure needs room above the plot. It is centered on the top gridline, so the plot keeps half a line clear above it; without that the figure's scroll box cut it.

The stroke does not scale. The series' box stretches to the area, and vector-effect: non-scaling-stroke keeps the line one width, as Sparkline's does.

Bars are elements, not shapes in the stretched box. A corner radius in a box that stretches is an ellipse.

The sizer. The value axis holds its widest figure once more, not drawn, so the column is as wide as its figures without the stylesheet knowing them.

Every second label is marked, not removed. Below the width the system calls narrow, the one Collapsible already uses, a marked label is not drawn and keeps its place. A container query cannot read a custom property, so the width is that same literal.

The table is in a wrapper. A table will not shrink to the box that hides it, so the box is a <div> around it.

format is the options of Intl.NumberFormat, not a function. A function cannot cross from a server page to a client component.

More kinds than line and bar were refused in the spec. Each further kind is its own decision, with its own evidence.