|
|
|
@ -1,15 +1,20 @@
|
|
|
|
|
import type { LineTimeSerie, LineOptions } from '@chartwerk/line-pod'; |
|
|
|
|
import { LinePod } from '@chartwerk/line-pod'; |
|
|
|
|
import { LineTimeSerie, LineOptions, LinePod } from '@chartwerk/line-pod'; |
|
|
|
|
|
|
|
|
|
import { useEffect, useRef, useState, PropsWithChildren, forwardRef } from 'react'; |
|
|
|
|
import { AxisRange } from '@chartwerk/core/dist/types'; |
|
|
|
|
import { MarkerSerie } from '@chartwerk/line-pod/dist/models/marker'; |
|
|
|
|
import { SegmentSerie } from '@chartwerk/line-pod/dist/models/segment'; |
|
|
|
|
|
|
|
|
|
import { useEffect, useRef, useState } from 'react'; |
|
|
|
|
|
|
|
|
|
import _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
export type AxisRange = [number, number] | undefined; |
|
|
|
|
export type ChartwerkLinePodProps = { |
|
|
|
|
id: string; |
|
|
|
|
id?: string; |
|
|
|
|
series: LineTimeSerie[]; |
|
|
|
|
options: LineOptions; |
|
|
|
|
options?: LineOptions; |
|
|
|
|
markers?: MarkerSerie[], |
|
|
|
|
segments?: SegmentSerie[], |
|
|
|
|
className?: string; |
|
|
|
|
// TODO: callback types should be exported from chartwerk
|
|
|
|
|
onZoomIn?: (ranges: AxisRange[]) => void; |
|
|
|
@ -25,7 +30,8 @@ export type ChartwerkLinePodProps = {
|
|
|
|
|
onRenderEnd?: () => void, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const ChartwerkLinePod = forwardRef<HTMLDivElement, PropsWithChildren<ChartwerkLinePodProps>>((props, ref) => { |
|
|
|
|
export function ChartwerkLinePod(props: ChartwerkLinePodProps) { |
|
|
|
|
|
|
|
|
|
const [pod, setPod] = useState<LinePod | null>(null); |
|
|
|
|
const [hack, setHack] = useState<number | null>(null); |
|
|
|
|
|
|
|
|
@ -35,23 +41,16 @@ export const ChartwerkLinePod = forwardRef<HTMLDivElement, PropsWithChildren<Cha
|
|
|
|
|
useEffect(() => { |
|
|
|
|
// this function will be called on component unmount
|
|
|
|
|
return () => { |
|
|
|
|
if (pod === null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log('remove chart'); |
|
|
|
|
|
|
|
|
|
if(pod === null) { return; } |
|
|
|
|
// @ts-ignore
|
|
|
|
|
pod.removeEventListeners(); |
|
|
|
|
} |
|
|
|
|
}, []); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (chart === null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(chart === null) { return; } |
|
|
|
|
|
|
|
|
|
let eventsCallbacks = _.cloneDeep(props.options.eventsCallbacks || {}); |
|
|
|
|
let eventsCallbacks = _.cloneDeep(props.options?.eventsCallbacks || {}); |
|
|
|
|
if (props.onZoomIn) { |
|
|
|
|
eventsCallbacks.zoomIn = props.onZoomIn; |
|
|
|
|
} |
|
|
|
@ -83,39 +82,36 @@ export const ChartwerkLinePod = forwardRef<HTMLDivElement, PropsWithChildren<Cha
|
|
|
|
|
eventsCallbacks.renderStart = props.onRenderStart; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (pod === null) { |
|
|
|
|
console.log('create chart'); |
|
|
|
|
|
|
|
|
|
if(pod === null) { |
|
|
|
|
const newPod = new LinePod( |
|
|
|
|
// @ts-ignore
|
|
|
|
|
chart, |
|
|
|
|
props.series, |
|
|
|
|
{ |
|
|
|
|
...props.options, |
|
|
|
|
eventsCallbacks, |
|
|
|
|
} |
|
|
|
|
eventsCallbacks |
|
|
|
|
}, |
|
|
|
|
props.markers, |
|
|
|
|
props.segments |
|
|
|
|
); |
|
|
|
|
setPod(newPod); |
|
|
|
|
newPod.render(); |
|
|
|
|
} else { |
|
|
|
|
pod.updateData(props.series, { |
|
|
|
|
...props.options, |
|
|
|
|
eventsCallbacks, |
|
|
|
|
}); |
|
|
|
|
pod.updateData(props.series, props.options); |
|
|
|
|
} |
|
|
|
|
}, [chart, pod, props.id, props.series, props.options]); |
|
|
|
|
}, [chart, pod, props.id, props.options, props.markers, props.segments]); |
|
|
|
|
|
|
|
|
|
// TODO: it's a hack to render the LinePod right after the div appears in DOM
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
if (hack === null) { |
|
|
|
|
if(hack === null) { |
|
|
|
|
setHack(1); |
|
|
|
|
} |
|
|
|
|
}, 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<div ref={ref}> |
|
|
|
|
<div id={props.id} className={props.className} ref={chartRef}></div> |
|
|
|
|
{props.children} |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default ChartwerkLinePod; |
|
|
|
|