簡體   English   中英

D3.js 如何創建這種縮放行為?

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

這是我的 d3 默認行為圖表使用 d3 默認行為

我的問題:

  1. 如何始終將縮放集中在右邊緣(圖表上的最新數據)而不是鼠標當前所在的位置?
  2. 如何防止 x 軸向右擴展導致我的最新數據(在右邊緣)離開視口?

這不是一個優雅的解決方案,但它確實有效。

// 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM