javascript - Equal Space between the legends in d3 -
i newbie in using d3 library, trying have equal space between legends. in current stage of work -attached- not provide equal spaces.
i know how able fix.
here code have far:
var margin = { top: 20, right: 50, bottom: 30, left: 60 }; self.drawlegends = function () { var legenddata = [{ "color": "blue", text: "normal distribution" }, { color: "green", text: " ave a" }, { color: "red", text: "ave b" }] var legends = self.svg.selectall("g legends").data(legenddata); var legendbox = legends.enter() .append("g") .attr("transform", function (d, i) { return "translate(" + parsefloat((i + 1) * (($("#chart").width() - margin.left - margin.right) / 4)) + ",-10)" }) var circles = legendbox.append("circle") .attr("r", 5) .attr("stroke", "black") .attr("fill", function (d, i) { return d.color }) legendbox.append("text") .attr("dx", function (d) { return 10 }) .attr("dy", function (d) { return 5 }) .attr("fill","white") .text(function (d) { return d.text }) },
here's updated fiddle think you're after: http://jsfiddle.net/henbox/7le4tc92/2/
when first draw circle
, text
in each g
element, don't use transform. then, select each g
, text length (using getcomputedtextlength()
) calculate translation want:
svg.selectall("g") .attr("transform", function (d, i) { var x_pos = d3.select(this).select("text").node().getcomputedtextlength() + 20; x_offset = x_offset + x_pos; return "translate(" + (x_offset - x_pos + margin.left) + ", 20)" })
Comments
Post a Comment