2015-05-12 90 views

回答

1

这里有一个快速的尝试:

... 

data.forEach(function(d) { 
    d.total = 0; 
    d.ages = color.domain().map(function(name) { 
     d.total += +d[name]; // add a total of all population for each state 
     return {name: name, population: +d[name]}; 
    }); 
    }); 

    // determine the maximum population for all states (ie California) 
    var maxPopulation = d3.max(data, function(d){ 
    return d.total; 
    }); 

    // now for each d.age (where the the arcs come from) 
    // stash an arc function based on a scaled size 
    data.forEach(function(d) { 
    var r = radius * (d.total/maxPopulation); 
    var someArc = d3.svg.arc() // make the radius scale according to our max population 
     .outerRadius(r) 
     .innerRadius(r - (r-10)); 
    d.ages.forEach(function(a){ 
     a.arc = someArc; // stash the arc in the d.ages data so we can access later 
    }) 
    }); 

    ... 
    svg.selectAll(".arc") 
    .data(function(d) { return pie(d.ages); }) 
    .enter().append("path") 
    .attr("class", "arc") 
    .attr("d", function(d){ 
     return d.data.arc(d); // use our stashed arc function to create the path arc 
    }) 
    .style("fill", function(d) { return color(d.data.name); }) 

here

你可能不得不改变我如何缩放派的大小。我的线性方法很有效,但与CA相比,一些州的人数很少,他们的饼图可查看。