Ben birçok yol denedim ama böyle bir veriye sahip nerede d3 içinde pasta grafikler çizemezdi:D3 - pasta grafikler
"something": [{
"a":"some value",
"b":"another value",
.
.
.
},{
"c":"a value",
"d":"value",
.
.
.
}]
Bu veriler neredeyse 55.000 satır ve her gruba 19 farklı değere sahiptir. Her grup "{" ile başlar ve "}" ile biter. İstediğim kod, verileri dinamik olarak okumak için pasta grafiklerini çizmelidir. 2
var width = 550;
var height = 350;
var radius = 300/ 2;
var color = d3.scale.category20b(); //builtin range of colors
var svg = d3.select('#pie_chart').append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + (width/2) +',' + (height/2) + ')');
var total = 0;
for(var a=0;a<Data.length;a++){
total=total+parseInt(Data[a].count); // simple logic to calculate total of data count value
console.log(total);
}
var pie_data=[];
for(var a=0;a<Data.length;a++){ // simple logic to calculate percentage data for the pie
pie_data[a]=(Data[a].count/total)*100;
}
var arc = d3.svg.arc().outerRadius(radius);
// creating arc element.
var pie = d3.layout.pie()
.value(function(d,i) { return pie_data[i]; })
.sort(null);
//Given a list of values, it will create an arc data for us
//we must explicitly specify it to access the value of each element in our data array
var path = svg.selectAll('path')
.data(pie(Data))
.enter()
.append('path')
.attr('d', arc)
.attr('fill', function(d, i) {
return Data[i].color;
});
//set the color for each slice to be chosen, from the color defined in sample_data.json
//this creates the actual SVG path using the associated data (pie) with the arc drawing function
*/
//pie chart
var width = 300;
var height = 300;
//each arc in the pie chart
var outerRadius = width/2;
var innerRadius = width/3;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
//pie chart
var pie = d3.layout.pie();
//colors
var color = d3.scale.category20();
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
//Set up groups
var arcs = svg.selectAll("g.arc")
.data(pie(data))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
//Draw arc paths
arcs.append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", arc);
//Labels
arcs.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d) {
return d.value;
});
herhangi bir fikir denemek?
kodunuzu paylaşın:
Biraz çalışma kodu ile birlikte bu iki fikir koyalım: Bu seçim İçinde her yay ve etiket için
g
unsurdur . [Minimal, Tam ve Doğrulanabilir bir örnek nasıl oluşturulur?] 'U okumalısınız. (Http://stackoverflow.com/help/mcve) –@MehdiElFadil Bu bölüm hakkındaki tüm kodları yok ettim. Ben çalışmayan bir şey paylaşmak istemedim –
@ D.Ister aslında o değil düzeltmek için en iyi ipuçları işe yaramazsa kodu içinde –