переделал графики с js на ts

This commit is contained in:
Фролов 2021-04-13 18:02:25 +05:00
parent 09e5ec5179
commit f0ea4423ad

View File

@ -0,0 +1,152 @@
import { useEffect, useRef, useState} from 'react';
import {
Chart,
TimeScale,
LinearScale,
Legend,
LineController,
PointElement,
LineElement,
ChartData,
ChartTypeRegistry,
ChartOptions} from 'chart.js'
import 'chartjs-adapter-date-fns';
Chart.register( TimeScale, LinearScale, LineController, LineElement, PointElement, Legend );
const defailtOptions = {
maintainAspectRatio: false,
aspectRatio:0.4,
animation: false,
scales: {
y:{
type: 'time',
reverse:true,
time: {
unit: 'second',
stepSize: 20,
displayFormats: {
millisecond: 'HH:mm:ss.SSS',
second: 'HH:mm:ss',
minute: 'HH:mm:ss',
hour: 'dd HH:mm:ss',
day: 'MM.dd HH:mm',
week: 'yy.MM.dd HH:mm',
month: 'yyyy.MM.dd',
quarter: 'yyyy.MM.dd',
year: 'yyyy.MM',
},
},
position:{ x: 20 },
grid:{
drawTicks: false,
},
ticks: {
z: 1,
textStrokeColor : "#ffff",
textStrokeWidth : 1,
color:"#000"
}
},
x:{
type:'linear',
position:'top'
}
},
elements:{
point:{
radius:0,
hoverRadius:5,
},
},
plugins:{
legend:{
display: false,
}
}
}
export type ChartTimeData = ChartData<keyof ChartTypeRegistry, {
x: number;
y: Date;
}[], unknown>
export type ChartTimeDataParams = {
data: ChartTimeData,
yStart?: Date,
yInterval?: number,
displayLabels?: Boolean,
}
export type ChartTimeBaseProps = {
dataParams: ChartTimeDataParams,
options?: ChartOptions<keyof ChartTypeRegistry>,
}
// const mergeDataSets = (target: {datasets:any[]}, source: {datasets:any[]})=>{
// source.datasets.forEach(sds=>{
// let tds = target.datasets.find(o=>o.label === sds.label)
// if(!tds)
// target.datasets.push(sds)
// else
// tds.data = sds.data
// })
// target.datasets.forEach(tds=>{
// let sdsIdx = target.datasets.findIndex(o=>o.label === tds.label)
// if(sdsIdx === -1)
// target.datasets.splice(sdsIdx, 1)
// })
// }
export const ChartTimeBase: React.FC<ChartTimeBaseProps> = (props) => {
const chartRef = useRef<HTMLCanvasElement>(null)
const [chart, setChart] = useState<any>()
// const [chart, setChart] = useState<Chart<keyof ChartTypeRegistry, {
// x: number;
// y: Date;
// }[], unknown>>()
useEffect(()=>{
if(chartRef.current && (!chart)){
let thisOptions = {}
Object.assign(thisOptions, defailtOptions, props.options)
let newChart = new Chart(chartRef.current, {
type: 'line',
options: thisOptions,
data: props.dataParams.data
})
setChart(newChart)
return () => chart?.destroy()
}
}, [chart, props.options, props.dataParams])
useEffect(()=>{
if(!chart)
return
//mergeDataSets(chart.data, props.dataParams.data)
chart.data = props.dataParams.data
if(props.dataParams.yStart){
let interval = props.dataParams.yInterval ?? 600
let start = new Date(props.dataParams.yStart)
let end = new Date(props.dataParams.yStart)
end.setSeconds(end.getSeconds() + interval)
if(chart.options.scales?.y){
chart.options.scales.y.max = end.getTime()
chart.options.scales.y.min = start.getTime()
}
}
chart.update()
}, [chart, props])
return(<canvas ref={chartRef} />)
}