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