简体   繁体   English

D3.js - 带有多个环的甜甜圈图表

[英]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? 以下示例显示D3.js中的圆环图,是否可以向图表添加多个环?

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/ 示例: 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? 我想要的是在同一个中心点共有5个环,这是可能的,有没有人有一个例子?

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 . 在这里更新了jsfiddle。

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. 因此,如果您为每个具有各种半径的数据集创建了不同的弧,则图表中会有额外的环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM