forked from ddrilling/asb_cloud_front
74 lines
1.9 KiB
React
74 lines
1.9 KiB
React
|
import moment from 'moment';
|
||
|
import { useEffect, useState} from 'react';
|
||
|
import {ChartTimeBase} from './ChartTimeBase'
|
||
|
|
||
|
const GetRandomColor = () => "#" + Math.floor(Math.random()*16777215).toString(16)
|
||
|
|
||
|
const CreateDataset = (lineConfig) => {
|
||
|
let color = lineConfig.borderColor
|
||
|
?? lineConfig.backgroundColor
|
||
|
?? lineConfig.color
|
||
|
?? GetRandomColor()
|
||
|
|
||
|
let dataset = {
|
||
|
label: lineConfig.label,
|
||
|
data: [],
|
||
|
backgroundColor: lineConfig.backgroundColor ?? color,
|
||
|
borderColor: lineConfig.borderColor ?? color,
|
||
|
borderWidth: lineConfig.borderWidth ?? 1,
|
||
|
borderDash: lineConfig.dash ?? [],
|
||
|
}
|
||
|
return dataset
|
||
|
}
|
||
|
|
||
|
const ChartOptions = {
|
||
|
responsive: true,
|
||
|
// plugins:{
|
||
|
// legend:{
|
||
|
// maxHeight: 64,
|
||
|
// fullSize: true,
|
||
|
// display: true,
|
||
|
// posision:'chartArea',
|
||
|
// align: 'start',
|
||
|
// }
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
export const ChartTimeArchive = ({lines, data, yDisplay, rangeDate, chartRatio}) => {
|
||
|
const [dataParams, setDataParams] = useState({data:{datasets:[]}})
|
||
|
|
||
|
useEffect(() => {
|
||
|
if ((!lines)
|
||
|
|| (!data))
|
||
|
return
|
||
|
|
||
|
let newDatasets = lines.map(lineCfg => {
|
||
|
let dataset = CreateDataset(lineCfg)
|
||
|
dataset.data = data.map(dataItem => {
|
||
|
return {
|
||
|
x: dataItem[lineCfg.xAccessorName],
|
||
|
y: new Date(dataItem[lineCfg.yAccessorName??'date'])
|
||
|
}
|
||
|
})
|
||
|
return dataset
|
||
|
})
|
||
|
|
||
|
let interval = rangeDate ? (rangeDate[1] - rangeDate[0]) / 1000 : null
|
||
|
let startDate = rangeDate ? rangeDate[0] : moment()
|
||
|
let newParams = {
|
||
|
yInterval: interval,
|
||
|
yStart: startDate,
|
||
|
displayLabels: yDisplay??false,
|
||
|
data: {
|
||
|
datasets: newDatasets
|
||
|
}
|
||
|
}
|
||
|
setDataParams(newParams)
|
||
|
}, [data, lines, yDisplay, rangeDate, chartRatio])
|
||
|
|
||
|
const opt = ChartOptions
|
||
|
opt.aspectRatio = chartRatio
|
||
|
|
||
|
return (<ChartTimeBase dataParams={dataParams} options={opt}/>)
|
||
|
}
|