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