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