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.
86 lines
2.3 KiB
86 lines
2.3 KiB
import { LineTimeSerie, LineOptions, LinePod } from '@chartwerk/line-pod'; |
|
|
|
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'; |
|
|
|
|
|
export type ChartwerkLinePodProps = { |
|
id?: string; |
|
series: LineTimeSerie[]; |
|
options?: LineOptions; |
|
markers?: MarkerSerie[], |
|
segments?: SegmentSerie[], |
|
className?: string; |
|
// TODO: callback types should be exported from chartwerk |
|
onZoomIn?: (ranges: AxisRange[]) => void; |
|
onZoomOut?: (centers: { x: number, y: number }) => void; |
|
onMouseMove?: (event: any) => void; |
|
onMouseOut?: () => void; |
|
onLegendClick?: (idx: number) => void, |
|
onPanning?: (event: { ranges: AxisRange[], d3Event: any }) => void; |
|
onPanningEnd?: (ranges: AxisRange[]) => void; |
|
onContextMenu?: (evt: any) => void; |
|
onSharedCrosshairMove?: (evt: any) => void; |
|
onRenderStart?: () => void, |
|
onRenderEnd?: () => void, |
|
} |
|
|
|
export function ChartwerkLinePod(props: ChartwerkLinePodProps) { |
|
|
|
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 () => { |
|
if(pod === null) { return; } |
|
// @ts-ignore |
|
pod.removeEventListeners(); |
|
} |
|
}, []); |
|
|
|
useEffect(() => { |
|
if(chart === null) { return; } |
|
if(pod === null) { |
|
console.log('create chart'); |
|
console.log("markers"); |
|
console.log(props.markers); |
|
|
|
const newPod = new LinePod( |
|
// @ts-ignore |
|
chart, |
|
props.series, |
|
props.options, |
|
props.markers, |
|
// props.segments |
|
); |
|
setPod(newPod); |
|
newPod.render(); |
|
} else { |
|
console.log('update data'); |
|
pod.updateData(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) { |
|
setHack(1); |
|
} |
|
}, 1); |
|
|
|
|
|
return ( |
|
<div id={props.id} className={props.className} ref={chartRef}></div> |
|
); |
|
} |
|
|
|
export default ChartwerkLinePod;
|
|
|