d3饼图和甜甜圈图

3
2个回答

2
当您执行以下操作时:
var path2 = svg.selectAll("path")
    .data(piedata2)
  .enter().append("path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc2);

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

您正在将数据绑定到相同的DOM元素,第二个.data覆盖了第一个中的一些数据。

简单的解决方法是将其拆分为两个独立的g

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)

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

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

var path = g1.selectAll("path")
    .data(piedata)
    ...

var path2 = g2.selectAll("path")
    .data(piedata2)
    ...

更新了代码示例


1
明白了!非常感谢! - mcact

1

var dataset = {
  apples: [53245, 28479, 19697, 24037, 40245],
    pear:[53245, 28479]
};
var dataset2 = {
    apples:[53245, 28479, 19697, 24037, 40245]
};
var width = 300,
    height = 300,
    radius = Math.min(width, height) / 2;

var color = d3.scale.category20();

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

var piedata = pie(dataset.apples);
var piedata2 = pie(dataset2.apples);

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


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


var path2 = svg.selectAll("g").append("g").attr("id","pie")
    .data(piedata)
  .enter().append("path")
    .attr("fill", function(d, i) {
        return color(i); 
    })
    .attr("d", arc2);
        
var path = svg.selectAll("g").append("g").attr("id","donut")
    .data(piedata2)
  .enter().append("path")
    .attr("fill", function(d, i) {
        return color(i); 
    })
    .attr("d", arc);



svg.selectAll("text").data(piedata)
    .enter()
    .append("text")
    .attr("text-anchor", "middle")
    .attr("x", function(d) {
        var a = d.startAngle + (d.endAngle - d.startAngle)/2 - Math.PI/2;
        d.cx = Math.cos(a) * (radius - 75);
        return d.x = Math.cos(a) * (radius - 20);
    })
    .attr("y", function(d) {
        var a = d.startAngle + (d.endAngle - d.startAngle)/2 - Math.PI/2;
        d.cy = Math.sin(a) * (radius - 75);
        return d.y = Math.sin(a) * (radius - 20);
    })
    .text(function(d) { return d.value; })
    .each(function(d) {
        var bbox = this.getBBox();
        d.sx = d.x - bbox.width/2 - 2;
        d.ox = d.x + bbox.width/2 + 2;
        d.sy = d.oy = d.y + 5;
    });

svg.append("defs").append("marker")
    .attr("id", "circ")
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("refX", 1)
    .attr("refY", 1)
    .append("circle")
    .attr("cx", 0)
    .attr("cy", 0)
    .attr("r", 0);

svg.selectAll("path.pointer").data(piedata).enter()
    .append("path")
    .attr("class", "pointer")
    .style("fill", "none")
    .style("stroke", "black")
    .attr("marker-end", "url(#circ)")
    .attr("d", function(d) {
        if(d.cx > d.ox) {
            return "M" + d.sx + "," + d.sy + "L" + d.ox + "," + d.oy + " " + d.cx + "," + d.cy;
        } else {
            return "M" + d.ox + "," + d.oy + "L" + d.sx + "," + d.sy + " " + d.cx + "," + d.cy;
        }
    });
body {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  margin: auto;
  position: relative;
  width: 960px;
}

text {
  font: 10px sans-serif;
}

form {
  position: absolute;
  right: 10px;
  top: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

我想你正在寻找这个....

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接