繁体   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