简体   繁体   English

D3js排序问题适用于过渡

[英]D3js sorting issue on transition applied

I have a dataset already binded to svg:g via a d.id 我有一个数据集已经通过d.id绑定到svg:g

var categorized = g1.selectAll("g.node")
                    .data(dataset, function(d){return d.id})
                    .classed('filtered', false);
categorized.enter()
           .append("g")
           .attr("class", "node")
...

I use a function to order it from a data value like this: 我使用一个函数按照如下所示的数据值对其进行排序:

var sorted = dataset
                 .filter(function(d) { return d.notation[3].value >=50 } )
                 .sort(function(a, b) { return d3.descending(a.notation[3].value,
                                        b.notation[3].value) });

It returns the correct order when I console.log it 当我进行console.log时,它将返回正确的顺序

var filtered = g1.selectAll("g.node")
                 .data(sorted, function(d) {return d.id})
                 .classed('filtered', true);

Still in the right order if I console.log it, but if I apply a delay it reverses the result order 如果我进行console.log ,顺序仍然是正确的,但是如果我施加延迟,则会颠倒结果顺序

scored.transition()
      .delay(500).duration(1000)
      .attr("id", function(d) {
          console.log(d.id);
      });

but keeps it well sorted if I remove the delay. 但是如果我删除延迟,请保持排序良好。

My question : am I doing something in a bad way? 我的问题是:我是否在做坏事?

I think you're observing that d3.js generally uses the "optimized" for loop that iterates in reverse (see Are loops really faster in reverse? among other references). 我认为您正在观察到d3.js通常使用“优化”的for循环进行反向迭代(请参见其他参考文献中的循环是否真的反过来更快? )。

Would it work to simply reverse your selection? 简单地撤消选择是否可行? I'm not sure what you're transitioning such that you need the tween steps to be applied in a certain order. 我不确定要转换的内容,是否需要按特定顺序应用补间步骤。

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

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