我正在尝试使用 d3 js 在单个页面中添加多个饼图。 饼图在为 2 时加载,但是当我添加第三个饼图时,图表不会附加到 SVG。 [这是我的完整代码。] 我的 fiddle :http://www.jsfiddle.net/SampathPerOxide/xt0L1scu/10/
var h = 600;
var r = h / 2;
var arc = d3.svg.arc().outerRadius(r);
var data = [{
"label": "a",
"value": 74
},
{
"label": "b",
"value": 7
},
{
"label": "b",
"value": 7
},
{
"label": "d",
"value": 12
}
];
var data1 = [{
"label": "e",
"value": 74
},
{
"label": "f",
"value": 7
},
{
"label": "g",
"value": 7
},
{
"label": "h",
"value": 12
}
];
var data2 = [{
"label": "i",
"value": 74
},
{
"label": "j",
"value": 7
},
{
"label": "k",
"value": 7
},
{
"label": "l",
"value": 12
}
];
var colors = [
'rgb(178, 55, 56)',
'rgb(213, 69, 70)',
'rgb(230, 125, 126)',
'rgb(239, 183, 182)'
]
nv.addGraph(function() {
var chart = nv.models.pieChart()
.x(function(d) {
return d.label
})
.y(function(d) {
return d.value
})
.color(colors)
.showLabels(true)
.labelType("percent");
d3.select("#chart svg")
.datum(data)
.transition().duration(1200)
.call(chart);
d3.selectAll(".nv-label text")
.attr("transform", function(d) {
d.innerRadius = -450;
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
/* Alter CSS attributes */
.style({
"font-size": "1em"
});
d3.selectAll('.nv-series').each(function(d, i) {
var group = d3.select(this),
circle = group.select('circle');
var color = circle.style('fill');
circle.remove();
var symbol = group.append('path')
.attr('d', d3.svg.symbol().type('square'))
.style('stroke', color)
.style('fill', color)
.attr('transform', 'scale(1.5) translate(-2,0)')
});
return chart;
});
nv.addGraph(function() {
var chartnew = nv.models.pieChart()
.x(function(d) {
return d.label
})
.y(function(d) {
return d.value
})
.color(colors)
.showLabels(true)
.labelType("percent");
d3.select("#chartnew svg")
.datum(data1)
.transition().duration(1200)
.call(chartnew);
d3.selectAll(".nv-label text")
/* Alter SVG attribute (not CSS attributes) */
.attr("transform", function(d) {
d.innerRadius = -450;
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.style({
"font-size": "1em"
});
d3.selectAll('.nv-series').each(function(d, i) {
var group = d3.select(this),
circle = group.select('circle');
var color = circle.style('fill');
circle.remove();
var symbol = group.append('path')
.attr('d', d3.svg.symbol().type('square'))
.style('stroke', color)
.style('fill', color)
.attr('transform', 'scale(1.5) translate(-2,0)')
});
return chartnew;
});
nv.addGraph(function() {
var chartnewagains = nv.models.pieChart()
.x(function(d) {
return d.label
})
.y(function(d) {
return d.value
})
.color(colors)
.showLabels(true)
.labelType("percent");
d3.select("#chartnewagain svg")
.datum(data2)
.transition().duration(1200)
.call(chartnewagains);
d3.selectAll(".nv-label text")
.attr("transform", function(d) {
d.innerRadius = -450;
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.style({
"font-size": "1em"
});
d3.selectAll('.nv-series').each(function(d, i) {
var group1 = d3.select(this),
circle = group.select('circle');
var color = circle.style('fill');
circle.remove();
var symbol = group.append('path')
.attr('d', d3.svg.symbol().type('square'))
.style('stroke', color)
.style('fill', color)
// ADJUST SIZE AND POSITION
.attr('transform', 'scale(1.5) translate(-2,0)')
});
return chartnewagains;
});
如何使用上述代码在同一页面上添加五个饼图?
最佳答案
我认为多纳特建议的解决方案不会起作用,因为问题不仅仅是选择。我简化了代码,将 data
、data1
、data2
推送到名为 alldata
的数组中。这使我能够为每个 alldata 循环创建一个 forEach
循环,并创建一个统一的 drawchart 函数,该函数可以绘制数据的饼图。
经过多次故障排除后,我发现代码中的问题是附加的数据图例导致了问题。看来是 circle.remove();
导致了这个问题。我编辑了下面的 fiddle 中的代码,通过使用更好的选择删除圆圈来解决问题。
这是一个完整的工作 fiddle ,其中包含改进的代码,使其更易于阅读: https://jsfiddle.net/coolakul/z1b2p7x3/
我希望这有帮助。如果您有任何需要澄清的地方,请告诉我。
关于javascript - 多个饼图未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55103016/