D3 Circle Pack Layout 水平排列
D3 Circle Pack Layout with a horizontal arrangement
我正在尝试使用水平排列的 D3 包布局创建文字云。
我没有限制宽度,而是限制了高度。
包装布局会自动布置圆圈,其中较大的圆圈居中,其他圆圈围绕他。如果高度受限,不是水平扩展圆圈布置,而是缩小每个圆圈的大小。
如果大圆周围没有更多 space,我如何才能阻止布局调整圆的大小并开始将它们添加到边上。
我想要这样的东西:
http://imgur.com/7MDnKHF
但我只做到了这一点:
http://jsfiddle.net/v9xjra6c/
这是我当前的代码:
var width,
height,
diameter,
padding,
format,
pack,
svg,
node;
var initSizes = function() {
var dimensions = { width: 900, height: 288 };
width = dimensions.width;
height = dimensions.height;
diameter = Math.min(width, height);
padding = 12;
format = d3.format(',d');
};
var initLayout = function() {
pack = d3.layout.pack()
.sort(null)
.size([width, height])
.padding(padding);
};
var createSVG = function() {
svg = d3.select('.chart-container').append('svg')
.attr('width', width)
.attr('height', height)
.attr('class', 'bubble');
};
var createBubbles = function() {
var dataset = pack.nodes(DATA);
node = svg.selectAll('.node')
.data(dataset.filter(function(d) { return !d.children; }))
.enter().append('g')
.attr('class', 'node')
.attr('transform', function(d) { return 'translate(' + d.x + ',' + d.y + ')'; });
node.append('title')
.text(function(d) { return d.name + ': ' + format(d.value); });
node.append('circle')
.attr('r', function(d) { return d.r; });
node.append('text')
.attr('dy', '.3em')
.style('text-anchor', 'middle')
.text(function(d) { return d.name.substring(0, d.r / 3); });
};
initSizes();
initLayout();
createSVG();
createBubbles();
谢谢!
你的解决方案就像合并这个 Example1 + Example2
所以从示例 1 开始,我采用了限制圆圈在边界内的机制,这样它就不会超出 svg 的高度和宽度:
function tick(e) {
node
.each(cluster(10 * e.alpha * e.alpha))
.each(collide(.5))
//max radius is 50 restricting on the width
.attr("cx", function(d) { return d.x = Math.max(50, Math.min(width - 50, d.x)); })
//max radius is 50 restricting on the height
.attr("cy", function(d) { return d.y = Math.max(50, Math.min(height - 50, d.y)); }); }
创建半径的比例尺
//so now for your data value which ranges from 0 to 100 you will have radius range from 5 to 500
var scale = d3.scale.linear().domain([0,100]).range([5, 50]);
按照示例2制作数据
var nodes = data.map(function(d){
var i = 0,
r = scale(d.value),
d = {cluster: i, radius: r, name: d.name};
if (!clusters[i] || (r > clusters[i].radius)) {clusters[i] = d;}
return d
});
最终结果看起来像 this
注意:您可以降低代码中的高度,圆圈将根据可用的 space 重新排列。
注意:你也可以在集群周围玩耍,将类似的节点分组,如example在我的例子中,我做了一个单一的集群。
希望对您有所帮助!
我正在尝试使用水平排列的 D3 包布局创建文字云。
我没有限制宽度,而是限制了高度。
包装布局会自动布置圆圈,其中较大的圆圈居中,其他圆圈围绕他。如果高度受限,不是水平扩展圆圈布置,而是缩小每个圆圈的大小。
如果大圆周围没有更多 space,我如何才能阻止布局调整圆的大小并开始将它们添加到边上。
我想要这样的东西: http://imgur.com/7MDnKHF
但我只做到了这一点: http://jsfiddle.net/v9xjra6c/
这是我当前的代码:
var width,
height,
diameter,
padding,
format,
pack,
svg,
node;
var initSizes = function() {
var dimensions = { width: 900, height: 288 };
width = dimensions.width;
height = dimensions.height;
diameter = Math.min(width, height);
padding = 12;
format = d3.format(',d');
};
var initLayout = function() {
pack = d3.layout.pack()
.sort(null)
.size([width, height])
.padding(padding);
};
var createSVG = function() {
svg = d3.select('.chart-container').append('svg')
.attr('width', width)
.attr('height', height)
.attr('class', 'bubble');
};
var createBubbles = function() {
var dataset = pack.nodes(DATA);
node = svg.selectAll('.node')
.data(dataset.filter(function(d) { return !d.children; }))
.enter().append('g')
.attr('class', 'node')
.attr('transform', function(d) { return 'translate(' + d.x + ',' + d.y + ')'; });
node.append('title')
.text(function(d) { return d.name + ': ' + format(d.value); });
node.append('circle')
.attr('r', function(d) { return d.r; });
node.append('text')
.attr('dy', '.3em')
.style('text-anchor', 'middle')
.text(function(d) { return d.name.substring(0, d.r / 3); });
};
initSizes();
initLayout();
createSVG();
createBubbles();
谢谢!
你的解决方案就像合并这个 Example1 + Example2
所以从示例 1 开始,我采用了限制圆圈在边界内的机制,这样它就不会超出 svg 的高度和宽度:
function tick(e) {
node
.each(cluster(10 * e.alpha * e.alpha))
.each(collide(.5))
//max radius is 50 restricting on the width
.attr("cx", function(d) { return d.x = Math.max(50, Math.min(width - 50, d.x)); })
//max radius is 50 restricting on the height
.attr("cy", function(d) { return d.y = Math.max(50, Math.min(height - 50, d.y)); }); }
创建半径的比例尺
//so now for your data value which ranges from 0 to 100 you will have radius range from 5 to 500
var scale = d3.scale.linear().domain([0,100]).range([5, 50]);
按照示例2制作数据
var nodes = data.map(function(d){
var i = 0,
r = scale(d.value),
d = {cluster: i, radius: r, name: d.name};
if (!clusters[i] || (r > clusters[i].radius)) {clusters[i] = d;}
return d
});
最终结果看起来像 this
注意:您可以降低代码中的高度,圆圈将根据可用的 space 重新排列。
注意:你也可以在集群周围玩耍,将类似的节点分组,如example在我的例子中,我做了一个单一的集群。
希望对您有所帮助!