[英]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.