javascript - Modify Existing d3 code - dynamic rectangle height to match text -


i working code @ codepen base build gantt chart needs. however, trying modify code actual rectangle heights accommodate text given them. in example shown, of task texts 1 word long fit within rectangle. however, if task several words long , rectangle width short, text not wrap , overflows.

how modify code rectangle heights drawn fit text within them, or alternatively have text wrap , have rectangle grow (in height) accommodate text?

right rectangle heights hard-coded in codepen example:

var barheight = 20; 

the example adds text in following way rectangles (see below). i've experimented trying put html in rectangle instead of text no avail:

 var recttext = rectangles.append("text")                .text(function(d){                 return d.task;                }) 

you asking 2 questions. one, how wrap text , two, how scale rect height wrapped text.

1.) canonical example of wrapping text presented m. bostock here.

2.) scale height of rect text then, can use .getbbox() @benlyall hints at. first wrap text, call .getbbox() on text node , apply height rect.

here's complete example:

var somewidth = math.random() * 250;  var longtext = "now time men come aid of country";   var rect = g.append('rect')     .style('fill','steelblue')     .attr('width', somewidth) //<-- random width don't know     .attr('height', 1); // <-- start arbitrary height    var txt = g.append('text')     .text(longtext) //<-- our super long text     .attr('x', 4)     .attr('y', 10)     .attr('dy', '.71em')     .style('fill', 'white')     .call(wrap, somewidth); //<-- wrap according our width    var height = txt.node().getbbox().height + 15; //<-- our height plus margin   rect.attr('height', height); //<-- change our rect 

here's working example.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -