Demo
Trendlines and empty points
Documented in Chart analysis.
Trendlines and empty points
Fit a linear, exponential, logarithmic, polynomial, power or moving-average trendline with a forecast, a forced intercept and markers, and choose how missing values are drawn.
Scatter chart: Monthly units with a trendline, 1 series, Month from 5 to 30. Units: 24 points, minimum 40, maximum 153, trending up.
| Month | Units |
|---|---|
| 1 | 40 |
| 2 | 52 |
| 3 | 41 |
| 4 | 45 |
| 5 | 63 |
| 6 | 57 |
| 7 | 54 |
| 8 | 72 |
| 9 | 75 |
| 10 | 66 |
| 11 | 81 |
| 12 | 93 |
| 13 | 83 |
| 14 | 91 |
| 15 | 109 |
| 16 | 104 |
| 17 | 104 |
| 18 | 124 |
| 19 | 127 |
| 20 | 121 |
| 21 | 139 |
| 22 | 150 |
| 23 | 142 |
| 24 | 153 |
Line chart: Sensor readings with missing values, 1 series, x from 8:00 to 19:00. Reading: 8 points, minimum 12, maximum 19, trending up.
| x | Reading |
|---|---|
| 8:00 | 12 |
| 9:00 | 14 |
| 10:00 | |
| 11:00 | 17 |
| 12:00 | 19 |
| 13:00 | |
| 14:00 | |
| 15:00 | 18 |
| 16:00 | 16 |
| 17:00 | 15 |
| 18:00 | |
| 19:00 | 13 |
Demo.tsx
'use client';
import { useState } from 'react';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { NumberField } from '@salmonui/inputs/number-field';
import { Select } from '@salmonui/dropdowns/select';
import { Chart } from '@salmonui/charts/chart';
import type { ChartTrendline } from '@salmonui/charts/chart';
const sales = Array.from({ length: 24 }, (_, i) => ({ month: i + 1, units: Math.round(40 + 3.2 * i + 0.08 * i * i + 9 * Math.sin(i * 1.9)) }));
// Missing readings (null) in a sensor log.
const readings = [12, 14, null, 17, 19, null, null, 18, 16, 15, null, 13].map((value, i) => ({ hour: `${8 + i}:00`, value }));
type Kind = ChartTrendline['kind'];
type Empty = 'gap' | 'zero' | 'drop' | 'average';
export default function ChartTrendlinesDemo() {
const [kind, setKind] = useState<Kind>('polynomial');
const [order, setOrder] = useState(2);
const [forward, setForward] = useState(6);
const [intercept, setIntercept] = useState(false);
const [markers, setMarkers] = useState(false);
const [empty, setEmpty] = useState<Empty>('gap');
return (
<div className="demo-stack">
<div className="demo-toolbar">
<label>
Trendline
<Select aria-label="Trendline" data={(['linear', 'exponential', 'logarithmic', 'polynomial', 'power', 'movingAverage'] as const).map((k) => ({ value: k, label: k }))} value={kind} onValueChange={(value) => value != null && setKind(value as Kind)} />
</label>
<label>
Order
<NumberField aria-label="Order" min={2} max={6} value={order} onValueChange={(next) => next != null && setOrder(next)} disabled={kind !== 'polynomial'} style={{ inlineSize: '6em' }} />
</label>
<label>
Forecast forward
<NumberField aria-label="Forecast forward" min={0} max={12} value={forward} onValueChange={(next) => next != null && setForward(next)} disabled={kind === 'movingAverage'} style={{ inlineSize: '6em' }} />
</label>
<Checkbox checked={intercept} onCheckedChange={(checked) => setIntercept(checked)} disabled={kind !== 'linear' && kind !== 'polynomial' && kind !== 'exponential'}>Intercept at 30</Checkbox>
<Checkbox checked={markers} onCheckedChange={(checked) => setMarkers(checked)}>Trendline markers</Checkbox>
</div>
<Chart
title="Monthly units with a trendline"
data={sales}
height={300}
xAxis={{ title: 'Month' }}
series={[
{
type: 'scatter',
name: 'Units',
xField: 'month',
yField: 'units',
trendlines: [
{
kind,
order,
period: 3,
forecast: kind === 'movingAverage' ? undefined : { forward },
intercept: intercept ? 30 : undefined,
name: `Trend (${kind})`,
markers,
},
],
},
]}
/>
<div className="demo-toolbar">
<label>
Empty points
<Select aria-label="Empty points" data={[{ value: 'gap', label: 'gap' }, { value: 'zero', label: 'zero' }, { value: 'drop', label: 'drop' }, { value: 'average', label: 'average' }]} value={empty} onValueChange={(value) => value != null && setEmpty(value as Empty)} />
</label>
</div>
<Chart
title="Sensor readings with missing values"
data={readings}
height={240}
legend={false}
series={[{ type: 'line', name: 'Reading', xField: 'hour', yField: 'value', markers: true, emptyPoints: empty }]}
/>
</div>
);
}
series.trendlines fits the selected kind to the scatter series
(order for polynomials, period: 3 for the moving average).
forecast.forward extends the line by months, intercept forces it
through (0, 30), name labels its legend entry and tooltip, and markers
draws points on it.
The sensor log has missing readings (null): emptyPoints breaks the
line ('gap'), plots 0, skips the point ('drop') or uses the average of
the neighbors. The accessible table marks the empty points.