Demo
Titles, data labels, markers and annotations
Documented in Chart elements.
Titles, data labels, markers and annotations
A titled column chart with positioned, rotated and templated data labels and annotations, and a line chart with marker shapes, custom SVG markers and labels, smart label overlap, last-value badges and inline series labels.
Column chart: Sign-ups, 1 series, x from Jan to Aug. Sign-ups: 8 points, minimum 0, maximum 179, trending up.
| x | Sign-ups |
|---|---|
| Jan | 120 |
| Feb | 167 |
| Mar | 124 |
| Apr | 0 |
| May | 128 |
| Jun | 175 |
| Jul | 132 |
| Aug | 179 |
Line chart: Price per kilo, 3 series, x from Jan to Aug. Coffee: 8 points, minimum 4.1, maximum 4.5, trending up. Tea: 8 points, minimum 4, maximum 4.4, trending up. Cocoa: 8 points, minimum 3.2, maximum 4.04, trending up.
| x | Coffee | Tea | Cocoa |
|---|---|---|---|
| Jan | $4.10 | $4.00 | $3.20 |
| Feb | $4.40 | $4.20 | $3.32 |
| Mar | $4.20 | $4.40 | $3.44 |
| Apr | $4.50 | $4.10 | $3.56 |
| May | $4.30 | $4.30 | $3.68 |
| Jun | $4.10 | $4.00 | $3.80 |
| Jul | $4.40 | $4.20 | $3.92 |
| Aug | $4.20 | $4.40 | $4.04 |
Demo.tsx
'use client';
import { useState } from 'react';
import { Chart } from '@salmonui/charts/chart';
import type { ChartDataLabels } from '@salmonui/charts/chart';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];
const prices = months.map((month, i) => ({ month, coffee: 4.1 + ((i * 3) % 5) / 10, tea: 4.0 + ((i * 2) % 5) / 10, cocoa: 3.2 + i * 0.12 }));
const signups = months.map((month, i) => ({ month, signups: i === 3 ? 0 : 120 + ((i * 47) % 90) }));
type Position = NonNullable<ChartDataLabels['position']>;
type Overlap = NonNullable<ChartDataLabels['overlap']>;
export default function ChartLabelsMarkersDemo() {
const [position, setPosition] = useState<Position>('top');
const [angle, setAngle] = useState(0);
const [showZero, setShowZero] = useState(true);
const [overlap, setOverlap] = useState<Overlap>('shiftY');
const [dedupe, setDedupe] = useState(false);
const [badges, setBadges] = useState(true);
return (
<div className="demo-stack">
<div className="demo-toolbar">
<label>
Label position
<Select aria-label="Label position" data={['outside', 'top', 'middle', 'bottom', 'auto']} value={position} onValueChange={(value) => value != null && setPosition(value as Position)} />
</label>
<label>
Angle
<Select aria-label="Angle" data={[{ value: 0, label: '0' }, { value: -45, label: '-45' }, { value: -90, label: '-90' }]} value={angle} onValueChange={(value) => value != null && setAngle(Number(value))} />
</label>
<Checkbox checked={showZero} onCheckedChange={(checked) => setShowZero(checked)}>
showZero
</Checkbox>
</div>
<Chart
title="Sign-ups"
subtitle="April had a site outage"
data={signups}
height={260}
legend={false}
annotations={[{ x: 'Apr', y: 60, content: <span className="demo-badge">Outage</span> }, { x: 128, y: 18, units: 'pixel', content: <span className="demo-badge" data-tone="info">Preliminary</span> }]}
series={[
{
type: 'column',
name: 'Sign-ups',
xField: 'month',
yField: 'signups',
labels: { position, angle, showZero, format: '{value} users' },
},
]}
/>
<div className="demo-toolbar">
<label>
Label overlap
<Select aria-label="Label overlap" data={['none', 'hide', 'shiftY', 'shiftX', 'rotate90']} value={overlap} onValueChange={(value) => value != null && setOverlap(value as Overlap)} />
</label>
<Checkbox checked={dedupe} onCheckedChange={(checked) => setDedupe(checked)}>
dedupe
</Checkbox>
<Checkbox checked={badges} onCheckedChange={(checked) => setBadges(checked)}>
Last value and inline labels
</Checkbox>
</div>
<Chart
title="Price per kilo"
data={prices}
height={300}
legend={false}
yAxis={{ format: 'c2' }}
locale="en-US"
series={[
{ type: 'line', name: 'Coffee', xField: 'month', yField: 'coffee', markers: { shape: 'diamond', size: 8 }, labels: { format: 'c1', overlap, dedupe }, lastValue: badges, inlineLabel: badges },
{ type: 'line', name: 'Tea', xField: 'month', yField: 'tea', markers: { shape: 'triangle', size: 8, filled: false }, labels: { format: 'c1', overlap, dedupe }, lastValue: badges ? { format: 'c2' } : false, inlineLabel: badges },
{
type: 'line',
name: 'Cocoa',
xField: 'month',
yField: 'cocoa',
// Custom SVG markers and labels rendered from functions.
renderMarker: (p) => <rect x={-5} y={-5} width={10} height={10} rx={2} fill={p.color} />,
labels: { position: 'bottom', render: (p) => <tspan fontStyle="italic">{p.index % 2 === 0 ? p.yText : ''}</tspan> },
inlineLabel: badges,
},
]}
/>
</div>
);
}
The sign-ups chart has a title and subtitle (the figure caption).
Its labels take position, angle, showZero (April is 0) and a
'{value} users' template. Two annotations place a badge at a data
point and one at a pixel position.
The price chart draws diamond and open triangle markers; cocoa uses
renderMarker and labels.render for SVG content. Coffee and tea labels
collide: labels.overlap hides them, shifts them or rotates them, and
dedupe drops a label equal to the previous one. lastValue draws the
last value at the plot edge and inlineLabel the series name at the end
of the line.