gpt4 book ai didi

javascript - 如何在 D3.js 的 Hierarchical Edge Bundling 中控制组节点距离

转载 作者:搜寻专家 更新时间:2023-11-01 04:16:15 29 4
gpt4 key购买 nike

这是 D3 中 HEB 可视化示例的链接。

http://mbostock.github.io/d3/talk/20111116/bundle.html

enter image description here

我注意到在可视化中,组之间有一个明显的距离(或所谓的空间),显然这个组之间的距离大于同一组内节点之间的距离。但是我并没有真正得到代码的哪一部分控制了这个距离。任何人都知道这里是如何控制距离的?我想在我自己的 HEB 中使用此功能。

最佳答案

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

这可以通过向集群布局提供自定义 separation() 函数来修改。要添加的关键行是:

    .separation(function(a, b) { return (a.parent == b.parent ? 1 : 5 ) })

这会将组距离增加到五个节点宽度。

通过选择其他值代替5可以得到:

enter image description here

enter image description here

enter image description here

enter image description here

这是完整的例子:

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

var cluster = d3.layout.cluster()
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 5 ) })
.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>

关于javascript - 如何在 D3.js 的 Hierarchical Edge Bundling 中控制组节点距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297022/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com