简体   繁体   English

d3 v5轴过渡

[英]d3 v5 axis transition

I have a graph in d3 v5, that displays part of a data set at a time and updates it on button presses. 我在d3 v5中有一个图形,该图形一次显示一部分数据集,并在按下按钮时对其进行更新。 The graph content (lines, polygons, circles, images and text) transition just fine, but I can't figure out how to transition axes in d3 v5. 图形内容(线,多边形,圆,图像和文本)过渡很好,但是我不知道如何在d3 v5中过渡轴。

I have tried to follow this tutorial : https://bl.ocks.org/HarryStevens/678935d06d4601c25cb141bacd4068ce 我试图按照本教程进行操作: https : //bl.ocks.org/HarryStevens/678935d06d4601c25cb141bacd4068ce

Sadly, it uses d3 v4. 可悲的是,它使用的是d3 v4。 I tried to use that syntax for my project, but it seems impossible to use call on a transition in d3 v5. 我试图在项目中使用该语法,但在d3 v5中的过渡上似乎无法使用调用。

The problem is this part : 问题是这部分:

svg.select(".x")
  .transition()
    .call(x_axis);

I get this error : 我收到此错误:

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.

It goes on a bit, basically it says that the axisBottom function should be applied to a selection and not to a transition. 继续一点,基本上它说axisBottom函数应该应用于选择而不是转换。

I have looked around, but everything I find either uses d3 v4 or doesn't mention axis transitions. 我环顾四周,但是我找到的所有东西都使用d3 v4或没有提及轴过渡。 So : is it possible to transition axes in d3 v5, and if so how ? 因此:可以在d3 v5中转换轴吗?

Thanks to @Mark, I realized that I was looking at the problem from the wrong perspective. 感谢@Mark,我意识到我是从错误的角度看问题。 It didn't come from d3 but from Typescript. 它不是来自d3,而是来自Typescript。 The axisBottom variable is a function, and Typescript requires it to have a context when used as a callback. axisBottom变量是一个函数,当用作回调时,Typescript要求它具有上下文。 Binding this solves that problem, as in : 绑定此解决了该问题,如:

svg.select(".x")
  .transition()
    .call(x_axis.bind(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM