D3.JS - how do I add gridlines to my pie chart

久未见 提交于 2020-01-04 09:36:05

问题


I have extended the pie-chart example at: with pies that vary in radius depending on a percentage. I would like to add gridlines (circles) every 20 percent, but I can't figure out how.

here is the updated csv:

age,population,percent <5,2704659,67 5-13,4499890,38 14-17,2159981,91 18-24,3853788,49 25-44,14106543,71 45-64,8819342,88 =65,612463,64

and here is the updated code with pie-parts of different radius:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
  background: #333;
}

.arc path {
  stroke: #fff;
    stroke-width: 2px;
}

.arc grid {
    stroke: #fff;
    stroke-width: 1;
    stroke-dasharray: 5,5;
}

.arc text {
    fill:#fff;
    font-size:12px;
    font-weight:bold;
}

.arc line {
  stroke: #fff;
}

</style>
<body>
<script src="d3.js"></script>
<script>

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

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

var arc = d3.svg.arc()
    .outerRadius(function(d) { return 50 + (radius - 50) * d.data.percent / 100; })
    .innerRadius(20);

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

var grid = d3.svg.area.radial()
    .radius(150);

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

d3.csv("data.csv", function(error, data) {

  data.forEach(function(d) {
    d.population = +d.population;
    d.percent = d.percent;
  });

  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); });

    g.append("text")
        .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
        .attr("dy", ".35em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.data.age; });

});

</script>

回答1:


First set the number of ticks:

var numTicks = 5;   // Each tick is 20%

Then create the data to create the gridlines:

var sdat = [];
for (i=0; i<=numTicks; i++) {
    sdat[i] = (radius/numTicks) * i;
}

And then you can use a function to create the radial gridlines, and you can call it from within the d3.csv block:

addCircleAxes = function() {
    var circleAxes, i;

    svg.selectAll('.circle-ticks').remove();

    circleAxes = svg.selectAll('.circle-ticks')
      .data(sdat)
      .enter().append('svg:g')
      .attr("class", "circle-ticks");

    // radial tick lines
    circleAxes.append("svg:circle")
      .attr("r", String)
      .attr("class", "circle")
      .style("stroke", "#CCC")
      .style("opacity", 0.5)
      .style("fill", "none");

    // Labels for each circle
    circleAxes.append("svg:text")
      .attr("text-anchor", "center")
      .attr("dy", function(d) { return d - 5 })
      .style("fill", "#fff")
      .text(function(d,i) { return i * (100/numTicks) });

};

An example is here: http://bl.ocks.org/3994129

(Borrowed from: http://kreese.net/blog/2012/08/26/d3-js-creating-a-polar-area-diagram-radial-bar-chart/)



来源:https://stackoverflow.com/questions/13177974/d3-js-how-do-i-add-gridlines-to-my-pie-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!