简体   繁体   English

d3js可重用图表组件和过渡

[英]d3js reusable chart components and transitions

I am trying to create graphs using the reusable component approach Mike Bostock describes in this article . 我正在尝试使用Mike Bostock在本文中描述的可重用组件方法来创建图形。 I am however experiencing that calling .transition() on a selection outside of the component doesn't cause a transition on the attributes I expected. 但是,我遇到了在组件外部的选择上调用.transition()不会导致我期望的属性发生转变的情况。

For example I have made this plunker which demonstrates my question. 例如,我制作了这个小矮人 ,这说明了我的问题。 In this example I use Mike's timeSeriesChart function and then simply changes the width or the height. 在此示例中,我使用Mike的timeSeriesChart函数 ,然后仅更改宽度或高度。 By using the transition operator on a selection of the chart I then call the timeSeriesChart function to see the width transform to its new value. 通过在所选图表上使用过渡运算符,然后调用timeSeriesChart函数以查看宽度转换为其新值的过程。 However, the width/height changes instantly and only the axes smoothly transition to their new lengths. 但是,宽度/高度会立即变化,只有轴才能平滑过渡到新的长度。

If I add .transition().duration(x) when updating the width's inside the component the widths are updated smoothly, but I don't want to clutter the component with transition operators, and I think it should work to do it outside as well. 如果在更新组件内部的宽度时添加.transition().duration(x) ,则宽度可以平滑更新,但我不想用过渡运算符使组件混乱,我认为它应该可以在外部进行操作好。 It certainly seems to work for the axes. 它似乎确实适用于轴。

What am I missing? 我想念什么?

To see the code go to the plunker 要查看代码,请转到插棒

If you look at the source of the axis component , you'll see that all element selectors are wrapped in d3.transition() . 如果查看轴组件源代码 ,则会看到所有元素选择器都包装在d3.transition() This is what makes the transition happen transparently. 这就是使过渡透明发生的原因。

I've applied this to your plunker here . 我已经在这里将它应用于您的pl废者。 All you need to do is to replace 您需要做的就是更换

g.select(".area")

with

d3.transition(g.select(".area"))

and similarly for the line. 和类似的线。

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

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