Demo
Size, animation, RTL and JSX children
Documented in Size, animation and RTL.
Size, animation, RTL and JSX children
A chart sized by its container's CSS that relays out on resize and reports onResize, animated or still data changes, a right-to-left container, and series and axes written as JSX children.
Column chart: Bookings, 2 series, x from Jan to Jun. Hotel A: 6 points, minimum 30, maximum 60, trending up. Hotel B: 6 points, minimum 35, maximum 50, trending down.
| x | Hotel A | Hotel B |
|---|---|---|
| Jan | 30 | 50 |
| Feb | 36 | 47 |
| Mar | 42 | 44 |
| Apr | 48 | 41 |
| May | 54 | 38 |
| Jun | 60 | 35 |
onResize: not measured yet
Demo.tsx
'use client';
import { useState } from 'react';
import { Chart, ChartAxis, ChartSeries } from '@salmonui/charts/chart';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Slider } from '@salmonui/inputs/slider';
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
const base = months.map((month, i) => ({ month, a: 30 + i * 6, b: 50 - i * 3 }));
const shifted = base.map((d, i) => ({ ...d, a: d.a + (i % 2 ? 12 : -8), b: d.b + (i % 2 ? -10 : 9) }));
export default function ChartSizeRtlDemo() {
const [width, setWidth] = useState(100);
const [height, setHeight] = useState(260);
const [size, setSize] = useState('not measured yet');
const [animated, setAnimated] = useState(true);
const [alternate, setAlternate] = useState(false);
const [rtl, setRtl] = useState(false);
return (
<div className="demo-stack">
<div className="demo-toolbar">
<Slider label="Container width (%)" min={40} max={100} step={10} value={width} onValueChange={setWidth} style={{ inlineSize: 160 }} />
<Slider label="Container height (px)" min={180} max={360} step={20} value={height} onValueChange={setHeight} style={{ inlineSize: 160 }} />
<Checkbox checked={animated} onCheckedChange={(checked) => setAnimated(checked)}>
Animated
</Checkbox>
<Button variant="outline" type="button" onClick={() => setAlternate((a) => !a)}>
Change data
</Button>
<Checkbox checked={rtl} onCheckedChange={(checked) => setRtl(checked)}>
Right to left
</Checkbox>
</div>
<div dir={rtl ? 'rtl' : 'ltr'} style={{ inlineSize: `${width}%` }}>
{/* No height prop: the chart fills the CSS height of its root and relays out on resize. */}
<Chart title="Bookings" subtitle="Sized by CSS, series as JSX children" data={alternate ? shifted : base} animated={animated} style={{ height }} onResize={(s) => setSize(`${Math.round(s.width)} × ${Math.round(s.height)}`)} legend={{ position: 'top', align: 'start' }}>
<ChartAxis id="y" direction="y" title="Rooms" />
<ChartSeries type="column" name="Hotel A" xField="month" yField="a" />
<ChartSeries type="line" name="Hotel B" xField="month" yField="b" markers />
</Chart>
</div>
<p className="demo-status" aria-live="polite">
onResize: <code>{size}</code>
</p>
</div>
);
}
The chart has no height prop: it fills the CSS height of its root
(style={{ height }}) and the width of its container. Move the sliders:
one ResizeObserver writes the new size into the chart store and
onResize reports it. "Change data" plays the update transition when
animated is on (the entrance and transitions are off under
prefers-reduced-motion). "Right to left" sets dir="rtl" on the
container: the title and the legend mirror while the x axis keeps reading
left to right. The series and the y axis title are ChartSeries and
ChartAxis children, which compile to the same model as the arrays.