[英]d3 + create pie chart with different formats
This jsfiddle works on this format of data to create a piechart: 这个jsfiddle使用这种数据格式来创建一个饼图:
var data = [ { label: 'mylabel1', value: '1342' },
{ label: 'mylabel2', value: '1505' } ]
How do i get it to run on this format of data in this fiddle ? 我如何才能在这种小提琴中以这种数据格式运行它?
data=[{ country: 'Australia',
lat: '-25.274398',
lng: '133.775136',
values:
[ { label: 'ham', value: '1342' },
{ label: 'kpr', value: '1505' } ]
}]
This is the line I think I have to change but I just don't quite have it: 这是我认为我必须更改的行,但我不太了解:
var pie = d3.layout.pie().value(function(d){return d.value;});
I have to get it to work on the values array in the new data object. 我必须让它在新数据对象中的values数组上工作。
All my code from the 2nd jsfiddle: 我所有来自第二个jsfiddle的代码:
var w = 400;
var h = 400;
var r = h/2;
var color = d3.scale.category20c();
var data = [{"label":"Category A", "value":20},
{"label":"Category B", "value":50},
{"label":"Category C", "value":30}];
var data = [ { label: 'mylabel1', value: '1342' },
{ label: 'mylabel2', value: '1505' } ]
data=[{ country: 'Australia',
lat: '-25.274398',
lng: '133.775136',
values:
[ { label: 'ham', value: '1342' },
{ label: 'kpr', value: '1505' } ]
}]
var vis = d3.select('#chart').append("svg:svg").data([data]).attr("width", w).attr("height", h).append("svg:g").attr("transform", "translate(" + r + "," + r + ")");
var pie = d3.layout.pie().value(function(d){return d.value;});
// declare an arc generator function
var arc = d3.svg.arc().outerRadius(r);
// select paths, use arc generator to draw
var arcs = vis.selectAll("g.slice").data(pie).enter().append("svg:g").attr("class", "slice");
arcs.append("svg:path")
.attr("fill", function(d, i){
return color(i);
})
.attr("d", function (d) {
// log the result of the arc generator to show how cool it is :)
console.log(arc(d));
return arc(d);
});
// add the text
arcs.append("svg:text").attr("transform", function(d){
d.innerRadius = 0;
d.outerRadius = r;
return "translate(" + arc.centroid(d) + ")";}).attr("text-anchor", "middle").text( function(d, i) {
return data[i].label;}
);
you have to change the data that you pass for data join. 您必须更改传递给数据联接的数据。 in this case it's the
values
array of your data: 在这种情况下,它是数据的
values
数组:
var vis = d3.select('#chart').append("svg:svg").data([data[0].values])
also, to adjust the label drawing to this new data format, you need change return data[i].label;
另外,要将标签绘图调整为这种新的数据格式,您需要更改
return data[i].label;
to return d.data.label;
return d.data.label;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.