You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

D3 v7编程式平移时MouseEvent变换属性未生效问题咨询

D3FC图表鼠标平移时transform.x值异常问题

场景概述

我有两种图表平移方式:鼠标拖拽或按钮点击(固定步长平移)。

Canvas定义

this.canvas = d3.select('#chart')
    .datum(displayData)
    .call(this.chart);

图表实例定义

this.chart = chartCartesian(this.xScale, d3.scaleLinear())
    .webglPlotArea(this.barSeries)
    .xDomain(this.xScale.domain())
    .decorate(context =>
        context
            .call(this.zoom)
            .call(pointer)
    );

按钮点击触发的编程式平移代码

this.canvas.transition().call(
   this.zoom.transform,
   this.transform(direction)
);

transform函数实现

d3.zoomIdentity
  .translate(position, 0)
  .scale(/* current transform.k set in chart's zoomFn */);

Zoom行为定义(执行正常)

this.zoom = d3
   .zoom()
   .scaleExtent([1, this.timeFrameInS])
   .translateExtent([[0, 0], [this.webglService.canvasWidth$.value, this.webglService.canvasHeight$.value]])
   .extent([[0, 0], [this.webglService.canvasWidth$.value, this.webglService.canvasHeight$.value]])
   .on('zoom', event => {
      this.xPos = Math.trunc(event.transform.x);
      this.zoomEvent = event;
   });

当前问题

使用鼠标平移时,Zoom的'zoom'回调中event.transform.x的值来自之前的鼠标事件,而非最新的编程式平移事件。目前使用d3fc.chartCartesian,曾尝试使用zoom.translateTo方法,但该方法会直接重置当前的缩放和平移状态,不符合需求。

内容的提问来源于stack exchange,提问作者Tsanko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 02:22:15