Zoom offset bug when resizing window
This block demonstrates a zoom-offset bug in a D3 v4 timeline chart with a time scale x-axis and a band scale y-axis. The SVG chart fills the window width, using `d3.scaleTime` and `d3.scaleBand` with `d3.zoom` for panning and zooming. On window resize, the x-axis range recalculates, causing the zoom transform to drift from the mouse cursor. The static data consists of two hardcoded date ranges rendered as rectangle bars.
AI-generated description(To observe bug, make sure to open the block in its own window).
A pretty barebones, mostly unstyled block, used to demonstrate the zoom bug that occurs upon resize (as described here).
This chart is a timeline-style chart, with a time (continuous) scale for the x-axis and a band scale for the y-axis. The chart always fills the width of the window. The bug behaviour is described below:
Bug description
What is happening
We have a chart with a d3.scaleTime as the x-axis, and a d3.scaleBand as the y-axis. We only zoom and pan on the x-axis. When we zoom or pan the chart, there is a 1:1 movement with the mouse cursor. When the window is resized horizontally, the x-axis range is recalculated, so that the chart will shrink or expand to fill the space - however, this results in the zoom and panning having an offset (the mouse moves a longer or shorter distance than the chart, and the zoom will focus in on a point to the left or right of the mouse cursor).
What should be happening
The zoom should stay 1:1 with the mouse (move the same distance, zoom in on the mouse position) after window resize.
How to reproduce
- Load block in its own window.
- Resize the window - the bug will be more obvious with a larger resize.
- Try to zoom on a point - you will see that the chart zooms towards a different point than the mouse is currently at. Also, if you pan the chart, you will see that the chart is moving faster or slower than the mouse cursor (try comparing with one of the axis ticks for reference).
Built with blockbuilder.org