You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.4 KiB
69 lines
1.4 KiB
import { LineChart } from 'react-chartkick' |
|
import styles from '../../styles/styles.module.css'; |
|
import 'chart.js' |
|
|
|
const defaultProps = { |
|
active: false, |
|
payload: Object, |
|
unit: '', |
|
}; |
|
|
|
interface TimedValue { |
|
time: Date; |
|
value: number; |
|
} |
|
|
|
interface ChartProps { |
|
data?: TimedValue[], |
|
title?: string, |
|
color: string, |
|
unit: string, |
|
dataCollections?: any[], |
|
} |
|
|
|
function createGraphDataset(dataArray) { |
|
const dataValues = {}; |
|
dataArray.forEach(item => { |
|
const dateObject = new Date(item.time); |
|
const dateString = `${dateObject.getFullYear() }-${ dateObject.getMonth() }-${ dateObject.getDay() } ${ dateObject.getHours() }:${ dateObject.getMinutes()}`; |
|
dataValues[dateString] = item.value; |
|
}) |
|
return dataValues; |
|
} |
|
|
|
export default function Chart({ data, title, color, unit, dataCollections }: ChartProps) { |
|
const renderData = []; |
|
|
|
if (data && data.length > 0) { |
|
renderData.push({ |
|
name: title, |
|
color, |
|
data: createGraphDataset(data) |
|
}); |
|
} |
|
|
|
dataCollections.forEach(collection => { |
|
renderData.push( |
|
{name: collection.name, data: createGraphDataset(collection.data), color: collection.color} |
|
) |
|
}); |
|
|
|
return ( |
|
<LineChart |
|
className={styles.lineChartContainer} |
|
xtitle="Time" |
|
ytitle={title} |
|
suffix={unit} |
|
legend="bottom" |
|
color={color} |
|
data={renderData} |
|
download={title} |
|
/> |
|
) |
|
} |
|
|
|
Chart.defaultProps = { |
|
dataCollections: [], |
|
data: [], |
|
title: '', |
|
};
|
|
|