2021-01-03 13:13:28 +03:00
|
|
|
import { LineChart } from 'react-chartkick';
|
2020-11-29 05:14:08 +03:00
|
|
|
import 'chart.js';
|
2020-12-23 10:15:37 +03:00
|
|
|
import format from 'date-fns/format';
|
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;
|
2020-11-01 10:01:37 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 10:53:24 +03:00
|
|
|
interface ChartProps {
|
2021-02-04 20:17:20 +03:00
|
|
|
data?: TimedValue[];
|
|
|
|
title?: string;
|
|
|
|
color: string;
|
|
|
|
unit: string;
|
|
|
|
dataCollections?: any[];
|
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);
|
2020-12-16 06:52:01 +03:00
|
|
|
const dateString = format(dateObject, 'p P');
|
|
|
|
dataValues[dateString] = item.value;
|
2021-02-04 20:19:16 +03:00
|
|
|
});
|
2020-11-25 11:17:35 +03:00
|
|
|
return dataValues;
|
|
|
|
}
|
|
|
|
|
2020-11-12 06:05:38 +03:00
|
|
|
export default function Chart({ data, title, color, unit, dataCollections }: ChartProps) {
|
2020-11-29 02:28:39 +03:00
|
|
|
const renderData = [];
|
2020-11-01 10:01:37 +03:00
|
|
|
|
2020-11-25 11:07:46 +03:00
|
|
|
if (data && data.length > 0) {
|
|
|
|
renderData.push({
|
|
|
|
name: title,
|
2020-11-29 02:28:39 +03:00
|
|
|
color,
|
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({
|
|
|
|
name: collection.name,
|
|
|
|
data: createGraphDataset(collection.data),
|
|
|
|
color: collection.color,
|
|
|
|
});
|
2020-11-25 11:07:46 +03:00
|
|
|
});
|
2020-11-12 06:05:38 +03:00
|
|
|
|
2020-10-27 09:53:04 +03:00
|
|
|
return (
|
2021-02-04 20:17:20 +03:00
|
|
|
<div className="line-chart-container">
|
2020-11-29 05:14:08 +03:00
|
|
|
<LineChart
|
|
|
|
xtitle="Time"
|
|
|
|
ytitle={title}
|
|
|
|
suffix={unit}
|
|
|
|
legend="bottom"
|
|
|
|
color={color}
|
|
|
|
data={renderData}
|
|
|
|
download={title}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2020-10-27 09:53:04 +03:00
|
|
|
}
|
2020-11-01 09:17:44 +03:00
|
|
|
|
|
|
|
Chart.defaultProps = {
|
|
|
|
dataCollections: [],
|
2020-11-13 14:43:28 +03:00
|
|
|
data: [],
|
|
|
|
title: '',
|
2020-11-01 09:17:44 +03:00
|
|
|
};
|