Chart
The parts every chart is made of, as shadcn/ui's: the chart, with its rows and its series' names and colors (its config); the plot, with a grid, axes and a cursor; around it, a tooltip and a legend. The series — areas, bars, lines — go in the plot. The keyboard moves through the categories; the tooltip says their values.
Installation
It has its own entry point, @semantic-components/ui/charts, built on d3-scale d3-shape — an optional peer of ui: install it where you use the component.
import { ScChart, ScChartPlot, ScChartTooltipProvider, ScChartTooltipPortal, ScChartTooltip, ScChartTooltipLabel, ScChartTooltipItem, ScChartTooltipIndicator, ScChartTooltipName, ScChartTooltipValue, ScChartLegend, ScChartLegendItem, ScChartLegendIndicator } from '@semantic-components/ui/charts';Usage
import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScChart, ScChartBar, ScChartCategoryAxis, type ScChartConfig, ScChartCursor, ScChartGrid, ScChartPlot, ScChartTooltip, ScChartTooltipIndicator, ScChartTooltipItem, ScChartTooltipName, ScChartTooltipPortal, ScChartTooltipProvider, ScChartTooltipValue,} from '@semantic-components/ui/charts';
@Component({ selector: 'app-chart-usage-demo', imports: [ ScChart, ScChartPlot, ScChartGrid, ScChartCategoryAxis, ScChartCursor, ScChartBar, ScChartTooltip, ScChartTooltipPortal, ScChartTooltipProvider, DecimalPipe, ScChartTooltipIndicator, ScChartTooltipItem, ScChartTooltipName, ScChartTooltipValue, ], template: ` <div scChart [config]="config" [data]="data"> <svg scChartPlot categoryKey="month" aria-label="Desktop visitors, January to June" > <g scChartGrid /> <g scChartCategoryAxis [tickFormatter]="short" /> <g scChartCursor /> <g scChartBar key="desktop" /> </svg> <div scChartTooltipProvider> <ng-template scChartTooltipPortal> <div scChartTooltip #tooltip="scChartTooltip"> @for (item of tooltip.payload(); track item.dataKey) { <div scChartTooltipItem [item]="item"> <span scChartTooltipIndicator></span> <span scChartTooltipName>{{ item.name }}</span> <span scChartTooltipValue>{{ item.value | number }}</span> </div> } </div> </ng-template> </div> </div> `, host: { class: 'block w-full max-w-xl' }, encapsulation: ViewEncapsulation.None,})export class ChartUsageDemo { private readonly data = [ { month: 'January', desktop: 186, mobile: 80 }, { month: 'February', desktop: 305, mobile: 200 }, { month: 'March', desktop: 237, mobile: 120 }, { month: 'April', desktop: 73, mobile: 190 }, { month: 'May', desktop: 209, mobile: 130 }, { month: 'June', desktop: 214, mobile: 140 }, ]; private readonly config: ScChartConfig = { desktop: { label: 'Desktop', color: 'var(--chart-1)' }, mobile: { label: 'Mobile', color: 'var(--chart-2)' }, }; /** January → Jan. */ private readonly short = (month: string) => month.slice(0, 3);}Anatomy
The chart holds the rows and the config; the plot draws the grid, the axes, the cursor and the series; the tooltip and the legend are yours to write, from their state. A part you leave out isn't there.
import { ScChart, ScChartBar, ScChartCategoryAxis, ScChartCursor, ScChartGrid, ScChartLabels, ScChartLegend, ScChartLegendIndicator, ScChartLegendItem, ScChartPlot, ScChartTooltip, ScChartTooltipIndicator, ScChartTooltipItem, ScChartTooltipLabel, ScChartTooltipName, ScChartTooltipPortal, ScChartTooltipProvider, ScChartTooltipValue, ScChartValueAxis } from '@semantic-components/ui/charts';<div scChart [config]="…" [data]="…"> <svg scChartPlot categoryKey="…" aria-label="…"> <g scChartGrid /> <g scChartCategoryAxis /> <g scChartValueAxis /> <g scChartCursor /> <g scChartBar key="…"> <g scChartLabels /> </g> </svg> <div scChartTooltipProvider> <ng-template scChartTooltipPortal> <div scChartTooltip #tooltip="scChartTooltip"> <div scChartTooltipLabel>…</div> <div scChartTooltipItem [item]="…"> <span scChartTooltipIndicator></span> <span scChartTooltipName>…</span> <span scChartTooltipValue>…</span> </div> </div> </ng-template> </div> <div scChartLegend #legend="scChartLegend"> <div scChartLegendItem [item]="…"> <span scChartLegendIndicator></span> </div> </div></div>Accessibility
- The plot is one tab stop, a group named by its aria-label: on focus, the first category; the arrow keys, Home and End move between them; Escape closes the tooltip. A tap points like the pointer.
- As the keyboard moves, screen readers hear what the tooltip says — your text, as you formatted it — through the CDK's LiveAnnouncer (politely). The indicators are hidden from them.
- The axes, the grid and the cursor are hidden from assistive technology: the tooltip says what they show.
- With reduced motion (prefers-reduced-motion), the series appear and change at once.
Tooltip and legend
Each series' name and color come from the chart's config; its color is --color-<key>, for your own SVG too. You write what the tooltip and the legend show, from their state (the category, an item per series): each part you leave out isn't there, and the text and its format are yours. The tooltip marks each value with a dot, a line or a dash.
Tooltip and legend
import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import { ScToggleGroup, ScToggleGroupItem } from '@semantic-components/ui';import { ScChart, ScChartBar, ScChartCategoryAxis, type ScChartConfig, ScChartCursor, ScChartGrid,Chart types
Area Chart
Filled down to 0, or stacked — to 100% if you like.
Bar Chart
Side by side, stacked, below 0.
Line Chart
Through the values, with dots if you like.
Pie Chart
Slices of a whole: a pie, a donut, rings.
Radial Chart
A ring per row, its value as an arc.
Radar Chart
Polygons over the categories, one per series.
Scatter Chart
Points at two values — sized by a third: bubbles.
Funnel Chart
Stages, top to bottom, narrowing to the next.
Treemap Chart
Boxes as large as their values, in groups.
Sunburst Chart
Two rings: groups inside, their rows around.
Sankey Chart
Flows between nodes, as thick as their values.
API reference
The parts every chart shares, and their inputs; each series' are on its own page. Each part takes class, merged over its own.
| Part | Inputs |
|---|---|
div[scChart] | data, config |
svg[scChartPlot] | categoryKey (required), margin (as Recharts: 5 on each side without one; the sides a given one leaves out are 0), layout (horizontal | vertical: categories down the side, bars across), stackOffset (none | expand: to 100%); polar charts: startAngle, endAngle (Recharts': degrees, 0 at 3 o'clock, counterclockwise; unset, 0 to 360 — a radar's 90 to −270, from the top clockwise); name it (aria-label) |
g[scChartGrid] | horizontal (true), vertical (false) — its lines at the value axis's ticks, as Recharts' grid |
g[scChartCategoryAxis] | The categories, under the plot (beside it, laid out vertical): tickFormatter(category, index), width (60, beside) — evenly skipped when they don't fit. reversed: the categories from the right (from the bottom, vertical); orientation (left, right): its side, beside — both unset by default: the chart's (see Right to left). orientation also top or bottom along the plot (top: the labels above it); hide: no room, no labels (Recharts'). |
g[scChartValueAxis] | The values, beside the plot (under it, laid out vertical): width (40), tickCount (5: Recharts' nice ticks, which also set the value domain — 0 to 320 for values up to 305), tickFormatter(value) — the app's locale by default. reversed: the values down from the top (from the right, vertical); orientation (left, right): its side — both unset by default: the chart's (see Right to left). domain ([0, 100]): its values' range, fixed (Recharts') — else from the data; hide: no room, no labels, its domain still holding. |
g[scChartCursor] | — a band behind bars, a line across points |
g[scChartPolarGrid] | Polar charts: shape (polygon | circle), ringCount (4), spokes (true) |
g[scChartPolarAngleAxis] | A radar's categories around it: tickFormatter(category, index) |
g[scChartCenter] | A polar chart's center: your SVG at (0, 0) — a donut's total |
g[scChartLabels] | Inside a series: its values written on the chart — position (outside | inside | start), offset (8), formatter(value, category, index) |
div[scChartTooltipProvider] | The tooltip: its portal in a CDK overlay while a category is active — beside the pointer, or the item the keyboard reached; flipped and kept in the window. side (right), alignment (start), sideOffset (10), alignmentOffset (10): Recharts' place. isAnimationActive (true), animationDuration (400), animationEasing (ease): it glides from place to place, as Recharts' (not with reduced motion). Holds the state |
ng-template[scChartTooltipPortal] | The tooltip's content, rendered while it shows |
div[scChartTooltip] | The box, in the style's look (exportAs: scChartTooltip): active(), label() (the category), payload() (an item per series: dataKey, name, value, color) — see [Tooltip and legend](#tooltip-and-legend) |
[scChartTooltipLabel] | The tooltip's title: write the category |
[scChartTooltipItem] | A row: item (one of payload()) — its indicator takes its colour |
[scChartTooltipIndicator] | The row's mark: variant (dot | line | dashed) |
[scChartTooltipName], [scChartTooltipValue] | The row's name (muted) and value (at its end, tabular figures): write them |
div[scChartLegend] | The legend's row (exportAs: scChartLegend): payload() — the series, or a pie's categories, a sunburst's groups, a sankey's nodes (dataKey, name, color) |
[scChartLegendItem], [scChartLegendIndicator] | An entry: item (one of payload()), and its colour swatch |