Chartwerk Bar 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.

65 lines
1.5 KiB

10 months ago
import { BarSerie, BarOptions, ChartwerkBarPod } from '@chartwerk/bar-pod';
10 months ago
import { useEffect, useRef, useState } from 'react';
import _ from 'lodash';
export type ChartwerkBarPodProps = {
id?: string;
series: BarSerie[];
options?: BarOptions;
className?: string;
}
10 months ago
export function ChartwerkReactBarPod(props: ChartwerkBarPodProps) {
10 months ago
10 months ago
const [pod, setPod] = useState<ChartwerkBarPod | null>(null);
10 months ago
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 () => {
if(pod === null) { return; }
// @ts-ignore
pod.removeEventListeners();
}
}, []);
useEffect(() => {
if(chart === null) { return; }
if(pod === null) {
10 months ago
const newPod = new ChartwerkBarPod(
10 months ago
// @ts-ignore
chart,
props.series,
props.options,
);
setPod(newPod);
newPod.render();
} else {
// TODO: actually it's wrong logic with updates
// because it creates new pod anyway
pod.updateData(props.series, props.options);
}
10 months ago
}, [chart, props.id, props.series, props.options]);
10 months ago
// TODO: it's a hack to render the BarPod right after the div appears in DOM
setTimeout(() => {
if(hack === null) {
setHack(1);
}
10 months ago
console.log('hack');
10 months ago
}, 1);
return (
<div id={props.id} className={props.className} ref={chartRef}></div>
);
}
10 months ago
export default ChartwerkReactBarPod;