Chart
Draw any chart that TanStack Charts can describe, with native elements. Describe the chart with TanStack's defineChart, and pass it to <ui-chart>. It draws the chart, and handles touch, tooltips, screen readers, pan and zoom, and dark mode.
For one series, the Line, Area, Bar, Scatter and Candlestick presets are quicker. Use <ui-chart> for several series, overlays, or marks the presets don't offer.
Installation
dolan add also installs the npm packages the chart imports: @tanstack/charts, d3-shape and d3-shape's types, @types/d3-shape. If that install fails, it prints the command to run yourself.
Usage
- A definition maps fields of your data to a mark's channels (
x,y,fill, …) and configures the scales. - Import
defineChartand the marks from@tanstack/charts/universal, the entry point without browser code. Scales come from@tanstack/charts/scales/*. - The TanStack Charts docs cover every mark, scale and option. Don't use
@tanstack/charts/angular: it draws into the browser DOM, which Lynx doesn't have.
Data from a signal
The example builds the definition in a computed(), so the chart redraws when the data changes. Bind it with [definition]="definition()".
A chart whose data never changes can take a plain field: [definition]="definition".
Size
Without width or height, the chart fills its container's width and is 200px tall. It redraws when its layout changes, such as on rotation. Set the height with a class, or fix either side in px:
Series and marks
Put every series in one array and split them with the z channel. Each series takes the next palette color, var(--chart-1) to var(--chart-5).
focus: 'group-x'focuses every series at the touched x value, so the tooltip lists them together.- If each series is its own array, give each a mark with a fixed
z:lineY(lastYear, { x: 'month', y: 'value', z: () => 'Last year' }). - For a legend, lay out
<view>swatches next to the chart. Thebg-chart-1tobg-chart-5classes match the series colors.
List several marks to layer them. They share the scales, and each draws over the ones before it:
- Rules don't take focus, so a touch still lands on the bars.
- For a trend line, use
linearRegressionY(points, { x: 'x', y: 'y', ci: 0 })before adotmark.ci: 0leaves out the confidence band. - For smooth lines and areas, pass
curve: d3Curve(curveMonotoneX), withcurveMonotoneXfromd3-shape. It never overshoots the data.
To keep a preset's axes and styling, start from its definition function and add marks. Each preset exports one, such as lineChartDefinition:
Tooltips
Put tooltip in the definition to show a tooltip for the touched point. Without it, a touch still focuses points and emits focusChange.
- Import
tooltipfrom the chart component, not from@tanstack/charts/tooltip, which is built from browser DOM elements. This one draws native views. - Pass TanStack's tooltip options with
use, as intooltip: { use: tooltip, sticky: false }. Seetooltipfor the options it reads.
To lay out the tooltip yourself, put an <ng-template uiChartTooltip> inside the chart. The chart still places the tooltip and draws its card.
- Import
UiChartTooltipandLYNX_ELEMENTSin your component. - The template gets a
UiChartTooltipContext. A point'sdatumis typedunknownthere, so read its fields in a component method that casts it.
Touch, selection and zoom
- Scrub: touch and drag. Focus follows the nearest point, which the chart highlights and shows in the tooltip.
- Tap: emits the point from
pointSelectand pins the tooltip. While it's pinned, touches don't move focus. The next tap unpins it. Withsticky: false, a tap doesn't pin. - If a scrolling parent takes over the touch, focus clears. A pinned tooltip stays.
ChartPointcomes from@tanstack/charts/types. Itsdatumis the row you passed to the mark.- Both outputs fire inside the touch, so they can change signals or navigate, as a tap handler can.
- A TanStack
selectioncontroller in the definition is told about each tap.
By default, a touch focuses the nearest point within 48px (maxFocusDistance). Set focus in the definition to change that: 'nearest-x' prefers the closest x value, 'group-x' takes every series at that x, and false turns focus off.
Pan and zoom
Set zoomable to let users drag to pan, with +, − and 1:1 (reset) buttons in the top-right corner.
- Zoom works on continuous axes in
scales.xandscales.y, such asscaleLinear. Band and point scales don't zoom, and an axis missing fromscalesstays fixed. - On a zoomable chart, a drag pans instead of moving focus. A short tap still selects a point.
- There's no pinch to zoom, because Lynx has no native pinch handler yet.
- For your own controls, set
showZoomControlstofalseand callzoomIn(),zoomOut()orresetZoom()from aviewChild.required(UiChart). - Zooming doesn't touch your data or definition. The chart sets TanStack's
viewportoption on the zoomed scales.
Theming and dark mode
Give marks theme colors as CSS variables, such as var(--primary) or the palette var(--chart-1) to var(--chart-5). They switch with dark mode.
- Fixed colors such as
#2563ebstay the same in both modes. - Marks without a color take the palette in order.
- Text and gridlines follow
var(--foreground).
Charts follow the app's appearance, as the theme and the icons do. To change a theme color, or keep one chart light or dark, see Chart Colors.
API
UiChart
A ChartPoint has datum (your row), xValue, yValue, groupLabel (the series name), x and y (its position in px), and color.
UiChartTooltip
<ng-template uiChartTooltip let-tooltip> replaces the tooltip's contents. Its context is a UiChartTooltipContext:
tooltip
The tooltip token: tooltip alone, or { use: tooltip, ...options }. <ui-chart> reads the TanStack options below. Options that style the browser tooltip's DOM, such as className and portal, have no effect.
Migrating from the old chart components
<ui-cartesian-chart> and its <ui-*-series> children are gone, and so is the ChartPoint type from cartesian-chart.
- Single-series charts keep their preset, which now fills its container's width (it was 320px).
- Use
XYDatumandCandlestickDatumfor the point types. - Set a color scheme to match how your app switches
.dark. - Pinch to zoom is gone.
- Several series move to
<ui-chart>, with one mark per series:
Accessibility
The chart is one accessibility element. Set label on every chart to say what it shows. The default is 'Chart'.
- A screen reader announces the
label, then the focused point's series, x value and y value. - Two custom actions, Next point and Previous point, step focus through the points and show the tooltip.
Related
- Line Chart, Area Chart, Bar Chart, Scatter Chart and Candlestick Chart: presets for one series
- Chart Colors: change the colors, or pin a chart to light or dark
- Theming: the theme colors marks can use
- Dark mode: switch the app's
.darkclass <svg>: the element the marks are drawn with
