Ich versuche nur, die Etiketten von mobostocks Kreisdiagramm Beispiel anzufügen, aber aus irgendeinem Grund kann ich es nicht zum Funktionieren bringen.Aktualisieren Kreisdiagramm mit Etiketten (d3)
Ich versuche im Wesentlichen nur zwei seiner Beispiele zu kombinieren (pie update, pie labels), also versuche ich diesen Code zu implementieren;
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.apples); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.data.apples; });
});
in diese Variable in seinem Kreisdiagramm Update Beispiel;
var path = svg.datum(data).selectAll("path")
.data(pie)
.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc)
.each(function(d) { this._current = d; }); // store the initial angles
Hier ist der Plnk meines Versuches, hoffentlich kann jemand das Problem klären, das ich habe.
http://plnkr.co/edit/e05kKjB8KWGqRteh8OdS?p=preview
-Code für meinen Versuch;
var width = 960,
height = 500,
radius = Math.min(width, height)/2;
var color = d3.scale.category20();
var pie = d3.layout.pie()
.value(function(d) { return d.apples; })
.sort(null);
var arc = d3.svg.arc()
.innerRadius(radius - 100)
.outerRadius(radius - 20);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
d3.tsv("data.tsv", type, function(error, data) {
if (error) throw error;
/* var path = svg.datum(data).selectAll("path")
.data(pie)
.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc)
.each(function(d) { this._current = d; }); // store the initial angles*/
var g = svg.datum(data).selectAll(".arc")
.data(pie)
.enter().append("g")
.attr("class", "arc")
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.apples); })
.each(function(d) { this._current = d; }); // store the initial angles
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.data.apples; });
d3.selectAll("input")
.on("change", change);
var timeout = setTimeout(function() {
d3.select("input[value=\"oranges\"]").property("checked", true).each(change);
});
function change() {
var value = this.value;
clearTimeout(timeout);
pie.value(function(d) { return d[value]; }); // change the value function
path = g.data(pie); // compute the new angles
path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
}
});
function type(d) {
d.apples = +d.apples;
d.oranges = +d.oranges;
return d;
}
// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
Perfect, vielen Dank! – alexc101