2021-04-13 18:03:17 +05:00
|
|
|
import { useEffect, useState} from 'react';
|
|
|
|
import {ChartTimeBase, ChartTimeData, ChartTimeDataParams} from './ChartTimeBase'
|
|
|
|
|
|
|
|
const GetRandomColor = () => "#" + Math.floor(Math.random()*16777215).toString(16)
|
|
|
|
function GetOrCreateDatasetByLineConfig (data: ChartTimeData, lineConfig: LineConfig) {
|
|
|
|
let dataset = data?.datasets.find(d=>d.label === lineConfig.label)
|
|
|
|
if(!dataset)
|
|
|
|
{
|
|
|
|
let color = lineConfig.borderColor
|
|
|
|
?? lineConfig.backgroundColor
|
|
|
|
?? lineConfig.color
|
|
|
|
?? GetRandomColor()
|
|
|
|
|
|
|
|
dataset = {
|
|
|
|
label:lineConfig.label,
|
|
|
|
data:[],
|
|
|
|
backgroundColor: lineConfig.backgroundColor ?? color,
|
|
|
|
borderColor: lineConfig.borderColor ?? color,
|
|
|
|
borderWidth: lineConfig.borderWidth ?? 1,
|
|
|
|
borderDash: lineConfig.dash ?? [],
|
2021-05-27 16:59:05 +05:00
|
|
|
showLine: lineConfig.showLine,
|
2021-04-13 18:03:17 +05:00
|
|
|
}
|
|
|
|
data.datasets.push(dataset);
|
|
|
|
}
|
|
|
|
return dataset
|
|
|
|
}
|
|
|
|
|
|
|
|
export type LineConfig = {
|
|
|
|
type?: string
|
|
|
|
label: string
|
2021-04-16 15:50:01 +05:00
|
|
|
units?: string
|
|
|
|
xAccessorName: string
|
|
|
|
yAccessorName: string
|
2021-04-13 18:03:17 +05:00
|
|
|
color?:string
|
|
|
|
borderColor?: string
|
|
|
|
backgroundColor?: string
|
|
|
|
borderWidth?: number
|
|
|
|
dash?:number[]
|
|
|
|
labels?: any[]
|
2021-05-27 16:59:05 +05:00
|
|
|
showLine: boolean
|
|
|
|
xConstValue?: number|null
|
2021-04-13 18:03:17 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
export type ChartTimeProps = {
|
2021-04-16 15:50:01 +05:00
|
|
|
label?: string,
|
2021-04-13 18:03:17 +05:00
|
|
|
yDisplay: Boolean,
|
2021-04-16 15:50:01 +05:00
|
|
|
// linePv?: LineConfig,
|
|
|
|
// lineSp?: LineConfig,
|
|
|
|
// lineIdle?: LineConfig,
|
2021-04-13 18:03:17 +05:00
|
|
|
lines: LineConfig[],
|
|
|
|
data: any[],
|
|
|
|
interval: number,
|
|
|
|
}
|
|
|
|
|
2021-04-16 15:50:01 +05:00
|
|
|
export const ChartTimeOnline: React.FC<ChartTimeProps> = (props) => {
|
2021-04-13 18:03:17 +05:00
|
|
|
const [dataParams, setDataParams] = useState<ChartTimeDataParams>({data: {datasets:[]}, yStart: new Date(), })
|
|
|
|
|
|
|
|
useEffect(()=>{
|
|
|
|
if( (!props?.lines)
|
|
|
|
|| (!props?.data)
|
|
|
|
|| (props.lines.length === 0)
|
|
|
|
|| (props.data.length === 0))
|
|
|
|
return
|
|
|
|
|
2021-04-16 15:50:01 +05:00
|
|
|
setDataParams((preDataParams) => {
|
|
|
|
props.lines.forEach(lineCfg => {
|
|
|
|
let dataset = GetOrCreateDatasetByLineConfig(preDataParams.data, lineCfg)
|
|
|
|
let points = props.data.map(dataItem => {return{
|
2021-05-27 16:59:05 +05:00
|
|
|
x: lineCfg.xConstValue ?? dataItem[lineCfg.xAccessorName],
|
|
|
|
label: dataItem[lineCfg.xAccessorName],
|
2021-04-16 15:50:01 +05:00
|
|
|
y: new Date(dataItem[lineCfg.yAccessorName])}
|
|
|
|
})
|
|
|
|
//dataset.data = [ ...dataset.data, ...points,].slice(-1024)
|
|
|
|
let data = [ ...dataset.data, ...points,]
|
|
|
|
if(points?.length > 2)
|
|
|
|
data.sort((a,b) => a.y > b.y ? 1 : -1)
|
|
|
|
if(data.length > 1024)
|
|
|
|
data.splice(0, (1024 - data.length))
|
|
|
|
dataset.data = data;
|
|
|
|
});
|
2021-05-27 16:59:05 +05:00
|
|
|
|
2021-04-16 15:50:01 +05:00
|
|
|
preDataParams.yStart = new Date()
|
|
|
|
preDataParams.yStart.setSeconds(preDataParams.yStart.getSeconds() - props.interval)
|
|
|
|
preDataParams.yInterval = props.interval
|
|
|
|
preDataParams.displayLabels = props.yDisplay
|
|
|
|
return preDataParams
|
|
|
|
})
|
2021-04-13 18:03:17 +05:00
|
|
|
|
2021-04-16 15:50:01 +05:00
|
|
|
}, [ props.data, props.lines, props.interval, props.yDisplay])
|
2021-04-13 18:03:17 +05:00
|
|
|
|
2021-05-27 16:59:05 +05:00
|
|
|
const chartPluginsOptions = {
|
|
|
|
plugins:{
|
|
|
|
legend:{
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
datalabels: {
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
borderRadius: 4,
|
|
|
|
color: '#000B',
|
|
|
|
display: function(context:any) {
|
|
|
|
return context.dataset.label === 'wellDepth'
|
|
|
|
? 'auto'
|
|
|
|
: false
|
|
|
|
},
|
|
|
|
formatter: function(value: any, context: any) {
|
|
|
|
return `${value.y.toLocaleTimeString()} ${value.label.toPrecision(4)}`
|
|
|
|
},
|
|
|
|
padding: 6,
|
|
|
|
align: 'left',
|
|
|
|
anchor: 'center',
|
|
|
|
clip: true
|
2021-06-25 09:18:11 +05:00
|
|
|
},
|
2021-07-19 17:28:09 +05:00
|
|
|
tooltip: {
|
|
|
|
enable: true
|
|
|
|
}
|
2021-05-27 16:59:05 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(<ChartTimeBase dataParams = {dataParams} options = { chartPluginsOptions } />)
|
2021-04-13 18:03:17 +05:00
|
|
|
}
|
|
|
|
|