d3.layout.chord() 分析

源码:git

var τ = 2 * Math.PI;
d3.layout.chord = function() {
    var chord = {}, chords, groups, matrix, n, padding = 0, sortGroups, sortSubgroups, sortChords;
    function relayout() {
      var subgroups = {}, groupSums = [], groupIndex = d3.range(n), subgroupIndex = [], k, x, x0, i, j;
      chords = [];
      groups = [];
      k = 0, i = -1;
      while (++i < n) {
        x = 0, j = -1;
        while (++j < n) {
          x += matrix[i][j];
        }
        groupSums.push(x);
        subgroupIndex.push(d3.range(n));
        k += x;
      }
      if (sortGroups) {
        groupIndex.sort(function(a, b) {
          return sortGroups(groupSums[a], groupSums[b]);
        });
      }
      if (sortSubgroups) {
        subgroupIndex.forEach(function(d, i) {
          d.sort(function(a, b) {
            return sortSubgroups(matrix[i][a], matrix[i][b]);
          });
        });
      }
    //k运算以前为matrix的全部取值的和,运算以后为单位1数字所表示的角度值 k
= (τ - padding * n) / k; x = 0, i = -1; while (++i < n) { x0 = x, j = -1; while (++j < n) { var di = groupIndex[i], dj = subgroupIndex[di][j], v = matrix[di][dj], a0 = x, a1 = x += v * k; subgroups[di + "-" + dj] = { index: di, subindex: dj, startAngle: a0, endAngle: a1, value: v }; } groups[di] = { index: di, startAngle: x0, endAngle: x, value: (x - x0) / k }; x += padding; } i = -1; while (++i < n) { j = i - 1; while (++j < n) { var source = subgroups[i + "-" + j], target = subgroups[j + "-" + i]; if (source.value || target.value) { chords.push(source.value < target.value ? { source: target, target: source } : { source: source, target: target }); } } } if (sortChords) resort(); } function resort() { chords.sort(function(a, b) { return sortChords((a.source.value + a.target.value) / 2, (b.source.value + b.target.value) / 2); }); } chord.matrix = function(x) { if (!arguments.length) return matrix; n = (matrix = x) && matrix.length; chords = groups = null; return chord; }; chord.padding = function(x) { if (!arguments.length) return padding; padding = x; chords = groups = null; return chord; }; chord.sortGroups = function(x) { if (!arguments.length) return sortGroups; sortGroups = x; chords = groups = null; return chord; }; chord.sortSubgroups = function(x) { if (!arguments.length) return sortSubgroups; sortSubgroups = x; chords = null; return chord; }; chord.sortChords = function(x) { if (!arguments.length) return sortChords; sortChords = x; if (chords) resort(); return chord; }; chord.chords = function() { if (!chords) relayout(); return chords; }; chord.groups = function() { if (!groups) relayout(); return groups; }; return chord; };

d3.layout.chord()

构建新的弦布局。在默认状况下,输入数据并未分类,而且各群组之间没有填充。和其余布局不一样,该弦布局并非应用于数据的函数;相反,数据经过设置关联矩阵来指定,经过chords和groups访问器检索。github

chord.matrix([matrix])

指定矩阵以后,设定该布局用到的输入数据矩阵。若是没有指定矩阵,返回当前数据矩阵,默认为未定义。输入矩阵的数字必须为“方形矩阵” :[[11975,5871,8916,2868], [1951,10048,2060,6171], [8010,16145,8090,8045], [1013,990,940,6907]]缓存

矩阵的每一行对应一个特定分组,如上文所述某个发色。矩阵中每一列i同第i行相对应;每一个单元格ij对应表示第i组到第j组之间的关系。函数

chord.padding([padding])

If padding is specified, sets the angular padding between groups to the specified value inradians. If padding is not specified, returns the current padding, which defaults to zero. You may wish to compute the padding as a function of the number of groups (the number of rows or columns in the associated matrix). 指定填充以后,在不一样组之间设定角度填充,为指定的值(弧度为单位)。若是没有指定填充,返回当前填充,默认值为0。你可能但愿计算填充是分组数量(关联矩阵中行和列的数量)的函数。布局

chord.sortGroups([comparator])

若是已经指定comparator,使用指定comparator函数为布局设定分组(行)的排列顺序。为每两行调用comparator函数,传递的入参是行i和行j的总和。一般,须要将comparator按照d3.ascending或d3.descending进行指定。若是没有指定comparator,则返回当前分组排列顺序,该顺序默认值为空。spa

chord.sortSubgroups([comparator])

若是已经指定comparator,使用指定comparator函数为布局设定分组(行内各列)的排列顺序。为每对单元格调用comparator函数,值为各单元格的值。一般,须要将comparator以升序或降序进行指定。若是没有指定comparator,则回当前子分组排列顺序,该顺序默认值为空。code

chord.sortChords([comparator])

若是已经指定comparator,运用指定comparator函数为弦布局设定弦(Z顺序)的排列顺序。为每两条弦调用comparator函数,入参为源单元格和目标单元格的最小值。一般,要将comparator以升序或降序进行指定。若是没有指定comparator,返回当前chord排列顺序,默认值为空。对象

chord.chords()

给定布局的当前配置和关联矩阵,返回计算过的弦对象。若是弦对象已计算完毕,本方法返回缓存值。若是布局属性有任何改变,则清空以前计算的弦。此时,若是下次调用该方法,须要对布局进行从新计算。返回对象具备下列属性:  source -描述源对象。  target -描述目标对象。 这两个对象描述下列实体:  index -行索引,i。  subindex索引-列索引,j。  startAngle-弧的起始角,在radians内。  endAngle-弧的终止角,在radians内。  value -关联单元格ij的数值。 须要注意的是,这些对象同弦很方便为弦生成器匹配默认的访问器;但仍能够对访问器进行重写或者修改返回对象,实现布局微调。blog

chord.groups()

给定布局的当前配置和关联矩阵,返回计算过的分组对象。若是分组对象已计算完毕,本方法返回缓存值。若是布局属性有任何改变,则清空以前计算的分组。此时,若是下次调用该方法,须要对布局进行从新计算。返回对象具备下列属性:  index -行索引,i。  startAngle -弧的起始角,在radians内。  endAngle -弧的终止角,在radians内。  value -相关行 i的值的总和。 须要注意的是,这些对象同弧度生成器的默认访问器具备较好的吻合度;但仍能够对访问器进行重写或者修改返回对象,实现布局微调。索引

相关文章
相关标签/搜索