javascript - 了解 d3 多焦点部队布局

标签 javascript d3.js

我正在尝试了解这个漂亮示例的工作原理...

http://bl.ocks.org/mbostock/1804919

我看到聚类是通过节点的颜色完成的,但我对碰撞检测函数中有问题的行感到困惑...

r = d.radius + quad.point.radius + (d.color !== quad.point.color) * padding;

如何“添加”比较颜色“d.color”和“quad.point.color”的乘积?我本以为这只会返回真/假?无论哪种方式,我不确定我是否仅遵循这种对颜色的引用是否会产生按颜色聚类的预期效果?

无论如何,我还没有找到任何关于碰撞检测功能工作原理的逐行描述,所以我真的希望这里有人能很好地理解它来帮助向我解释这一点。

我最终想要实现的是使示例适应另一个非数字节点属性(例如 d.person_name !== quad.point.person_name)的聚类。

谢谢!

最佳答案

你问的那行是计算节点之间的允许距离,节点之间的距离(l)与r进行比较,以确定之间是否存在碰撞dquad.point。如果节点之间的颜色相同,则将值 padding 添加到允许的距离中。 bool 结果被上下文强制转换为数字类型。
不用假设 JS 做了什么,打开浏览器工具真的很容易,只需输入表达式就可以看到结果是什么......

enter image description here


但是碰撞检测不参与聚类,这是由这段代码处理的......

// Move nodes toward cluster focus.
function gravity(alpha) {
  return function(d) {
    d.y += (d.cy - d.y) * alpha;
    d.x += (d.cx - d.x) * alpha;
  };
}

如果您有一些数据并且想使用相同的代码按特定属性对它们进行分组,那么您需要将 cxcy 属性添加到您的数据使得具有相同属性值(值不需要是数字)的项目具有相同的 cxcy 值。


示例(this 的修改版本)

var width = 600,
  height = 200,
  padding = 6, // separation between nodes
  maxRadius = 6;

var n = 200, // total number of nodes
  names = ["Givens", "Crowder", "Lannister", "Baratheon", "Stark"],
  m = names.length; // number of distinct clusters

var color = d3.scale.category10()
  .domain(d3.range(m));

var x = d3.scale.ordinal()
  .domain(names)
  .rangePoints([0, width], 1),

  legend = d3.svg.axis()
  .scale(x)
  .orient("top")


var nodes = d3.range(n).map(function() {
  var i = Math.floor(Math.random() * m),
    v = (i + 1) / m * -Math.log(Math.random());
  return {
    radius: Math.sqrt(v) * maxRadius,
    color: color(i),
    cx: x(names[i]),
    cy: height / 2
  };
});

var force = d3.layout.force()
  .nodes(nodes)
  .size([width, height])
  .gravity(0)
  .charge(0)
  .on("tick", tick)
  .start();

var svg = d3.select("body").append("svg")
  .attr("width", width)
  .attr("height", height),
  gLegend = svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0, " + height * 0.9 + ")")
  .call(legend);
gLegend.selectAll(".tick text")
  .attr("fill", function(d, i) {
    return color(i);
  });


var circle = svg.selectAll("circle")
  .data(nodes)
  .enter().append("circle")
  .attr("r", function(d) {
    return d.radius;
  })
  .style("fill", function(d) {
    return d.color;
  })
  .call(force.drag);

function tick(e) {
  circle
    .each(gravity(.2 * e.alpha))
    .each(collide(.5))
    .attr("cx", function(d) {
      return d.x;
    })
    .attr("cy", function(d) {
      return d.y;
    });
}

// Move nodes toward cluster focus.
function gravity(alpha) {
  return function(d) {
    d.y += (d.cy - d.y) * alpha;
    d.x += (d.cx - d.x) * alpha;
  };
}

// Resolve collisions between nodes.
function collide(alpha) {
  var quadtree = d3.geom.quadtree(nodes);
  return function(d) {
    var r = d.radius + maxRadius + padding,
      nx1 = d.x - r,
      nx2 = d.x + r,
      ny1 = d.y - r,
      ny2 = d.y + r;
    quadtree.visit(function(quad, x1, y1, x2, y2) {
      if (quad.point && (quad.point !== d)) {
        var x = d.x - quad.point.x,
          y = d.y - quad.point.y,
          l = Math.sqrt(x * x + y * y),
          r = d.radius + quad.point.radius + (d.color !== quad.point.color) * padding;
        if (l < r) {
          l = (l - r) / l * alpha;
          d.x -= x *= l;
          d.y -= y *= l;
          quad.point.x += x;
          quad.point.y += y;
        }
      }
      return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
    });
  };
}
circle {
  stroke: #000;
}
.x.axis path {
  fill: none;
}
.x.axis text {
  font-family: Papyrus, Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, sans-serif;
}
body {
  background-color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>

关于javascript - 了解 d3 多焦点部队布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31367095/

相关文章:

javascript - 云函数 - 如何仅实例化全局函数/变量一次?

javascript - 咕噜声服务: concurrent:server aborts due to warnings

d3.js - 如何使 geoAlbersUSA 投影像 geoMercator 一样笔直(不弯曲)?

javascript - d3.selectAll 在 v4 中按类更改样式

javascript - 在 d3.js 中的拖放操作期间不会触发鼠标悬停事件

d3.js - 如何将 href 赋予 d3js 文本元素

javascript - 达到10行后如何触发DataTables中的下一个按钮

javascript - 以 Angular 6 重试 HTTP 请求

javascript - 在 Windows 上使用 MinGw 构建 mozilla NSPR,无需 VC++

javascript - 无法读取 d3.force 实现中未定义的属性 'weight'