简体   繁体   English

D3线路径更新动画不流畅

[英]D3 line path update animation not smooth

I have a graph comprised up of areas, lines and circles that I am trying to update on user input, which I have already gotten to work, but when transitioning from a lower data amount to a higher amount, say from [4,2,3,4,5] to [2,4,3,1,5,8,6] , the lines animate their current paths to their new positions, but the offset just gets snapped into place, there is no animation for any new values added to the paths. 我有一个由区域,线条和圆圈组成的图表,我正在尝试更新用户输入,我已经开始工作,但是当从较低的数据量转换到更高的数量时,例如从[4,2,3,4,5][2,4,3,1,5,8,6] ,这些线将其当前路径设置为新位置的动画,但是偏移刚刚被捕捉到位,没有任何新的动画添加到路径的值。

What I would like it to do is animate from the current line path, to the new line path, with a smooth transition, so that the new values don't just snap into place, regardless of the number of data. 我想要它做的是从当前行路径动画到新行路径,并进行平滑过渡,这样无论数据的数量如何,新值都不会卡入到位。

Also, on a side note, the graphs width keeps changing on update, I'd like it to stretch to the full width of the svg no matter how much data is present, how could I achieve that? 此外,在旁注中,图表宽度在更新时不断变化,我希望无论有多少数据存在,它都会延伸到svg的整个宽度,我怎么能实现这一点?

Here is a fiddle of what I have thus far http://jsfiddle.net/znmBy/ . 这是我到目前为止的一个小提琴http://jsfiddle.net/znmBy/

I hope I was clear enough, I am new to D3 so I am a little unsure about the terminology. 我希望我很清楚,我是D3的新手,所以我对术语有点不确定。

The problem you describe is a bit more complex since in D3 you first have to add an object at a fixed position and then you can animate it from the original to another position. 您描述的问题有点复杂,因为在D3中您首先必须在固定位置添加对象,然后您可以将其从原始位置设置为另一个位置。

In your example, i would either compute the new (intermediate) initial positions based on the previous values, and then update the charts again with the real final values. 在您的示例中,我将根据先前的值计算新的(中间)初始位置,然后使用实际的最终值再次更新图表。 This will create a nice path animation from the start to the final position: 这将从开始到最终位置创建一个漂亮的路径动画:

    updateGraph([
            [...].concat(computeLatestIntermediates()),
            [...].concat(getLatestTimes())
          ]);

    updateGraph([
            [...].concat(getRealValues()),
            [...].concat(getLatestTimes())
          ]);

Alternatively you can set the initial values to 0 . 或者,您可以将初始值设置为0 This will create a nice animation from the bottom to the target value: 这将从底部到目标值创建一个漂亮的动画:

    updateGraph([
            [..., 0, 0, 0, 0],
            [...,10,12,13,14]
          ]);

    updateGraph([
            [..., 234, 245, 210, 170],
            [...,  10,  12,  13,  14]
          ]);

Btw: When animating line charts you usually have several other issues. 顺便说一句:在制作折线图时,您通常会遇到其他几个问题。 Some of them are covered on Mike's path animation page . Mike的路径动画页面中介绍了其中一些内容。 There is also a related question here . 还有一个相关的问题在这里

Edit (smarter solution): 编辑 (更智能的解决方案):

Instead of calling the update function twice, you could implement this two-step-drawing inside your drawing function like this: 您可以在绘图函数中实现这样的两步绘制,而不是两次调用更新函数:

function getPathData(d,i)        { return "M 0,0 ... L n,n"; }
function getInitalTransform(d,i) { return "translate(...)";  }
function getFinalTransform(d,i)  { return "translate(...)";  }

path
    .attr("d", getPathData)
    .attr("transform", getInitalTransform)
  .transition()
    .ease("linear")
    .attr("transform", getFinalTransform);

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

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