Chartwerk Line Pod
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.

76 lines
1.9 KiB

import { LineTimeSerie, LineOptions, LinePod } from '@chartwerk/line-pod';
import { MarkersConf } from '@chartwerk/line-pod/dist/models/marker';
12 months ago
import { SegmentSerie } from '@chartwerk/line-pod/dist/models/segment';
import { useEffect, useRef, useState } from 'react';
12 months ago
import _ from 'lodash';
export type ChartwerkLinePodProps = {
12 months ago
id?: string;
series: LineTimeSerie[];
12 months ago
options?: LineOptions;
markersConf?: MarkersConf,
12 months ago
segments?: SegmentSerie[],
className?: string;
}
12 months ago
export function ChartwerkLinePod(props: ChartwerkLinePodProps) {
10 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 () => {
12 months ago
if(pod === null) { return; }
// @ts-ignore
pod.removeEventListeners();
}
}, []);
useEffect(() => {
12 months ago
if(chart === null) { return; }
if(pod === null) {
const newPod = new LinePod(
// @ts-ignore
chart,
props.series,
props.options,
props.markersConf,
12 months ago
props.segments
);
12 months ago
setPod(newPod);
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
12 months ago
pod.updateData(props.series, props.options);
}
11 months ago
}, [chart, props.id, props.options, props.markersConf, props.segments]);
// TODO: it's a hack to render the LinePod right after the div appears in DOM
setTimeout(() => {
if(hack === null) {
setHack(1);
}
}, 1);
return (
<div id={props.id} className={props.className} ref={chartRef}></div>
);
}
export default ChartwerkLinePod;