【问题标题】:Preventing d3 chords from overlapping防止 d3 和弦重叠
【发布时间】:2014-08-07 22:25:15
【问题描述】:

This demo(不是我的代码)将两个不同类别的项目相互比较,所以——与任何项目可以连接到任何其他项目的和弦图不同——咖啡店不能连接到咖啡店并且状态不能连接到状态。

如果 Dunkin' Donuts 先出现,有些挖起杆看起来像这样:

但其他楔子看起来像这样,如果星巴克在该州有更高的价值:

在我看来,不必要的重叠。元素的顺序不应该由它们的值决定,而是由左侧元素的顺序决定——也就是说,总是先 Dunkin'。

我看到一个排序发生在

 var chord = d3.layout.chord()
             .padding(.02)
             .sortSubgroups(d3.descending)

但我不确定如何为状态元素指定自定义排序。将咖啡店子组按 desc(或 asc)排序是有意义的,但各州不应得到相同的处理。

我们如何知道和弦是否是一个状态?似乎可以通过将和弦传递给chordRdr 函数的rdr 实例来提取信息,该函数将正在排序的matrix 与来自mmap 对象的元信息联系起来。

如何创建条件子组排序?

【问题讨论】:

    标签: javascript d3.js svg chord-diagram


    【解决方案1】:

    简短的回答:你不能在 d3 中做到这一点。

    这是 d3 执行排序的方式:

    // Sort subgroups…
    if (sortSubgroups) {
      subgroupIndex.forEach(function(d, i) {
        d.sort(function(a, b) {
          return sortSubgroups(matrix[i][a], matrix[i][b]);
        });
      });
    }
    

    Source

    它将对每个子组执行排序。

    我能想到的使这项工作合法的唯一方法是拥有d3.chord.layout() 的自定义副本,如果您想玩弄它,可以在上面的链接中找到它。

    【讨论】:

    • 是的,好点。 chord() 函数对矩阵图一无所知。它只能对数字矩阵进行操作。排序函数还需要mmap 变量。
    【解决方案2】:

    我不建议实际使用这个solution:

    //all the values in matrix are integers
    //we can track the numbers that are starbucks stores
    //by adding a small decimal to them
    matrix = matrix.map(function(row){
      return row.map(function(d, i){
        return d + (i == row.length - 1 ? 1e-7 : 0) })
    })
    
    //now we can do a lexicographic sort 
    //first checking for the presence of a decimal and then size
    var chord = d3.layout.chord()
        .sortSubgroups(function(a, b){
          if (a != Math.round(a)) return false
          if (b != Math.round(b)) return true
          return b < a ? -1 : b > a ? 1 : 0;
        })
    

    修改d3.layout.chord 或使用不需要冗余值矩阵的something simpler 重新开始可能在大多数情况下效果更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多