d3 js - clustering bubbles to segments

后端 未结 2 1554
庸人自扰
庸人自扰 2021-01-17 03:18

**** 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

2条回答
  •  野的像风
    2021-01-17 04:01

    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) { return color(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) { return color(d.data.age); });
    
    function type(d) {
      d.population = +d.population;
      return d;
    }
    .arc text {
      font: 10px sans-serif;
      text-anchor: middle;
    }
    
    .arc path {
      stroke: #fff;
    }

提交回复
热议问题