Ich fühle mich wie ich bin hier ineffizient. Ich versuche eine Legende aus einem Array zu erstellen und es scheint, als würde ich zu viel schreiben, um dies zu tun. Kann mir jemand sagen, ob es optimiert werden kann?Erstellen einer Legende mit d3.js
http://jsbin.com/foqesivice/edit?js,console,output
var data = [
{name: "AnotherLong"},
{name: "BigData"},
{name: "What"},
{name: "Something"},
{name: "Smalls"}
];
var margin = {top: 10, right: 10, bottom: 10, left: 10};
var color = d3.scale.category20c();
var svg = d3.select("body").append("svg")
.attr("width", 400 - margin.left - margin.right)
.attr("height", 1000 - margin.top - margin.bottom)
var g = svg.selectAll(".row")
.data(data)
.enter().append("svg:g")
var rectangles = g.selectAll(".cell")
.data(data)
.enter().append("svg:rect")
.attr("width", 19)
.attr("height", 19)
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; })
.style("fill", function(d, i){return color(i);})
var text = g.selectAll(".text")
.data(data)
.enter().append("svg:text")
.attr("x", 24)
.attr("y", 9)
.attr("dy", ".35em")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; })
.text(function(d){return d.name;})
EDIT:
andere Sache, die mich stört. Es scheint, dass mein Styling aus irgendeinem Grund sehr fett ist. Ich habe keine Stile definiert. Ist das Standardverhalten?
Ja, deine Antwort scheint mir besser zu sein und sieht mehr wie Dinge aus, an denen ich mit d3 gearbeitet habe. Vielen Dank! – konrad