2021-07-09 20:42:01 +02:00
|
|
|
import ChartJs from 'chart.js/auto';
|
|
|
|
import Chartkick from 'chartkick';
|
|
|
|
import format from 'date-fns/format';
|
2021-01-03 11:13:28 +01:00
|
|
|
import { LineChart } from 'react-chartkick';
|
2021-04-13 04:56:37 +02:00
|
|
|
|
2021-07-09 20:42:01 +02:00
|
|
|
// from https://github.com/ankane/chartkick.js/blob/master/chart.js/chart.esm.js
|
|
|
|
Chartkick.use(ChartJs);
|
2020-11-01 07:17:44 +01:00
|
|
|
|
2020-11-01 08:01:37 +01:00
|
|
|
interface TimedValue {
|
|
|
|
time: Date;
|
2020-11-03 05:49:52 +01:00
|
|
|
value: number;
|
2020-11-01 08:01:37 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 08:53:24 +01:00
|
|
|
interface ChartProps {
|
2021-02-04 18:17:20 +01:00
|
|
|
data?: TimedValue[];
|
|
|
|
title?: string;
|
|
|
|
color: string;
|
|
|
|
unit: string;
|
|
|
|
dataCollections?: any[];
|
2020-10-28 08:53:24 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 09:17:35 +01:00
|
|
|
function createGraphDataset(dataArray) {
|
2020-11-29 00:28:39 +01:00
|
|
|
const dataValues = {};
|
2020-11-25 09:17:35 +01:00
|
|
|
dataArray.forEach(item => {
|
|
|
|
const dateObject = new Date(item.time);
|
2021-04-13 04:56:37 +02:00
|
|
|
const dateString = format(dateObject, 'H:mma');
|
2020-12-16 04:52:01 +01:00
|
|
|
dataValues[dateString] = item.value;
|
2021-02-04 18:19:16 +01:00
|
|
|
});
|
2020-11-25 09:17:35 +01:00
|
|
|
return dataValues;
|
|
|
|
}
|
|
|
|
|
2020-11-12 04:05:38 +01:00
|
|
|
export default function Chart({ data, title, color, unit, dataCollections }: ChartProps) {
|
2020-11-29 00:28:39 +01:00
|
|
|
const renderData = [];
|
2020-11-01 08:01:37 +01:00
|
|
|
|
2020-11-25 09:07:46 +01:00
|
|
|
if (data && data.length > 0) {
|
|
|
|
renderData.push({
|
|
|
|
name: title,
|
2020-11-29 00:28:39 +01:00
|
|
|
color,
|
2021-02-04 18:17:20 +01:00
|
|
|
data: createGraphDataset(data),
|
2020-11-01 08:01:37 +01:00
|
|
|
});
|
2020-10-29 03:28:52 +01:00
|
|
|
}
|
2020-11-01 07:17:44 +01:00
|
|
|
|
2020-11-25 09:07:46 +01:00
|
|
|
dataCollections.forEach(collection => {
|
2021-02-04 18:17:20 +01:00
|
|
|
renderData.push({
|
|
|
|
name: collection.name,
|
|
|
|
data: createGraphDataset(collection.data),
|
|
|
|
color: collection.color,
|
|
|
|
});
|
2020-11-25 09:07:46 +01:00
|
|
|
});
|
2020-11-12 04:05:38 +01:00
|
|
|
|
2020-10-27 07:53:04 +01:00
|
|
|
return (
|
2021-02-04 18:17:20 +01:00
|
|
|
<div className="line-chart-container">
|
2020-11-29 03:14:08 +01:00
|
|
|
<LineChart
|
|
|
|
xtitle="Time"
|
|
|
|
ytitle={title}
|
|
|
|
suffix={unit}
|
|
|
|
legend="bottom"
|
|
|
|
color={color}
|
|
|
|
data={renderData}
|
|
|
|
download={title}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2020-10-27 07:53:04 +01:00
|
|
|
}
|
2020-11-01 07:17:44 +01:00
|
|
|
|
|
|
|
Chart.defaultProps = {
|
|
|
|
dataCollections: [],
|
2020-11-13 12:43:28 +01:00
|
|
|
data: [],
|
|
|
|
title: '',
|
2020-11-01 07:17:44 +01:00
|
|
|
};
|