Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js - Donut charts with multiple rings

The following example shows a donut chart in D3.js, is it possible to add more than one ring to the chart?

var dataset = {
  apples: [53245, 28479, 19697, 24037, 40245],
};

var width = 460,
    height = 300,
    radius = Math.min(width, height) / 2;

var color = d3.scale.category20();

var pie = d3.layout.pie()
    .sort(null);

var arc = d3.svg.arc()
    .innerRadius(radius - 100)
    .outerRadius(radius - 50);

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

var path = svg.selectAll("path")
    .data(pie(dataset.apples))
  .enter().append("path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc);

Example: http://jsfiddle.net/gregfedorov/Qh9X5/9/

So in my data set I want something like the following:

var dataset = {
  apples: [53245, 28479, 19697, 24037, 40245],
  oranges: [53245, 28479, 19697, 24037, 40245],
  lemons: [53245, 28479, 19697, 24037, 40245],
  pears: [53245, 28479, 19697, 24037, 40245],
  pineapples: [53245, 28479, 19697, 24037, 40245],
};

What I want is to have 5 rings in total all around the same central point, is this possible and does anyone have an example?

like image 474
CLiown Avatar asked Jul 06 '13 22:07

CLiown


People also ask

How do you customize a donut chart?

Select the data that you want to plot in the doughnut chart. On the Insert tab, in the Charts group, click Other Charts. Under Doughnut, click Doughnut. Click the plot area of the doughnut chart.


2 Answers

Yes, you can do this quite easily. The key is to use nested selections. That is, you pass in your top level list of lists and create a container element for each list. Then you do the nested selection and draw the actual elements. In this particular case, you also need to adjust the radii of the arcs so that they don't overlap.

var gs = svg.selectAll("g").data(d3.values(dataset)).enter().append("g");
var path = gs.selectAll("path")
  .data(function(d) { return pie(d); })
.enter().append("path")
  .attr("fill", function(d, i) { return color(i); })
  .attr("d", function(d, i, j) {
    return arc.innerRadius(10+cwidth*j).outerRadius(cwidth*(j+1))(d);
  });

Updated jsfiddle here.

like image 98
Lars Kotthoff Avatar answered Oct 22 '22 06:10

Lars Kotthoff


Using the fiddle you posted, there is an "arc" defined in the fiddle here:

var arc = d3.svg.arc()
    .innerRadius(radius - 100)
    .outerRadius(radius - 50);

That arc is what's used to build the ring graph here:

var path = svg.selectAll("path")
    .data(pie(dataset.apples))
  .enter().append("path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc);

So if you just made a different arc for each of your data sets with a variety of radii, you would have additional rings in your chart.

like image 21
Randolpho Avatar answered Oct 22 '22 06:10

Randolpho