score:2
Accepted answer
You could just give the body
a background instead:
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr('y'),
x = text.attr('x')
dy = parseFloat(text.attr('dy')),
tspan = text
.text(null)
.append('tspan')
.attr('x', x)
.attr('y', y)
.attr('dy', dy + 'em');
while ((word = words.pop())) {
line.push(word);
tspan.text(line.join(' '));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(' '));
line = [word];
tspan = text
.append('tspan')
.attr('x', x)
.attr('y', y)
.attr('dy', ++lineNumber * lineHeight + dy + 'em')
.text(word);
}
}
});
}
var svg = d3
.select('body')
.append('svg')
.attr('width', 500)
.attr('height', 5000);
// var longText =
// 'This is very very long text. This is very very long text. This is very very long text. This is very very long text. This is very very long text. This is very very long text.';
// var longText =
// '<span>hi</span>';
var longText = '<span> hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]</span>';
var totHeight = 0;
drawRect();
function drawRect() {
//var someWidth = randomIntFromInterval(40, 300);
var someWidth = 212;
var g = svg
.append('g')
.attr('class', 'foreignObjwrapper')
.attr('transform', 'translate(20,' + totHeight + ')');
const text = g
.append('foreignObject')
.attr('width', 200)
.attr('height', 200)
.attr('id', 'textBox')
.attr('x', 50)
.attr('y', 60)
.append('xhtml:body')
.style("background", "steelblue")
.style("border", "dashed black 1px")
.html(longText);
// var img = g.append('svg:image')
// .attr('x', 250)
// .attr('y', 40)
// .attr('width', 24)
// .attr('height', 24)
// .attr(
// 'xlink:href',
// './img/edit-24-px.svg'
// );
text.on('click', function() {
var newText = '<span> bye[×10<sup>-15</sup> ss<sup>-1</sup>]</span>';
//d3.append('text').html("<span> bye </span>");
text.html(newText);
console.log('hi', newText);
//document.getElementsByClassName("textBox").innerHTML = newText;
});
//var height = text.node().getBBox().height + 25;
// totHeight += height + 10;
// rect.attr('height', height);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div id="body"></div>
Alternatively you can get the height of the body using text.node().clientHeight
:
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr('y'),
x = text.attr('x')
dy = parseFloat(text.attr('dy')),
tspan = text
.text(null)
.append('tspan')
.attr('x', x)
.attr('y', y)
.attr('dy', dy + 'em');
while ((word = words.pop())) {
line.push(word);
tspan.text(line.join(' '));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(' '));
line = [word];
tspan = text
.append('tspan')
.attr('x', x)
.attr('y', y)
.attr('dy', ++lineNumber * lineHeight + dy + 'em')
.text(word);
}
}
});
}
var svg = d3
.select('body')
.append('svg')
.attr('width', 500)
.attr('height', 5000);
// var longText =
// 'This is very very long text. This is very very long text. This is very very long text. This is very very long text. This is very very long text. This is very very long text.';
// var longText =
// '<span>hi</span>';
var longText = '<span> hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]hello[×10<sup>-15</sup> ss<sup>-1</sup>]</span>';
var totHeight = 0;
drawRect();
function drawRect() {
//var someWidth = randomIntFromInterval(40, 300);
var someWidth = 212;
var g = svg
.append('g')
.attr('class', 'foreignObjwrapper')
.attr('transform', 'translate(20,' + totHeight + ')');
const padding = 5;
var rect = g
.append('rect')
.style('fill', 'steelblue')
.attr("stroke", "black")
.attr("stroke-dasharray", "5")
.attr('x', 50)
.attr('y', 60)
.attr('width', someWidth + padding * 2);
const text = g
.append('foreignObject')
.attr('width', someWidth)
.attr('height', 200)
.attr('id', 'textBox')
.attr('x', 50 + padding)
.attr('y', 60 + padding)
.attr('id', 'textBox')
.style('fill', 'black')
.append('xhtml:body')
.style("margin", 0)
.html(longText);
rect.attr("height", text.node().clientHeight + padding * 2);
// var img = g.append('svg:image')
// .attr('x', 250)
// .attr('y', 40)
// .attr('width', 24)
// .attr('height', 24)
// .attr(
// 'xlink:href',
// './img/edit-24-px.svg'
// );
text.on('click', function() {
var newText = '<span> bye[×10<sup>-15</sup> ss<sup>-1</sup>]</span>';
//d3.append('text').html("<span> bye </span>");
text.html(newText);
console.log('hi', newText);
//document.getElementsByClassName("textBox").innerHTML = newText;
});
//var height = text.node().getBBox().height + 25;
// totHeight += height + 10;
// rect.attr('height', height);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div id="body"></div>
Source: stackoverflow.com
Related Query
- How to dynamically adjust the size of a D3 rect based on the foreignObject's height
- How can I dynamically resize an SVG rect based on text width?
- How to adjust the size of a d3.js globe?
- How can I dynamically change the position and the size of direction arrows on a directed d3.js force layout?
- Adjust text font size based on rect size in d3.js
- How to rearrange the DOM order of svg elements based on their data values in a dynamically changing d3 vis?
- How can I center my maps on the container and adjust the size so that it occupies the entire container?
- How increase height dynamically by fixing width of the rect box in d3 js
- How to adjust node size based on value in diagonalNetwork of networkD3
- how to adjust width and height when i click the rect in multilevel treeemap using d3.js?
- In d3.js How to adjust tooltip (up and down) based on the screen position?
- Dynamically resize the d3 tree layout based on number of childnodes
- D3 force layout: How do I set the size of every node?
- How to change the size of D3 nodes onclick
- How to set the size of a d3 symbol?
- How do I adjust zoom size for a point in D3?
- How to use SVG gradients to display varying colors relative to the size of the colored region
- D3: How to dynamically refresh a graph by changing the data file source?
- How to append the arrow marker dynamically in force layout d3.js
- How to set the image size in Vis.js network graph
- How to set SVG's(Draw by D3.js) height based on the content?
- How to assign the center of d3.forceRadial dynamically using functions?
- How to mirror this linear gradient across the x-axis and adjust it upon zooming
- How to change the size of dots in beeswarm plots in D3.js
- d3.js - how to adjust the height of the brush selection
- How to make the extent of a brush match the size of the parent element?
- How to filter children based on whether the parent is filtered in D3?
- D3 word-cloud: how to auto size the font so words will not run out , and to scale words to all the screen?
- How can I determine the overall size of a force directed graph
- How to adjust the direction of arrow in d3.js?
More Query from same tag
- Annotation connectors blocking tooltips
- D3JS Testing - enter() is not fired
- Real-time data with D3
- Javascript not working when Alert is removed
- I'm unable to remove correctly a node from a D3 force graph
- Conditional coloring in dc charts
- Iterative call to a function on d3.js
- Dynamically Centering Filtered Node within SVG Element in D3
- Adding markers to CanvasRenderingContext2D in javascript
- Average in Crossfilter and DC.js
- Dynamic Node Addition/Updation on D3 js Network Graph on button click
- How can I reverse the direction of arrow shown in this jsfiddle http://jsfiddle.net/5qaL886d/1/
- d3js how to select the element i'm currently working on
- Coloring hexbin series with d3js
- Why do the SVG properties of an SVG element sometimes show up as accessible and sometimes not?
- Clickable datapoints that launches closeable modal window in d3 chart (nvd3)
- Extending Bokeh to match D3.js
- jquery dialog box appended to d3 js tree on drag and drop
- How do I Bind Data from a .tsv to an SVG's paths using d3.js for a Choropleth Map
- (D3 line chart) Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaN"
- d3jsv4 timeline chart with vertical month axis during scrub
- Typings Error when using D3 V4 on Angular 2 and Typescript V2
- Adding commas to large numbers in d3.js
- Printing console messages in a output window on HTML using javascript
- d3.js appending text when there is already text on the page
- In D3 I'm assigning a colour to a data category using d3.scale.ordinal(). How can I assign it based on value of x-axis?
- how to reuse two (or more) sequences of chained transitions in D3
- Add image at top of each D3 bar rect
- Two geojson files not superposing
- d3.js - CircleLayout