LineChart
Muestra cómo cambia algo en el tiempo. Pasa el puntero, o enfócalo y usa las flechas, para leer todas las series en un momento.
import { LineChart, AreaChart, ChartFrame } from '@sypnia/ui/react'
Varias series
Una cruz se ajusta a la hora más cercana y una sola pista lista todas las series, así el puntero nunca tiene que caer justo en una línea.
- Reads
- Writes
- Sign-ins
Ver como tabla
| Hour | Reads | Writes | Sign-ins |
|---|---|---|---|
| 00:00 | 603 | 311 | 68 |
| 01:00 | 562 | 290 | 71 |
| 02:00 | 539 | 274 | 78 |
| 03:00 | 537 | 264 | 89 |
| 04:00 | 556 | 260 | 104 |
| 05:00 | 551 | 220 | 77 |
| 06:00 | 609 | 233 | 99 |
| 07:00 | 682 | 254 | 122 |
| 08:00 | 766 | 282 | 147 |
| 09:00 | 856 | 316 | 172 |
| 10:00 | 901 | 309 | 150 |
| 11:00 | 985 | 349 | 172 |
| 12:00 | 1058 | 389 | 190 |
| 13:00 | 1116 | 427 | 205 |
| 14:00 | 1156 | 461 | 216 |
| 15:00 | 1130 | 444 | 178 |
| 16:00 | 1128 | 465 | 181 |
| 17:00 | 1105 | 478 | 180 |
| 18:00 | 1064 | 483 | 176 |
| 19:00 | 1007 | 479 | 171 |
| 20:00 | 896 | 424 | 119 |
| 21:00 | 824 | 408 | 112 |
| 22:00 | 752 | 387 | 107 |
| 23:00 | 687 | 365 | 104 |
Código
import { LineChart } from '@sypnia/ui/react';
const hours = Array.from({ length: 24 }, (_, h) => `${String(h).padStart(2, '0')}:00`);
const wave = (base: number, swing: number, shift: number) => hours.map((_, h) => Math.round(base + swing * Math.sin((h - shift) / 3.8) + (h % 5) * 9));
export default function Requests() {
return (
<LineChart title="Requests" subtitle="Per hour, last 24 hours" xLabel="Hour" x={hours}
series={[
{ id: 'read', label: 'Reads', data: wave(820, 310, 9) },
{ id: 'write', label: 'Writes', data: wave(340, 120, 11) },
{ id: 'auth', label: 'Sign-ins', data: wave(120, 60, 8) },
]} />
);
} Una serie, como área
Ver como tabla
| Day | Database |
|---|---|
| Oct 1 | 18 MB |
| Oct 2 | 19 MB |
| Oct 3 | 20 MB |
| Oct 4 | 20 MB |
| Oct 5 | 21 MB |
| Oct 6 | 22 MB |
| Oct 7 | 23 MB |
| Oct 8 | 21 MB |
| Oct 9 | 22 MB |
| Oct 10 | 23 MB |
| Oct 11 | 24 MB |
| Oct 12 | 24 MB |
| Oct 13 | 25 MB |
| Oct 14 | 26 MB |
| Oct 15 | 24 MB |
| Oct 16 | 25 MB |
| Oct 17 | 26 MB |
| Oct 18 | 27 MB |
| Oct 19 | 27 MB |
| Oct 20 | 28 MB |
| Oct 21 | 29 MB |
| Oct 22 | 28 MB |
| Oct 23 | 28 MB |
| Oct 24 | 29 MB |
| Oct 25 | 30 MB |
| Oct 26 | 31 MB |
| Oct 27 | 31 MB |
| Oct 28 | 32 MB |
| Oct 29 | 31 MB |
| Oct 30 | 32 MB |
Código
import { AreaChart, useSypnia } from '@sypnia/ui/react';
const days = Array.from({ length: 30 }, (_, d) => `Oct ${d + 1}`);
export default function Storage() {
const { format } = useSypnia();
return (
<AreaChart title="Storage used" subtitle="One series needs no legend: the title names it" xLabel="Day" x={days} height={200}
formatValue={(v) => format.bytes(v)}
series={[{ id: 'db', label: 'Database', data: days.map((_, d) => Math.round((18 + d * 0.46 + (d % 7) * 0.3) * 1_048_576)) }]} />
);
} Estados
En la primera carga el gráfico muestra la forma de lo que viene. Al actualizarse conserva el último dibujo, atenuado, así nada salta.
Código
import { useState } from 'react';
import { Button, EmptyState, LineChart, SegmentedControl } from '@sypnia/ui/react';
type State = 'loading' | 'refreshing' | 'empty' | 'error';
const x = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const data = [{ id: 'errors', label: 'Errors', data: [12, 9, 14, 31, 18, 6, 4] }];
export default function States() {
const [state, setState] = useState<State>('loading');
return (
<div className="sy-stack">
<SegmentedControl label="State" value={state} onChange={setState}
options={[{ value: 'loading', label: 'First load' }, { value: 'refreshing', label: 'Refreshing' }, { value: 'empty', label: 'Empty' }, { value: 'error', label: 'Error' }]} />
<LineChart title="Errors" subtitle="Per day, this week" x={state === 'loading' || state === 'empty' ? [] : x} series={state === 'loading' || state === 'empty' ? [] : data} height={200}
loading={state === 'loading' || state === 'refreshing'}
error={state === 'error' ? 'The errors could not be loaded. Check your connection and try again.' : undefined}
empty={<EmptyState compact icon="check" title="No errors this week." actions={<Button size="sm">See last month</Button>} />} />
</div>
);
} Skeleton
La pieza real y su skeleton: la misma caja, así nada se mueve cuando llegan los datos.
- Answered
- Failed
Ver como tabla
| Answered | Failed | |
|---|---|---|
| Mon | 820 | 12 |
| Tue | 940 | 30 |
| Wed | 1010 | 8 |
| Thu | 980 | 14 |
| Fri | 1120 | 22 |
| Sat | 610 | 5 |
| Sun | 540 | 3 |
Código
import { ChartSkeleton, LineChart } from '@sypnia/ui/react';
// The chart and its skeleton: the same height, the axes and the room of the legend.
const x = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
export default function Skeleton() {
return (
<div className="sy-grid">
<LineChart title="Requests" subtitle="Per day, last week" x={x} height={200}
series={[{ id: 'ok', label: 'Answered', data: [820, 940, 1010, 980, 1120, 610, 540] }, { id: 'err', label: 'Failed', data: [12, 30, 8, 14, 22, 5, 3] }]} />
<ChartSkeleton height={200} legend={2} />
</div>
);
} Cuándo usarlo
Úsalo cuando
- La pregunta es «¿cómo cambió esto?»: solicitudes por hora, almacenamiento por día, errores por semana.
- Hasta cinco series. Mantienen el mismo color en todas partes, en el mismo orden.
- Una cantidad que se acumula (almacenamiento, usuarios totales): usa AreaChart, el mismo gráfico con un relleno bajo la línea.
No lo uses cuando
- No hay tiempo ni orden en el eje horizontal: compara categorías con un BarChart.
- Quieres dos unidades distintas en un gráfico (solicitudes y milisegundos). Nunca dos escalas verticales: usa dos gráficos.
- Más de cinco series: agrupa el resto como «Otros», o muestra varios gráficos pequeños.
- Todo se trata de un número: usa un Stat, con un Sparkline si la forma importa.
Para todos
- El gráfico tiene un nombre que dice qué muestra y qué series tiene.
- Con el teclado: Tab hasta el gráfico, y luego Izquierda y Derecha mueven la cruz; se anuncian los valores.
- «Ver como tabla» muestra los mismos números como tabla, así nada queda disponible solo al pasar el puntero.
- Las series nunca se distinguen solo por color: siempre hay una leyenda, y el texto nunca se dibuja con el color de una serie.
- Los cinco colores de las series están revisados para daltonismo y contraste en noche y papel.
Props
| LineChart | Tipo | Por defecto |
|---|---|---|
seriesobligatorio | Series[] | |
titleobligatorio | ReactNode | |
xobligatorioThe positions along the horizontal axis: dates, hours, names. | string[] | |
areaFills under each line with a light wash: for a quantity that accumulates. | boolean | |
empty | ReactNode | |
error | ReactNode | |
formatValueHow a value is written in ticks, tooltip and table. Defaults to a compact number in the person's language. | ((v: number) => string) | |
height | number | |
loading | boolean | |
subtitle | ReactNode | |
xLabelThe header of the first column of the table view: what `x` is. | string |
| AreaChart | Tipo | Por defecto |
|---|---|---|
seriesobligatorio | Series[] | |
titleobligatorio | ReactNode | |
xobligatorioThe positions along the horizontal axis: dates, hours, names. | string[] | |
empty | ReactNode | |
error | ReactNode | |
formatValueHow a value is written in ticks, tooltip and table. Defaults to a compact number in the person's language. | ((v: number) => string) | |
height | number | |
loading | boolean | |
subtitle | ReactNode | |
xLabelThe header of the first column of the table view: what `x` is. | string |
| ChartFrame | Tipo | Por defecto |
|---|---|---|
titleobligatorio | ReactNode | |
emptyShown instead of the plot when there is nothing to draw. | ReactNode | |
error | ReactNode | |
height | number | 240 |
isEmpty | boolean | |
legendSeries to list in the legend. Not shown for a single series: the title already names it. | { label: string; line?: boolean; }[] | |
loadingNew data is on its way: the chart keeps its last drawing, dimmed. On the first load pass `loading` with no data. | boolean | |
skeletonWhat the plot shows on the first load instead of the default plot skeleton, when the chart's shape is not a plot with axes (rows of bars, a gauge). | ReactNode | |
subtitleWhat is plotted and in what unit: «Requests per minute, last 24 hours». | ReactNode | |
tableThe same numbers as a table, behind «View as table». | { columns: string[]; rows: (string | number)[][]; } |