简体   繁体   English

d3.layout.pack在排序递增时堆叠圆圈

[英]d3.layout.pack stacks circles when sort is ascending

I have a dataset such that when sorted in ascending order the circles end up 'stacked' but appear properly distributed when using null or descending sort order. 我有一个数据集,当按升序排序时,圆最终“堆叠”,但当使用null或降序排序时,它们看起来分布正确。

上升

降序

空值

Complete example is here: http://jsfiddle.net/SXLHx/3/ . 完整的示例在这里: http : //jsfiddle.net/SXLHx/3/

Anyone have a suggestion? 有人有建议吗?



    sortItems = function(a,b) {
        var str,result;
        switch(sortOrder%3){
            case 0:
                str = 'ascending';
                result = a.size - b.size;
                break;
            case 1:
                str = 'descending';
                result = b.size - a.size;
                break;
            default:
                str = 'null';
                result = null;
        }
        document.getElementById("sortLbl").innerHTML = str;
        return result;
    };

    pack = d3.layout.pack().sort(sortItems);

Some additional info: 一些其他信息:

I found that if I remove at least two of the blocks entries that have the value 0 (didn't matter which two but it had to be two) the layout is fine. 我发现,如果我删除至少两个值为0的块条目(不管是两个,但必须为两个),布局就可以了。 http://jsfiddle.net/SXLHx/4/ http://jsfiddle.net/SXLHx/4/

Also, applying a filter to not append circles with 0 value like so: 同样,应用过滤器以不附加0值的圆,如下所示:



    // Create circles
    node.append("circle")
        .filter(function(d){return d.size > 0;})
        .attr("r",function(d){return d.r;});

does not correct the layout issue. 无法解决布局问题。 Maybe I'm filtering incorrectly? 也许我过滤不正确?

You just have a couple of mistakes in the portion of code that is supposed to set new order and update the layout. 您应该在应该设置新顺序和更新布局的代码部分中犯几个错误。 It should look like this: (it is even simpler than what is currently in the code) 它看起来应该像这样:(它比代码中的当前代码还要简单)

pack
  .sort(sortItems)
  .nodes({blocks:data});

node
  .attr("transform",function(d){
      return "translate("+d.x+","+d.y+")";
  })
  .selectAll("circle")
  .attr("r",function(d){return d.r;});

I solved zero values with this line in pack initialization: 我在包初始化中用这一行解决了零值:

.value(function(d){return Math.max(0.01, d.size);});

Here is jsfiddle . 这是jsfiddle

Here is video of execution: 这是执行视频:

在此处输入图片说明

(note that after third button-press, circles do not return to original order, but this is due to other reasons that don't have direct connection to original problem (which is about ascending/descending order in circle pack)). (请注意,在第三次按下按钮后,圆圈不会恢复到原始顺序,但这是由于其他原因没有直接与原始问题相关联(这与圆圈包中的升序/降序有关)。

Hope this helps. 希望这可以帮助。

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

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