我找到了这个小提琴
http://jsfiddle.net/Qh9X5/1196/
它基本上是一个圆环图,我想要实现的是圆环图中的饼图:
http://jsfiddle.net/rfcee/q3z88wfx/1/
但它以某种方式无法正确呈现。这是可能吗? 谢谢!
答案 0 :(得分: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)
...
更新了fiddle。
答案 1 :(得分: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>
&#13;
我认为你在寻找这个......