简体   繁体   English

D3 - 初始化后第一次转换的问题

[英]D3 - issues on first transition after initialization

I have created this jsbin http://jsbin.com/ibewux/3/edit to show a strange behavior on transitions. 我创建了这个jsbin http://jsbin.com/ibewux/3/edit来显示转换时的奇怪行为。

When the chart is initialized, it correctly displays the data (see the table below it). 初始化图表时,它会正确显示数据(请参见下表)。

If I try to change the chart type through the dropdown menu, some series are swapped; 如果我尝试通过下拉菜单更改图表类型,则会交换某些系列; after this happens, the series are not swapped anymore. 在这种情况发生后,该系列不再被交换。

Same thing happens if you click on updateChartData button; 如果单击updateChartData按钮,也会发生同样的事情; first time it will swap the series compared to the data displayed in the table. 第一次它将交换系列与表中显示的数据进行比较。

So it seems that only the first transition after initialization is subject to this unwanted swap. 因此,似乎只有初始化后的第一次转换才会受到这种不必要的交换的影响。

It's a short piece of code and wonder if you can spot the reason why this happens. 这是一段简短的代码,并想知道你是否能发现这种情况发生的原因。

Thanks 谢谢

When isVerticalChart is true, you are using an ordinal scale with domain svg.pointsNames (which seems to be an array of strings of the form "Col " + i ): isVerticalChart为true时,您使用带有域svg.pointsNames的序数标度(它似乎是"Col " + i形式的字符串数组):

x = d3.scale.ordinal().domain(svg.pointsNames);

However, you then go on to use the datum index with this scale, instead of these strings: 但是,您继续使用此比例的基准索引,而不是这些字符串:

.attr("x", function(d, i) { return isVerticalChart ? x(i) : x(d.y0 - d.size); })

I think you should be passing a string from the domain to the scale here to avoid the strange behaviour you're seeing. 我认为你应该将一个字符串从域传递到这里的比例,以避免你看到的奇怪行为。

It only works at the moment because if you pass a key to an ordinal scale that hasn't been seen before, it will add it to the domain. 它只适用于此时,因为如果您将密钥传递给之前未曾见过的序数标度,它会将其添加到域中。

There may be other issues, but hopefully that gets you closer. 可能还有其他问题,但希望能让你更接近。

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

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