javascript - How do I append text to my rect in d3? -
This is my code, when I inspect the element but it is not showing, then adding text
Miscellaneous data = [0, 1, 2]; Var width = 100; Var Height = 100; Var canvas = D 3.select ("body"). Append ("SVG") .attr ("height", 500) .attr ("width", 500) .attr ("fill", "red") .append "g"); Var someData = canvas.selectAll ("rect"). .attr ("fill", "red") .attr ("conversion" , Function (d) {return "translation (" + height + d + ", 0)";}); Some data Append ("text") .attr ("transform", function (d) {return "translation (" + height * d + ", 0)";}) .attr ("font-size", "2em"). Attr ("color", "black") .text (function (d) {return d;});
What did I do?
Problems with your code:
-
a & lt; rectangle & gt; can not go inside -
The top level is filled with group color, hides everything in it.
The first point is that & lt; Rectangle & gt; & amp; & lt; G & gt; Update code: ()
of var data = [0, 1, 2]; Var width = 100; var height = 100; Var canvas = D 3.select ("body"). Append ("SVG") .attr ("height", 500) .attr ("width", 500) //.attr ("fill", "red") Attached ("g"); Var someData = canvas.selectAll ("rect"). Data (data) .attr ("width", width) .attr ("fill", "red") .attr (" transform ", function (d) {return" translate ("+ height + d +", 0) ";}); Canvas Selection ("G"). Append ("text") .attr ("transform", function (d) {return "translation (" + height * d + ", 30)";}) .attr ("font-size", "2em"). attr ("color", "black") .text (function (d) {return d;});
Comments
Post a Comment