Ich habe viele Wege versucht, aber ich konnte nicht Tortendiagramme in d3 ziehen, wo ich ein Daten wie habe:D3 - Tortendiagramme
"something": [{
"a":"some value",
"b":"another value",
.
.
.
},{
"c":"a value",
"d":"value",
.
.
.
}]
Diese Daten sind fast 55.000 Zeilen und jede Gruppe hat 19 verschiedenen Wert. Jede Gruppe beginnt mit "{" und endet mit "}". Der Code, den ich möchte, sollte Kreisdiagramm dynamisch vom Lesen der Daten zeichnen.
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
*/
2. versuchen
//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;
});
Jede Idee?
Bitte teilen Sie Ihr Code:
die diese beiden Ideen zusammen mit einigen Arbeits Code Lassen Sie setzen . Sie sollten lesen [Wie man ein minimales, komplettes und überprüfbares Beispiel erstellt] (http://stackoverflow.com/help/mcve) –
@MehdiElFadil Ich habe meinen ganzen Code über diesen Teil zerstört. Ich wollte nicht etwas teilen, das nicht funktioniert –
@ D.Ister tatsächlich ist es innerhalb des Codes, der nicht funktioniert, dass die besten Hinweise, wie man es beheben kann gefunden werden –