[英]D3.js how to create this zoom behavior?
我希望我的图表的 xAxis 在使用鼠标滚轮缩放时表现得像这样。 预期行为:
我目前的变焦
const zoomExtent = [ [ 0, 0 ], [ innerWidth, innerHeight ] ]
const xScale = scaleUtc().domain(minMaxData).range([0, innerWidth])
const handleZoom = ({ transform }) => {
transform.rescaleX(xScale)
}
const zoomBehavior = zoom().scaleExtent([ 0.4, 4 ]).extent(zoomExtent).translateExtent(zoomExtent).on('zoom', handleZoom)
我的问题:
这不是一个优雅的解决方案,但它确实有效。
// check if wheeled
const isWheeled = sourceEvent.type === 'wheel'
// i need it to be infinite because i lazy load the older data.
// there is no middle infinity.
const zoomExtent = [ [ Infinity, 0 ], [ innerWidth, innerHeight ] ]
// fix focal point at the innerwidth. using previous transform
const tx = innerWidth - transform.invertX(innerWidth) * k
const myZoom = zoomIdentity.translate(isWheeled ? tx : x, 0).scale(k)
// prevent jumping when dragging
sourceEvent.target.__zoom = myZoom
// update scale
const handleZoom = ({ transform }) => {
myZoom.rescaleX(xScale)
}
如果您有更好的解决方案,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.