简体   繁体   English

D3 Circle Pack布局采用水平排列

[英]D3 Circle Pack Layout with a horizontal arrangement

I'm trying to create a wordcloud with the D3 pack layout with a horizontal arrangement. 我正在尝试用水平排列的D3包布局创建一个wordcloud。

Instead of limiting the width, I am limiting the height. 我没有限制宽度,而是限制高度。

The pack layout automatically disposes the circles with the larger one in the center and the others around him. 包装布局自动处理圆圈,中间较大的圆圈和周围的圆圈。 If the height is limited, instead of expanding the circles disposition horizontally, it reduces the size of each circle. 如果高度有限,则不是水平扩展圆形布置,而是减小每个圆的大小。

How can I stop the layout from resizing the circles and start adding them to the sides if there is no more space around the larger one. 如果较大的圆圈周围没有更多空间,我怎样才能停止布局调整圆圈的大小并开始将它们添加到圆边。

I want something like this: http://imgur.com/7MDnKHF 我想要这样的东西: http//imgur.com/7MDnKHF

But I'm only achieving this: http://jsfiddle.net/v9xjra6c/ 但我只是实现了这个目标: http//jsfiddle.net/v9xjra6c/

This is my current code: 这是我目前的代码:

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();

Thanks! 谢谢!

Your solution would be like merging this Example1 + Example2 您的解决方案就像合并此Example1 + Example2

So from Example 1 I have taken the mechanism to restrict the circles with in the bounds, such that it does not go beyond the svg height and width: 因此,从示例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)); });        }

Creating a scale for making radius 创建用于制作半径的比例

//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]);

Make the data as per Example2 根据Example2制作数据

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
});

Finally result will be looking like this 最后结果将是这样的

Note: You can reduce the height in the code and the circles will rearrange as per the space available. 注意:您可以减小代码中的高度,圆圈将根据可用空间重新排列。

Note: You can also play around the cluster to group similar nodes as in example in my case I have made a single group cluster. 注意:您也可以在群集中进行游戏以对类似节点进行分组, 例如在我的情况下,我创建了一个组群集。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM