d3 v5 轴过渡

Dun*_*loo 2 javascript d3.js typescript

我在 d3 v5 中有一个图表,它一次显示数据集的一部分,并在按下按钮时更新它。图形内容(线、多边形、圆、图像和文本)过渡得很好,但我不知道如何在 d3 v5.x 中过渡轴。

我尝试按照本教程进行操作:https : //bl.ocks.org/HarryStevens/678935d06d4601c25cb141bacd4068ce

可悲的是,它使用 d3 v4。我尝试在我的项目中使用该语法,但似乎不可能在 d3 v5 中使用调用转换。

问题是这部分:

svg.select(".x")
  .transition()
    .call(x_axis);
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

error TS2345: Argument of type 'Axis<number | { valueOf(): number; }>' is not assignable to parameter of type '(transition: Transition<BaseType, { x: Date; y: number; doubt: number; }[][], HTMLElement, any>, ...'.
Types of parameters 'context' and 'transition' are incompatible.
Run Code Online (Sandbox Code Playgroud)

它继续了一点,基本上它说axisBottom函数应该应用于选择而不是过渡。

我环顾四周,但我发现的所有内容要么使用 d3 v4,要么没有提到轴转换。那么:是否可以在 d3 v5 中转换轴,如果可以,如何转换?

Dun*_*loo 5

感谢@Mark,我意识到我从错误的角度看待问题。它不是来自 d3,而是来自 Typescript。axisBottom 变量是一个函数,Typescript 要求它在用作回调时具有上下文。绑定 this 解决了这个问题,如:

svg.select(".x")
  .transition()
    .call(x_axis.bind(this));
Run Code Online (Sandbox Code Playgroud)