2021-07-09 20:42:01 +02:00
|
|
|
import format from 'date-fns/format';
|
2022-09-07 09:00:28 +02:00
|
|
|
import { FC } from 'react';
|
2021-04-12 19:56:37 -07:00
|
|
|
|
2023-02-02 14:34:44 -08:00
|
|
|
import {
|
|
|
|
Chart as ChartJS,
|
|
|
|
CategoryScale,
|
|
|
|
LinearScale,
|
|
|
|
PointElement,
|
|
|
|
LineElement,
|
|
|
|
Title,
|
|
|
|
Tooltip,
|
|
|
|
Legend,
|
|
|
|
LogarithmicScale,
|
|
|
|
} from 'chart.js';
|
|
|
|
import { Line } from 'react-chartjs-2';
|
|
|
|
|
|
|
|
ChartJS.register(
|
|
|
|
CategoryScale,
|
|
|
|
LogarithmicScale,
|
|
|
|
LinearScale,
|
|
|
|
PointElement,
|
|
|
|
LineElement,
|
|
|
|
Title,
|
|
|
|
Tooltip,
|
|
|
|
Legend,
|
|
|
|
);
|
2020-10-31 23:17:44 -07:00
|
|
|
|
2020-11-01 00:01:37 -07:00
|
|
|
interface TimedValue {
|
|
|
|
time: Date;
|
2020-11-02 20:49:52 -08:00
|
|
|
value: number;
|
2023-02-02 14:34:44 -08:00
|
|
|
pointStyle?: boolean | string;
|
|
|
|
pointRadius?: number;
|
2020-11-01 00:01:37 -07:00
|
|
|
}
|
|
|
|
|
2022-09-07 09:00:28 +02:00
|
|
|
export type ChartProps = {
|
2021-02-04 09:17:20 -08:00
|
|
|
data?: TimedValue[];
|
|
|
|
title?: string;
|
|
|
|
color: string;
|
|
|
|
unit: string;
|
2022-03-16 17:54:34 -07:00
|
|
|
yFlipped?: boolean;
|
|
|
|
yLogarithmic?: boolean;
|
2021-02-04 09:17:20 -08:00
|
|
|
dataCollections?: any[];
|
2022-09-07 09:00:28 +02:00
|
|
|
};
|
2020-10-28 00:53:24 -07:00
|
|
|
|
2020-11-25 00:17:35 -08:00
|
|
|
function createGraphDataset(dataArray) {
|
2020-11-28 15:28:39 -08:00
|
|
|
const dataValues = {};
|
2020-11-25 00:17:35 -08:00
|
|
|
dataArray.forEach(item => {
|
|
|
|
const dateObject = new Date(item.time);
|
2021-04-12 19:56:37 -07:00
|
|
|
const dateString = format(dateObject, 'H:mma');
|
2020-12-15 19:52:01 -08:00
|
|
|
dataValues[dateString] = item.value;
|
2021-02-04 09:19:16 -08:00
|
|
|
});
|
2020-11-25 00:17:35 -08:00
|
|
|
return dataValues;
|
|
|
|
}
|
|
|
|
|
2022-09-07 09:00:28 +02:00
|
|
|
export const Chart: FC<ChartProps> = ({
|
2022-03-16 17:54:34 -07:00
|
|
|
data,
|
|
|
|
title,
|
|
|
|
color,
|
|
|
|
unit,
|
|
|
|
dataCollections,
|
|
|
|
yFlipped,
|
|
|
|
yLogarithmic,
|
2022-09-07 09:00:28 +02:00
|
|
|
}) => {
|
2020-11-28 15:28:39 -08:00
|
|
|
const renderData = [];
|
2020-11-01 00:01:37 -07:00
|
|
|
|
2020-11-25 00:07:46 -08:00
|
|
|
if (data && data.length > 0) {
|
|
|
|
renderData.push({
|
2023-02-02 14:34:44 -08:00
|
|
|
id: title,
|
|
|
|
label: title,
|
|
|
|
backgroundColor: color,
|
|
|
|
borderColor: color,
|
|
|
|
borderWidth: 3,
|
2021-02-04 09:17:20 -08:00
|
|
|
data: createGraphDataset(data),
|
2020-11-01 00:01:37 -07:00
|
|
|
});
|
2020-10-28 19:28:52 -07:00
|
|
|
}
|
2020-10-31 23:17:44 -07:00
|
|
|
|
2020-11-25 00:07:46 -08:00
|
|
|
dataCollections.forEach(collection => {
|
2021-02-04 09:17:20 -08:00
|
|
|
renderData.push({
|
2023-02-02 14:34:44 -08:00
|
|
|
id: collection.name,
|
|
|
|
label: collection.name,
|
2021-02-04 09:17:20 -08:00
|
|
|
data: createGraphDataset(collection.data),
|
2023-02-02 14:34:44 -08:00
|
|
|
backgroundColor: collection.color,
|
|
|
|
borderColor: collection.color,
|
|
|
|
borderWidth: 3,
|
|
|
|
pointStyle: collection.pointStyle || 'circle',
|
|
|
|
radius: collection.pointRadius || 1,
|
2021-02-04 09:17:20 -08:00
|
|
|
});
|
2020-11-25 00:07:46 -08:00
|
|
|
});
|
2020-11-11 19:05:38 -08:00
|
|
|
|
2022-03-16 17:54:34 -07:00
|
|
|
const options = {
|
2023-02-02 14:34:44 -08:00
|
|
|
responsive: true,
|
|
|
|
|
2022-03-16 17:54:34 -07:00
|
|
|
scales: {
|
2023-02-02 14:34:44 -08:00
|
|
|
y: {
|
|
|
|
type: yLogarithmic ? ('logarithmic' as const) : ('linear' as const),
|
|
|
|
reverse: yFlipped,
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: unit,
|
|
|
|
},
|
2022-03-16 17:54:34 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-26 23:53:04 -07:00
|
|
|
return (
|
2021-02-04 09:17:20 -08:00
|
|
|
<div className="line-chart-container">
|
2023-02-02 14:34:44 -08:00
|
|
|
<Line data={{ datasets: renderData }} options={options} height="70vh" />
|
2020-11-28 18:14:08 -08:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 09:00:28 +02:00
|
|
|
};
|
2020-10-31 23:17:44 -07:00
|
|
|
|
|
|
|
Chart.defaultProps = {
|
|
|
|
dataCollections: [],
|
2020-11-13 03:43:28 -08:00
|
|
|
data: [],
|
|
|
|
title: '',
|
2022-03-16 17:54:34 -07:00
|
|
|
yFlipped: false,
|
|
|
|
yLogarithmic: false,
|
2020-10-31 23:17:44 -07:00
|
|
|
};
|