D3 中的非分层(组与组之间没有间隙)捆绑布局

Non-hierarchical (without gaps between groups) bundle layout in D3

我正在尝试使用 D3 进行捆绑布局。但是,我想让它成为非分层的(或伪非分层的),以便边缘节点之间没有间隙。希望下图可以很好地说明我要消除的间隙(请参阅不同颜色文本之间的图像顶部)。

我试图通过两种方式解决这个问题:通过将区分节点类型的类型替换为通用类型,以及通过将节点类型替换为作者的姓名(从而使所有节点成为不同的类型,强制相等间距)。这些尝试中的前者类似于针对 this 查询提出的建议。

然而,当我运行行

 var nodes = cluster.nodes(packageHierarchy(theArr, id_to_user_dict)); 

我不可避免地会收到错误 "Uncaught TypeError: Cannot read property 'parent' of undefined." 如何强制对这些数据使用通用根以使我的代码正常工作?

我为包含的图表生成代码结构的代码如下。我上面提出的两个解决方案在 "package hierarchy" 函数中被注释掉了。请注意,它源自 Bostock's sample for hierarchical edge bundling.

function packageHierarchy(classes, dictionary) {
for (var i = 0; i < classes.length; i++){
    var imports = [];
    // classes[i].name = "root" + "^" + classes[i]["author"];
    // classes[i].name = classes[i]["author"] + "^" + classes[i]["author"];
    classes[i].name = classes[i]["type"] + "^" + classes[i]["author"];
    for (var j = 0; j < classes[i]["values"].length; j++){
        if (classes[i]["values"][j]["type"] == "comments"){
            var responseObj = classes[i]["values"][j]["in_response_to"];
            var responseAuthor = dictionary[classes[i]["values"][j]["in_response_to"]];
            if (responseAuthor != undefined){
                imports.push(responseAuthor);
            }
        }
    }
    classes[i].imports = imports;

}


var map = {};

function find(name, data) {
    var node = map[name], i;
    if (!node) {
        node = map[name] = data || {name: name, children: [] };
        if (name.length) {
                node.parent = find(name.substring(0, i = name.indexOf("^")));
                node.parent.children.push(node);
                node.key = name.substring(i + 1);
        }
    }
    return node;
}

classes.forEach(function(d) {
    find(d.name, d);
});

return map[""];
}

您已经提到了两个解决方案,但是,我建议第三个,完全不同的想法。

组间距离由D3集群布局决定。默认行为是在组之间留空 space 个节点的大小。

我有这个涉及 D3 包布局的代码片段,组之间有默认间隔:

var diameter = 300,
    radius = diameter / 2,
    innerRadius = radius - 80;

var cluster = d3.layout.cluster()
    .size([360, innerRadius])
    .sort(null)
    .value(function(d) { return d.size; });

var bundle = d3.layout.bundle();

var line = d3.svg.line.radial()
    .interpolate("bundle")
    .tension(.85)
    .radius(function(d) { return d.y; })
    .angle(function(d) { return d.x / 180 * Math.PI; });

var svg = d3.select("body").append("svg")
    .attr("width", diameter)
    .attr("height", diameter)
    .append("g")
    .attr("transform", "translate(" + radius + "," + radius + ")");

var classes = getData();

var nodes = cluster.nodes(packageHierarchy(classes)),
    links = packageImports(nodes);

svg.selectAll(".link")
    .data(bundle(links))
   .enter()
    .append("path")
    .attr("class", "link")
    .attr("d", line);

svg.selectAll(".node")
    .data(nodes.filter(function(n) { return !n.children; }))
   .enter()
    .append("g")
    .attr("class", "node")
    .attr("transform", function(d) {
        return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")";
    })
    .append("text")
    .attr("dx", function(d) { return d.x < 180 ? 8 : -8; })
    .attr("dy", ".31em")
    .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
    .attr("transform", function(d) { return d.x < 180 ? null : "rotate(180)"; })
    .text(function(d) { return d.key; });

d3.select(self.frameElement).style("height", diameter + "px");

// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
  var map = {};

  function find(name, data) {
    var node = map[name], i;
    if (!node) {
      node = map[name] = data || {name: name, children: []};
      if (name.length) {
        node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
        node.parent.children.push(node);
        node.key = name.substring(i + 1);
      }
    }
    return node;
  }

  classes.forEach(function(d) {
    find(d.name, d);
  });

  return map[""];
}

// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
  var map = {},
      imports = [];

  // Compute a map from name to node.
  nodes.forEach(function(d) {
    map[d.name] = d;
  });

  // For each import, construct a link from the source to target node.
  nodes.forEach(function(d) {
    if (d.imports) d.imports.forEach(function(i) {
      imports.push({source: map[d.name], target: map[i]});
    });
  });

  return imports;
}

function getData() {
    return [
        {"name":"Female.Ann",     "imports":["Male.Andrew"]},
        {"name":"Female.Brenda",  "imports":["Male.Bill"]},
        {"name":"Female.Claudia", "imports":["Male.Andrew"]},
        {"name":"Female.Claudia", "imports":["Male.Bill"]},
        {"name":"Female.Deborah", "imports":["Male.Darryl"]},
        {"name":"Female.Emily",   "imports":["Male.Andrew"]},
        {"name":"Female.Flora",   "imports":["Male.Darryl"]},
        {"name":"Female.Gia",     "imports":["Male.Darryl"]},
        {"name":"Female.Hannah",  "imports":["Male.Curtis"]},
        {"name":"Female.Irene",   "imports":["Male.Hugh"]},
        {"name":"Male.Andrew",    "imports":["Female.Emily"]},
        {"name":"Male.Bill",      "imports":["Female.Emily"]},
        {"name":"Male.Curtis",    "imports":["Female.Emily"]},
        {"name":"Male.Darryl",    "imports":["Female.Claudia"]},
        {"name":"Male.Edgar",     "imports":["Female.Claudia"]},
        {"name":"Male.Franklin",  "imports":["Female.Claudia"]},
        {"name":"Male.George",    "imports":["Female.Claudia"]},
        {"name":"Male.Hugh",      "imports":["Female.Claudia"]},
    ];
};
.node {
  font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
}

.link {
  stroke: steelblue;
  stroke-opacity: .4;
  fill: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

这可以通过向集群布局提供自定义 separation() 函数来修改。

在下面的片段中,我只添加了一行:

    .separation(function(a, b) { return 1; })

差距消失了!

var diameter = 300,
    radius = diameter / 2,
    innerRadius = radius - 80;

var cluster = d3.layout.cluster()
    .separation(function(a, b) { return 1; })
    .size([360, innerRadius])
    .sort(null)
    .value(function(d) { return d.size; });

var bundle = d3.layout.bundle();

var line = d3.svg.line.radial()
    .interpolate("bundle")
    .tension(.85)
    .radius(function(d) { return d.y; })
    .angle(function(d) { return d.x / 180 * Math.PI; });

var svg = d3.select("body").append("svg")
    .attr("width", diameter)
    .attr("height", diameter)
    .append("g")
    .attr("transform", "translate(" + radius + "," + radius + ")");

var classes = getData();

var nodes = cluster.nodes(packageHierarchy(classes)),
    links = packageImports(nodes);

svg.selectAll(".link")
    .data(bundle(links))
   .enter()
    .append("path")
    .attr("class", "link")
    .attr("d", line);

svg.selectAll(".node")
    .data(nodes.filter(function(n) { return !n.children; }))
   .enter()
    .append("g")
    .attr("class", "node")
    .attr("transform", function(d) {
        return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")";
    })
    .append("text")
    .attr("dx", function(d) { return d.x < 180 ? 8 : -8; })
    .attr("dy", ".31em")
    .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
    .attr("transform", function(d) { return d.x < 180 ? null : "rotate(180)"; })
    .text(function(d) { return d.key; });

d3.select(self.frameElement).style("height", diameter + "px");

// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
  var map = {};

  function find(name, data) {
    var node = map[name], i;
    if (!node) {
      node = map[name] = data || {name: name, children: []};
      if (name.length) {
        node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
        node.parent.children.push(node);
        node.key = name.substring(i + 1);
      }
    }
    return node;
  }

  classes.forEach(function(d) {
    find(d.name, d);
  });

  return map[""];
}

// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
  var map = {},
      imports = [];

  // Compute a map from name to node.
  nodes.forEach(function(d) {
    map[d.name] = d;
  });

  // For each import, construct a link from the source to target node.
  nodes.forEach(function(d) {
    if (d.imports) d.imports.forEach(function(i) {
      imports.push({source: map[d.name], target: map[i]});
    });
  });

  return imports;
}

function getData() {
    return [
        {"name":"Female.Ann",     "imports":["Male.Andrew"]},
        {"name":"Female.Brenda",  "imports":["Male.Bill"]},
        {"name":"Female.Claudia", "imports":["Male.Andrew"]},
        {"name":"Female.Claudia", "imports":["Male.Bill"]},
        {"name":"Female.Deborah", "imports":["Male.Darryl"]},
        {"name":"Female.Emily",   "imports":["Male.Andrew"]},
        {"name":"Female.Flora",   "imports":["Male.Darryl"]},
        {"name":"Female.Gia",     "imports":["Male.Darryl"]},
        {"name":"Female.Hannah",  "imports":["Male.Curtis"]},
        {"name":"Female.Irene",   "imports":["Male.Hugh"]},
        {"name":"Male.Andrew",    "imports":["Female.Emily"]},
        {"name":"Male.Bill",      "imports":["Female.Emily"]},
        {"name":"Male.Curtis",    "imports":["Female.Emily"]},
        {"name":"Male.Darryl",    "imports":["Female.Claudia"]},
        {"name":"Male.Edgar",     "imports":["Female.Claudia"]},
        {"name":"Male.Franklin",  "imports":["Female.Claudia"]},
        {"name":"Male.George",    "imports":["Female.Claudia"]},
        {"name":"Male.Hugh",      "imports":["Female.Claudia"]},
    ];
};
.node {
  font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
}

.link {
  stroke: steelblue;
  stroke-opacity: .4;
  fill: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>