javascript - Get width of d3.js SVG text element after it's created -


i'm trying widths of bunch of text elements have created d3.js

this how i'm creating them:

var nodestext = svg.selectall("text")            .data(dataset)            .enter()            .append("text")            .text(function(d) {                 return d.name;            })           .attr("x", function(d, i) {                 return * (w / dataset.length);            })           .attr("y", function(d) {                 return 45;           }); 

i'm using width create rectangles same size text's boxes

var nodes = svg.selectall("rect")             .data(dataset)             .enter()             .append("rect")             .attr("x", function(d, i) {                 return * (w / dataset.length);             })             .attr("y", function(d) {                 return 25;             })             .attr("width", function(d, i) {               //to do: find width of each text element, after has been generated               var textwidth = svg.selectall("text")                   .each(function () {                       return d3.select(this.getcomputedtextlength());                   });                 console.log(textwidth);                 return textwidth;             })             .attr("height", function(d) {                 return 30;             }) 

i tried using bbox method here don't understand it. think selecting actual element i'm going wrong really.

i make length part of original data:

var nodestext = svg.selectall("text")        .data(dataset)        .enter()        .append("text")        .text(function(d) {             return d.name;        })       .attr("x", function(d, i) {             return * (w / dataset.length);        })       .attr("y", function(d) {             return 45;       })       .each(function(d) {         d.width = this.getbbox().width;       }); 

and later

var nodes = svg.selectall("rect")         .data(dataset)         .enter()         .append("rect")         .attr("width", function(d) { return d.width; }); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -