Skip to content Skip to sidebar Skip to footer

D3 Js - Clustering Bubbles To Segments

**** LATEST FIDDLE --- https://jsfiddle.net/tk5xog0g/8/ -- 2nd fiddle with a custom chart -- randomly placing the bubbles closer to region zones but can not account for overlapping

Solution 1:

Hope this helps.

var width = 960,
    height = 500,
    radius = Math.min(width, height) / 2;

var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var arc = d3.svg.arc()
    .outerRadius(radius - 60)
    .innerRadius(radius - 70);

var pie = d3.layout.pie()
    .sort(null)
    .value(function(d) { return d.population; });

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var data = [
	{"age":"<5", "population":"2704659"},
{"age":"5-13", "population":"4499890"},
{"age":"14-17", "population":"2159981"},
{"age":"18-24", "population":"3853788"},
{"age":"25-44", "population":"14106543"},
{"age":"45-64", "population":"8819342"},
{"age":"≥65", "population":"612463"}
];

  var g = svg.selectAll(".arc")
      .data(pie(data))
    .enter().append("g")
      .attr("class", "arc");

  g.append("path")
      .attr("d", arc)
      .style("fill", function(d) { returncolor(d.data.age); });

  arc
    .outerRadius(radius - 10)
    .innerRadius(0);
	  
  g.append("circle")
      .attr("transform", function(d) { return"translate(" + arc.centroid(d) + ")"; })
      .attr("r", "15px")
	  .style("fill", function(d) { returncolor(d.data.age); });

functiontype(d) {
  d.population = +d.population;
  return d;
}
.arc text {
  font: 10px sans-serif;
  text-anchor: middle;
}

.arc path {
  stroke: #fff;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Solution 2:

enter image description here

Latest fiddle. http://jsfiddle.net/NYEaX/1505/

( http://jsfiddle.net/NYEaX/1506/ )- refactored

1.-- how to animate the arcs2.-- how to animate the bubbles3.-- adding back the randomise button to test with 2 dummy data sets.

this is the old pie animations and worked very well

/* ------- ANIMATE PIE SLICES -------*/var slice = doughpie.select(".slices").selectAll("path.slice")
                  .data(pie(data), key);

                slice.enter()
                  .insert("path")
                  .style("fill", function(d) {
                    return color(d.data.label);
                  })
                  .style("transform", function(d, i){
                    //return "translate(0, 0)";
                  })
                  .attr("class", "slice");

                slice
                  .transition().duration(1000)
                  .attrTween("d", function(d) {
                    this._current = this._current || d;
                    var interpolate = d3.interpolate(this._current, d);
                    this._current = interpolate(0);
                    return function(t) {
                      return arc(interpolate(t));
                    };
                  })

                slice.exit()
                  .remove();
                /* ------- ANIMATE PIE SLICES -------*/

//this is the current pie arcs - but when I try and animate the pie in the same manner - it fails.

var g = svg.selectAll(".arc")
  .data(pie(data))
  .enter().append("g")
  .attr("class", "arc");

g.append("path")
  .attr("d", arc)
  .style("fill", function(d) {
    return color(d.data.label);
  });

arc
  .outerRadius(radius - 10)
  .innerRadius(0);

Post a Comment for "D3 Js - Clustering Bubbles To Segments"