2021-07-09 21:42:01 +03:00
|
|
|
import format from 'date-fns/format';
|
2023-04-15 21:31:20 +03:00
|
|
|
import { FC, useRef } from 'react';
|
2021-04-13 05:56:37 +03:00
|
|
|
|
2023-04-15 21:31:20 +03:00
|
|
|
import { DownloadOutlined } from '@ant-design/icons';
|
2023-02-03 01:34:44 +03:00
|
|
|
import {
|
|
|
|
Chart as ChartJS,
|
|
|
|
CategoryScale,
|
|
|
|
LinearScale,
|
|
|
|
PointElement,
|
|
|
|
LineElement,
|
|
|
|
Title,
|
|
|
|
Tooltip,
|
|
|
|
Legend,
|
|
|
|
LogarithmicScale,
|
2023-03-14 01:51:46 +03:00
|
|
|
ChartOptions,
|
2023-02-03 01:34:44 +03:00
|
|
|
} from 'chart.js';
|
|
|
|
import { Line } from 'react-chartjs-2';
|
2023-04-15 21:31:20 +03:00
|
|
|
import { Button } from 'antd';
|
2023-02-03 01:34:44 +03:00
|
|
|
|
|
|
|
ChartJS.register(
|
|
|
|
CategoryScale,
|
|
|
|
LogarithmicScale,
|
|
|
|
LinearScale,
|
|
|
|
PointElement,
|
|
|
|
LineElement,
|
|
|
|
Title,
|
|
|
|
Tooltip,
|
|
|
|
Legend,
|
|
|
|
);
|
2020-11-01 09:17:44 +03:00
|
|
|
|
2020-11-01 10:01:37 +03:00
|
|
|
interface TimedValue {
|
|
|
|
time: Date;
|
2020-11-03 07:49:52 +03:00
|
|
|
value: number;
|
2023-02-03 01:34:44 +03:00
|
|
|
pointStyle?: boolean | string;
|
|
|
|
pointRadius?: number;
|
2020-11-01 10:01:37 +03:00
|
|
|
}
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type ChartProps = {
|
2021-02-04 20:17:20 +03:00
|
|
|
data?: TimedValue[];
|
|
|
|
title?: string;
|
|
|
|
color: string;
|
|
|
|
unit: string;
|
2022-03-17 03:54:34 +03:00
|
|
|
yFlipped?: boolean;
|
|
|
|
yLogarithmic?: boolean;
|
2021-02-04 20:17:20 +03:00
|
|
|
dataCollections?: any[];
|
2023-03-14 01:51:46 +03:00
|
|
|
minYValue?: number;
|
|
|
|
yStepSize?: number;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2020-10-28 10:53:24 +03:00
|
|
|
|
2020-11-25 11:17:35 +03:00
|
|
|
function createGraphDataset(dataArray) {
|
2020-11-29 02:28:39 +03:00
|
|
|
const dataValues = {};
|
2020-11-25 11:17:35 +03:00
|
|
|
dataArray.forEach(item => {
|
|
|
|
const dateObject = new Date(item.time);
|
2021-04-13 05:56:37 +03:00
|
|
|
const dateString = format(dateObject, 'H:mma');
|
2020-12-16 06:52:01 +03:00
|
|
|
dataValues[dateString] = item.value;
|
2021-02-04 20:19:16 +03:00
|
|
|
});
|
2020-11-25 11:17:35 +03:00
|
|
|
return dataValues;
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export const Chart: FC<ChartProps> = ({
|
2022-03-17 03:54:34 +03:00
|
|
|
data,
|
|
|
|
title,
|
|
|
|
color,
|
|
|
|
unit,
|
|
|
|
dataCollections,
|
|
|
|
yFlipped,
|
|
|
|
yLogarithmic,
|
2023-03-14 01:51:46 +03:00
|
|
|
minYValue,
|
|
|
|
yStepSize = 0,
|
2022-09-07 10:00:28 +03:00
|
|
|
}) => {
|
2020-11-29 02:28:39 +03:00
|
|
|
const renderData = [];
|
2020-11-01 10:01:37 +03:00
|
|
|
|
2023-04-15 21:31:20 +03:00
|
|
|
const chartRef = useRef(null);
|
|
|
|
const downloadChart = () => {
|
|
|
|
if (chartRef.current) {
|
|
|
|
const link = document.createElement('a');
|
|
|
|
link.download = 'chart.png';
|
|
|
|
link.href = chartRef.current.canvas.toDataURL();
|
|
|
|
link.click();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-25 11:07:46 +03:00
|
|
|
if (data && data.length > 0) {
|
|
|
|
renderData.push({
|
2023-02-03 01:34:44 +03:00
|
|
|
id: title,
|
|
|
|
label: title,
|
|
|
|
backgroundColor: color,
|
|
|
|
borderColor: color,
|
|
|
|
borderWidth: 3,
|
2021-02-04 20:17:20 +03:00
|
|
|
data: createGraphDataset(data),
|
2020-11-01 10:01:37 +03:00
|
|
|
});
|
2020-10-29 05:28:52 +03:00
|
|
|
}
|
2020-11-01 09:17:44 +03:00
|
|
|
|
2020-11-25 11:07:46 +03:00
|
|
|
dataCollections.forEach(collection => {
|
2021-02-04 20:17:20 +03:00
|
|
|
renderData.push({
|
2023-02-03 01:34:44 +03:00
|
|
|
id: collection.name,
|
|
|
|
label: collection.name,
|
2021-02-04 20:17:20 +03:00
|
|
|
data: createGraphDataset(collection.data),
|
2023-02-03 01:34:44 +03:00
|
|
|
backgroundColor: collection.color,
|
|
|
|
borderColor: collection.color,
|
|
|
|
borderWidth: 3,
|
|
|
|
pointStyle: collection.pointStyle || 'circle',
|
|
|
|
radius: collection.pointRadius || 1,
|
2021-02-04 20:17:20 +03:00
|
|
|
});
|
2020-11-25 11:07:46 +03:00
|
|
|
});
|
2020-11-12 06:05:38 +03:00
|
|
|
|
2022-03-17 03:54:34 +03:00
|
|
|
const options = {
|
2023-02-03 01:34:44 +03:00
|
|
|
responsive: true,
|
2023-03-14 03:01:34 +03:00
|
|
|
clip: false,
|
2023-02-03 01:34:44 +03:00
|
|
|
|
2022-03-17 03:54:34 +03:00
|
|
|
scales: {
|
2023-02-03 01:34:44 +03:00
|
|
|
y: {
|
|
|
|
type: yLogarithmic ? ('logarithmic' as const) : ('linear' as const),
|
|
|
|
reverse: yFlipped,
|
2023-03-14 01:51:46 +03:00
|
|
|
min: minYValue,
|
|
|
|
ticks: {
|
|
|
|
stepSize: yStepSize,
|
|
|
|
},
|
2023-02-03 01:34:44 +03:00
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: unit,
|
|
|
|
},
|
2022-03-17 03:54:34 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-27 09:53:04 +03:00
|
|
|
return (
|
2021-02-04 20:17:20 +03:00
|
|
|
<div className="line-chart-container">
|
2023-03-14 01:51:46 +03:00
|
|
|
<Line
|
2023-04-15 21:31:20 +03:00
|
|
|
ref={chartRef}
|
2023-03-14 01:51:46 +03:00
|
|
|
data={{ datasets: renderData }}
|
|
|
|
options={options as ChartOptions<'line'>}
|
|
|
|
height="70vh"
|
|
|
|
/>
|
2023-04-15 21:31:20 +03:00
|
|
|
<Button
|
|
|
|
size="small"
|
|
|
|
onClick={downloadChart}
|
|
|
|
type="ghost"
|
|
|
|
icon={<DownloadOutlined />}
|
|
|
|
className="download-btn"
|
|
|
|
/>
|
2020-11-29 05:14:08 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2020-11-01 09:17:44 +03:00
|
|
|
|
|
|
|
Chart.defaultProps = {
|
|
|
|
dataCollections: [],
|
2020-11-13 14:43:28 +03:00
|
|
|
data: [],
|
|
|
|
title: '',
|
2022-03-17 03:54:34 +03:00
|
|
|
yFlipped: false,
|
|
|
|
yLogarithmic: false,
|
2020-11-01 09:17:44 +03:00
|
|
|
};
|