Browse Source

component refactorinmg begin

pull/33/head
glitch4347 12 months ago
parent
commit
326c666435
  1. 77
      react/ChartwerkLinePod.tsx

77
react/ChartwerkLinePod.tsx

@ -1,11 +1,10 @@
import type { LineTimeSerie, LineOptions } from '@chartwerk/line-pod'; import { LineTimeSerie, LineOptions, LinePod } from '@chartwerk/line-pod';
import { LinePod } from '@chartwerk/line-pod';
import { useEffect, useRef, useState, PropsWithChildren, forwardRef } from 'react'; import { AxisRange } from '@chartwerk/core/dist/types';
import { useEffect, useRef, useState } from 'react';
import * as _ from 'lodash';
export type AxisRange = [number, number] | undefined;
export type ChartwerkLinePodProps = { export type ChartwerkLinePodProps = {
id: string; id: string;
series: LineTimeSerie[]; series: LineTimeSerie[];
@ -25,7 +24,8 @@ export type ChartwerkLinePodProps = {
onRenderEnd?: () => void, onRenderEnd?: () => void,
} }
export const ChartwerkLinePod = forwardRef<HTMLDivElement, PropsWithChildren<ChartwerkLinePodProps>>((props, ref) => { function ChartwerkLinePod(props: ChartwerkLinePodProps) {
const [pod, setPod] = useState<LinePod | null>(null); const [pod, setPod] = useState<LinePod | null>(null);
const [hack, setHack] = useState<number | null>(null); const [hack, setHack] = useState<number | null>(null);
@ -35,55 +35,20 @@ export const ChartwerkLinePod = forwardRef<HTMLDivElement, PropsWithChildren<Cha
useEffect(() => { useEffect(() => {
// this function will be called on component unmount // this function will be called on component unmount
return () => { return () => {
if (pod === null) { if(pod === null) {
return; return;
} }
console.log('remove chart');
// @ts-ignore // @ts-ignore
pod.removeEventListeners(); pod.removeEventListeners();
} }
}, []); }, []);
useEffect(() => { useEffect(() => {
if (chart === null) { if(chart === null) {
return; return;
} }
let eventsCallbacks = _.cloneDeep(props.options.eventsCallbacks || {}); if(pod === null) {
if (props.onZoomIn) {
eventsCallbacks.zoomIn = props.onZoomIn;
}
if (props.onZoomOut) {
eventsCallbacks.zoomOut = props.onZoomOut;
}
if (props.onMouseMove) {
eventsCallbacks.mouseMove = props.onMouseMove;
}
if (props.onMouseOut) {
eventsCallbacks.mouseOut = props.onMouseOut;
}
if (props.onLegendClick) {
eventsCallbacks.onLegendClick = props.onLegendClick;
}
if (props.onPanning) {
eventsCallbacks.panning = props.onPanning;
}
if (props.onPanningEnd) {
eventsCallbacks.panningEnd = props.onPanningEnd;
}
if (props.onContextMenu) {
eventsCallbacks.contextMenu = props.onContextMenu;
}
if (props.onSharedCrosshairMove) {
eventsCallbacks.sharedCrosshairMove = props.onSharedCrosshairMove;
}
if (props.onRenderStart) {
eventsCallbacks.renderStart = props.onRenderStart;
}
if (pod === null) {
console.log('create chart'); console.log('create chart');
const newPod = new LinePod( const newPod = new LinePod(
@ -92,30 +57,34 @@ export const ChartwerkLinePod = forwardRef<HTMLDivElement, PropsWithChildren<Cha
props.series, props.series,
{ {
...props.options, ...props.options,
eventsCallbacks,
} }
); );
setPod(newPod); setPod(
newPod
);
console.log('initial chart render');
newPod.render(); newPod.render();
} else { } else {
console.log('update chart');
pod.updateData(props.series, { pod.updateData(props.series, {
...props.options, ...props.options,
eventsCallbacks,
}); });
} }
}, [chart, pod, props.id, props.series, props.options]); }, [chart, pod, props.id, props.options]);
// TODO: it's a hack to render the LinePod right after the div appears in DOM // TODO: it's a hack to render the LinePod right after the div appears in DOM
setTimeout(() => { setTimeout(() => {
if (hack === null) { if(hack === null) {
setHack(1); setHack(1);
} }
}, 1); }, 1);
return ( return (
<div ref={ref}> <div id={props.id} className={props.className} ref={chartRef}></div>
<div id={props.id} className={props.className} ref={chartRef}></div>
{props.children}
</div>
); );
}); }
export default ChartwerkLinePod;

Loading…
Cancel
Save