d3 bubble samples

This is a test for d3 bubbles for DWA

by Monica Salamanca

HTML

<script src="http://d3js.org/d3.v4.min.js"></script>

JavaScript

var width = 500,
    height = 500,
    padding = 1.5, // separation between same-color circles
    clusterPadding = 4, // separation between different-color circles
    maxRadius = 40; 
  
var n = 5, // total number of circles
    m = 1; // number of distinct clusters  
    
var color = d3.scale.category10()
    .domain(d3.range(m));

// The largest node for each cluster.
var clusters = new Array(m);

var nodes = d3.range(n).map(function() {
  var i = Math.floor(Math.random() * m),
      //r = Math.sqrt((i + 1) / m * -Math.log(Math.random())) * maxRadius,
      r = maxRadius,
      d = {cluster: i, radius: r};
  if (!clusters[i] || (r > clusters[i].radius)) clusters[i] = d;
  return d;
});

// Use the pack layout to initialize node positions.
d3.layout.pack()
    .sort(null)
    .size([width, height])
    .children(function(d) { return d.values; })
    .value(function(d) { return d.radius * d.radius; })
    .nodes({values: d3.nest()
    .key(function(d) { return d.cluster; })
    .entries(nodes)});

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

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var node = svg.selectAll("circle")
    .data(nodes)
    .enter().append("circle")
    .style("fill", function(d) { return color(d.cluster); })
    .on("click", function(d) {
      d.radius *= 1.1;
      d3.select(this).attr("r", d.radius);
    })
    .call(force.drag);

node.transition()
    .duration(750)
    .delay(function(d, i) { return i * 5; })
    .attrTween("r", function(d) {
      var i = d3.interpolate(0, d.radius);
      return function(t) { return d.radius = i(t); };
    });

function tick(e) {
  node
      .each(cluster(10 * e.alpha * e.alpha))
      .each(collide(.5))
      .attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; });
}

// Move d to be adjacent to the cluster...