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.
75 lines
1.9 KiB
75 lines
1.9 KiB
11 months ago
|
import { LineTimeSerie, LineOptions, LinePod } from '@chartwerk/line-pod';
|
||
11 months ago
|
import { MarkersConf } from '@chartwerk/line-pod/dist/models/marker';
|
||
11 months ago
|
import { SegmentSerie } from '@chartwerk/line-pod/dist/models/segment';
|
||
11 months ago
|
|
||
8 months ago
|
import React, { useEffect, useRef, useState } from 'react';
|
||
12 months ago
|
|
||
11 months ago
|
import _ from 'lodash';
|
||
|
|
||
12 months ago
|
|
||
|
export type ChartwerkLinePodProps = {
|
||
11 months ago
|
id?: string;
|
||
12 months ago
|
series: LineTimeSerie[];
|
||
11 months ago
|
options?: LineOptions;
|
||
11 months ago
|
markersConf?: MarkersConf,
|
||
11 months ago
|
segments?: SegmentSerie[],
|
||
12 months ago
|
className?: string;
|
||
|
}
|
||
|
|
||
11 months ago
|
export function ChartwerkLinePod(props: ChartwerkLinePodProps) {
|
||
10 months ago
|
|
||
12 months ago
|
const [pod, setPod] = useState<LinePod | null>(null);
|
||
|
const [hack, setHack] = useState<number | null>(null);
|
||
|
|
||
|
const chartRef = useRef(null);
|
||
|
const chart = chartRef.current;
|
||
|
|
||
|
useEffect(() => {
|
||
|
// this function will be called on component unmount
|
||
|
return () => {
|
||
11 months ago
|
if(pod === null) { return; }
|
||
12 months ago
|
// @ts-ignore
|
||
|
pod.removeEventListeners();
|
||
|
}
|
||
|
}, []);
|
||
|
|
||
|
useEffect(() => {
|
||
11 months ago
|
if(chart === null) { return; }
|
||
11 months ago
|
if(pod === null) {
|
||
12 months ago
|
const newPod = new LinePod(
|
||
|
// @ts-ignore
|
||
|
chart,
|
||
|
props.series,
|
||
11 months ago
|
props.options,
|
||
11 months ago
|
props.markersConf,
|
||
11 months ago
|
props.segments
|
||
12 months ago
|
);
|
||
11 months ago
|
setPod(newPod);
|
||
12 months ago
|
newPod.render();
|
||
|
} else {
|
||
10 months ago
|
if(props.markersConf) {
|
||
|
pod.updateMarkers(props.markersConf);
|
||
|
}
|
||
|
if(props.segments) {
|
||
|
pod.updateSegments(props.segments);
|
||
|
}
|
||
11 months ago
|
// TODO: actually it's wrong logic with updates
|
||
|
// because it creates new pod anyway
|
||
11 months ago
|
pod.updateData(props.series, props.options);
|
||
12 months ago
|
}
|
||
11 months ago
|
}, [chart, props.id, props.options, props.markersConf, props.segments]);
|
||
12 months ago
|
|
||
|
// TODO: it's a hack to render the LinePod right after the div appears in DOM
|
||
|
setTimeout(() => {
|
||
11 months ago
|
if(hack === null) {
|
||
12 months ago
|
setHack(1);
|
||
|
}
|
||
|
}, 1);
|
||
|
|
||
|
return (
|
||
11 months ago
|
<div id={props.id} className={props.className} ref={chartRef}></div>
|
||
12 months ago
|
);
|
||
11 months ago
|
}
|
||
|
|
||
|
export default ChartwerkLinePod;
|