d3.js - assign text to rectangles not working -


i dynamically creating rectangles , assigning 1 of data values text rectangle. in developer tools can see value not on rectangles.

code:

var x = d3.scale.linear().domain([0, data.length]).range([0, 1200]);                         console.log(filtereddata);                         rectangle= svg.selectall("rect").data(filtereddata).enter().append("g").append("rect");                           rectangleattrb = rectangle                         .attr("id", function (d,i) { return "rectid" + ; })                         .attr("x", function (d,i) {                                                     return x(i);                                                    })                         .attr("y", function (d,i) { return 40; })                         .attr("width",function(d,i) {                                                          if(d.value <100)                                                          {                                                             return 50;                                                          }                                                         else                                                          {                                                             return d.value/2 ;                                                          }                                                     } )                         .attr("height",function(d) { return 40; })                         .style("stroke", function (d) { return "white";})                         .style("fill", function(d) { return "#01df01"; })                         .on("click",function(d,i) { console.log(d);});                         console.log(rectangleattrb);                         rectangle.append("text")                       .attr("x", function(d,i) { return x(i) + 5; })                       .attr("y", 35)                       .attr("dy", ".35em")                       .attr("text-anchor", "middle")                        .attr("fill", "black")                       .text(function(d) { return d.value; }); 

my data :

[{status:1;value:300}, {status:2;value:200}, {status:3;value:50}] 

you need append rectangle tag , append text same . 'g' tag parent of both rect , text :))

as mentioned in comments here question refer :

svg: text inside rect


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -