diff --git a/src/components/d3/D3Chart.tsx b/src/components/d3/D3Chart.tsx index fa7822d..dff01e6 100644 --- a/src/components/d3/D3Chart.tsx +++ b/src/components/d3/D3Chart.tsx @@ -124,6 +124,7 @@ const _D3Chart = >({ const [xAxisRef, setXAxisRef] = useState(null) const [yAxisRef, setYAxisRef] = useState(null) const [chartAreaRef, setChartAreaRef] = useState(null) + const [currentZoomState, setCurrentZoomState] = useState(null) const xAxisArea = useCallback(() => d3.select(xAxisRef), [xAxisRef]) const yAxisArea = useCallback(() => d3.select(yAxisRef), [yAxisRef]) @@ -159,8 +160,13 @@ const _D3Chart = >({ xAxis.domain([domain?.x?.min ?? minX, domain?.x?.max ?? maxX]) } + if (currentZoomState) { + const newXScale = currentZoomState.rescaleX(xAxis) + xAxis.domain(newXScale.domain()) + } + return xAxis - }, [xAxisConfig, data, domain, width, offset]) + }, [xAxisConfig, data, domain, width, offset, currentZoomState]) const yAxis = useMemo(() => { if (!data) return @@ -200,8 +206,13 @@ const _D3Chart = >({ yAxis.range([height - offset.top - offset.bottom, 0]) + if (currentZoomState) { + const newYScale = currentZoomState.rescaleY(yAxis) + yAxis.domain(newYScale.domain()) + } + return yAxis - }, [charts, data, domain, height, offset]) + }, [charts, data, domain, height, offset, currentZoomState]) const nTicks = { color: 'lightgray', @@ -348,6 +359,19 @@ const _D3Chart = >({ redrawCharts() }, [redrawCharts]) + useEffect(() => { + if (!svgRef) return + const zoom = d3.zoom() + .scaleExtent([1, 5]) + .translateExtent([[0, 0], [width, height]]) + .on('zoom', () => { + const zoomState = d3.zoomTransform(svgRef) + setCurrentZoomState(zoomState) + }) + + d3.select(svgRef).call(zoom) + }, [svgRef, width, height, offset]) + return ( >({ - + + + + + +