简体   繁体   English

可以在固定圆圈尺寸的d3.js中使用圆形包装布局吗?

[英]Possible to use a circle pack layout in d3.js with fixed circle sizes?

This circle pack layout example ( http://bl.ocks.org/4063269 ) is perfect for a project I'm working on, however it sizes all the circles relative to one another: 这个圆包布局示例( http://bl.ocks.org/4063269 )非常适合我正在处理的项目,但它会相对于彼此调整所有圆圈的大小:

在此输入图像描述

Is there a simple way to specify fixed radii for each circle? 有没有一种简单的方法来为每个圆指定固定半径?

I've scoured the source code, examples, google, and stackoverflow and can't seem to find anything helpful. 我已经搜索了源代码,示例,谷歌和stackoverflow,似乎找不到任何有用的东西。

The exact sizing of circles is important to me. 圆圈的确切尺寸对我来说很重要。

It is possible, and simple thing to do. 这是可能的,而且很简单。 The first answer is accurate, but I believe mine is simpler, more explicit, so I am attaching it too. 第一个答案是准确的,但我相信我的更简单,更明确,所以我也附上它。

Please take a look at this example: jsfiddle 请看一下这个例子: jsfiddle

When you press "Constant" button, you will see something like this: 当您按“常量”按钮时,您将看到如下内容:

在此输入图像描述

The key code line is this: 关键代码行是这样的:

    pack.value(function(d) { return 100; })

This will make circle radiuses constant regardles of data. 这将使圆半径成为数据的恒定关系。 100 can be any constant of course. 100当然可以是任何常数。 You can apply this line in circle pack initialization (most likely this will be your case), or reinitialization (like in my example). 您可以在circle pack初始化中应用此行(很可能这将是您的情况),或重新初始化(如我的示例中所示)。

Hope this helps. 希望这可以帮助。

If you follow the code in the example you gave, the size of the <circle> elements is being decided here: 如果您按照您给出的示例中的代码,那么<circle>元素的大小将在此处决定:

node.append("circle")
  .attr("r", function(d) { return d.r; })
// ...

To fix the size of circles to, say, 50 , you can do this: 要将圆圈的大小固定为50 ,您可以执行以下操作:

node.append("circle")
  .attr("r", function(d) { return 50; })
// ...

Update 更新

This will, however, break the layout as pointed out in the comment. 但是,这将打破评论中指出的布局。 To fix that, one can provide the same value to each node: 要解决这个问题,可以为每个节点提供相同的value

// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
  var classes = [];

  function recurse(name, node) {
    if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
    else classes.push({packageName: name, className: node.name, value: node.size});
  }

  recurse(null, root);
  return {children: classes};
}

to: 至:

// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
  var classes = [];

  function recurse(name, node) {
    if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
    else classes.push({packageName: name, className: node.name, value: 1});
  }

  recurse(null, root);
  return {children: classes};
}

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

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