|
| 1 | +import { |
| 2 | + Chart as ChartJS, |
| 3 | + CategoryScale, |
| 4 | + LinearScale, |
| 5 | + PointElement, |
| 6 | + LineElement, |
| 7 | + Title, |
| 8 | + TimeScale, |
| 9 | + Tooltip, |
| 10 | + Legend, |
| 11 | + ChartData, |
| 12 | + ChartOptions, |
| 13 | + DefaultDataPoint, |
| 14 | + ChartDataset, |
| 15 | +} from 'chart.js' |
| 16 | +import { DateTime } from 'luxon' |
| 17 | +import { Line } from 'react-chartjs-2' |
| 18 | +import 'chartjs-adapter-luxon' // registers the Luxon adapter for the x-axis time scale |
| 19 | + |
| 20 | +ChartJS.register(TimeScale, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend) |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +const PALETTE: [string, string][] = [ |
| 25 | + ['rgb(255, 18, 67)', 'rgba(255, 18, 67, 0.5)'], |
| 26 | + ['rgb(125, 0, 35)', 'rgba(125, 0, 35, 0.5)'], |
| 27 | + ['rgb(36, 55, 70)', 'rgba(36, 55, 70, 0.5)'], |
| 28 | + ['rgb(0, 112, 121)', 'rgba(0, 112, 121, 0.5)'], |
| 29 | +] |
| 30 | + |
| 31 | +interface TimeseriesLinePlotDataPoint { |
| 32 | + time: Date, |
| 33 | + value: number |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +export interface TimeseriesLinePlotData { |
| 38 | + [id: string]: TimeseriesLinePlotDataPoint[] |
| 39 | +} |
| 40 | + |
| 41 | +interface Props { |
| 42 | + data: TimeseriesLinePlotData |
| 43 | + yLabel: string |
| 44 | +} |
| 45 | + |
| 46 | +export const TimeseriesLinePlot = ({ data, yLabel }: Props) => { |
| 47 | + const options: ChartOptions<'line'> = { |
| 48 | + responsive: true, |
| 49 | + spanGaps: true, |
| 50 | + |
| 51 | + plugins: { |
| 52 | + legend: { |
| 53 | + position: 'right' as const, |
| 54 | + }, |
| 55 | + title: { |
| 56 | + display: false, |
| 57 | + }, |
| 58 | + }, |
| 59 | + scales: { |
| 60 | + x: { |
| 61 | + type: 'time', |
| 62 | + adapters: { |
| 63 | + date: { |
| 64 | + zone: 'Europe/Oslo', |
| 65 | + }, |
| 66 | + }, |
| 67 | + time: { |
| 68 | + // Luxon format tokens (https://moment.github.io/luxon/#/formatting?id=table-of-tokens) |
| 69 | + tooltipFormat: 'dd.MM.yy HH:mm', |
| 70 | + displayFormats: { |
| 71 | + millisecond: 'HH:mm:ss.SSS', |
| 72 | + second: 'HH:mm:ss', |
| 73 | + minute: 'HH:mm', |
| 74 | + hour: 'HH:mm', |
| 75 | + day: 'dd.MM', |
| 76 | + week: 'dd.MM', |
| 77 | + month: 'MM.yyyy', |
| 78 | + quarter: 'qq yyyy', |
| 79 | + year: 'yyyy', |
| 80 | + }, |
| 81 | + }, |
| 82 | + title: { |
| 83 | + display: true, |
| 84 | + text: 'Time' |
| 85 | + } |
| 86 | + }, |
| 87 | + y: { |
| 88 | + title: { |
| 89 | + display: true, |
| 90 | + text: yLabel |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const labels: string[] = [] |
| 97 | + // @ts-expect-error ; Date isn't assignable to number for x-axis value - assumption: library can handle it anyways |
| 98 | + const datasets: ChartDataset<'line', DefaultDataPoint<'line'>>[] = Object.entries(data).map(([id, dataPointArray], index:number) => { |
| 99 | + const [borderColor, backgroundColor] = PALETTE[index % PALETTE.length] |
| 100 | + return { |
| 101 | + label: id, |
| 102 | + data: dataPointArray.map(dataPoint => ({x:DateTime.fromJSDate(dataPoint.time).toISO(), y:dataPoint.value})), |
| 103 | + borderColor, |
| 104 | + backgroundColor, |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + const chartData: ChartData<'line'> = { labels, datasets } |
| 109 | + return <Line options={options} data={chartData} /> |
| 110 | +} |
0 commit comments