score:16
The answer to this question might be relevant. SVG provides no way of wrapping text automatically, but you can embed HTML within SVGs and then use a div
for example.
I've updated the jsfiddle here, but it doesn't work that well together with the animation. If you want to make it work properly and behave like any other SVG element, you'll have to pre-compute the line breaks and insert them manually.
score:2
I had a similar issue and found a reasonable solution by calculating the width of my box. Secondly, I figured out that on average the character width for my current font is about 8. Next I simply do a substring on the text to be displayed. That seems to work perfectly in most cases.
var rectText = rectangles.append("text")
.text(function(d) {
TextBoxLength = timeScale(dateFormat.parse(d.endTime)) - timeScale(dateFormat.parse(d.startTime));
return d.task.substring(0, Math.floor(TextBoxLength / 8));
})
.attr("x", function(d) {
return (timeScale(dateFormat.parse(d.endTime)) - timeScale(dateFormat.parse(d.startTime))) / 2 + timeScale(dateFormat.parse(d.startTime)) + theSidePad;
})
.attr("y", function(d, i) {
return d.position * theGap + 14 + theTopPad;
})
.attr("font-size", 12)
.attr("text-anchor", "middle")
.attr("text-height", theBarHeight)
.attr("fill", "#000000");
score:2
Another approach, when trying to fit a straight line of text into an svg element, could use the strategy found in http://bl.ocks.org/mbostock/1846692:
node.append("text")
.text(function(d) { return d.name; })
.style("font-size", function(d) { return Math.min(2 * d.r, (2 * d.r - 8) / this.getComputedTextLength() * 24) + "px"; })
.attr("dy", ".35em");
score:3
This technique might come in handy. It works with the current svg spec and without foreignObject element http://bl.ocks.org/mbostock/7555321
score:14
To make it work with the animations just enclose in a group element and animate that one instead. http://jsfiddle.net/MJJEc/
body = d3.select('body')
svg = body.append('svg')
.attr('height', 600)
.attr('width', 200);
var g = svg.append('g').attr("transform" ,"scale(0)");
rect = g.append('rect')
.attr('width', 150)
.attr('height', 100)
.attr('x', 40)
.attr('y', 100)
.style('fill', 'none')
.attr('stroke', 'black')
text = g.append('foreignObject')
.attr('x', 50)
.attr('y', 130)
.attr('width', 150)
.attr('height', 100)
.append("xhtml:body")
.html('<div style="width: 150px;">This is some information about whatever</div>')
g.transition().duration(500).attr("transform" ,"scale(1)");
Source: stackoverflow.com
Related Query
- Fit Text into SVG Element (Using D3/JS)
- Match SVG text element id with data id using D3
- D3JS: help to create a SVG text element inside a circle using D3js
- Add text into an svg path using d3
- Text not wrapping in d3 SVG element even when using Bostock's wrap function
- Limiting Width on a Text element in SVG using D3.js
- How do I get the width of an svg element using d3js?
- Append SVG canvas to element other than body using D3
- Get width of d3.js SVG text element after it's created
- how to assign unique id to SVG text element in d3 javascript
- Sizing of SVG foreignObject element to fit HTML element
- Making a SVG Text draggable using d3.js
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- Renderer2 does not render SVG element while using d3 js - Angular 4
- How can I convert SVG coordinates (x,y) defined relatively to an element into absolute coordinates/position?
- duplicating the whole svg element using d3.js
- How to transform (rotate) svg text element without changing coordinates?
- d3 Trying to select text within a svg text element
- SVG text element text-anchor="middle" not working in Firefox
- Using Angular ng-class with d3.js on svg element
- Vertical alignment between an SVG rect and SVG text using D3 and javascript
- deduct height of text element using d3
- Unable to Apply CSS style to SVG text element
- Using D3 to generate SVG Pie Chart how to vertically align text
- Access the text within SVG object using d3js
- insert svg string element into an existing svg tag
- Fit text on svg textpath
- Using d3.behavior.drag, How to keep a drag image visible outside of the svg element
- Is it possible to insert an SVG image into an existing SVG element already rendered on the page?
- Svg text element being overlapped by another svg element
More Query from same tag
- bubble chart color by parent class
- d3 js drag event listener isnt getting the event - (d3, react)
- Draw line from HTML table cell to cell
- Export data to csv using Entity Framework
- Drawing a line between two y axis in d3js
- Detect Graph Edges Overlapping
- How to import and use a modified npm library packages dynamically
- nvd3 chart only displaying after resize
- d3 + drawing svgs + incorrectly overwriting
- Correctly scale DOT graphic
- D3 updating colors in co-occurence chart
- Is it possible to pass the scope from one controller file to another file
- Margin Convention bugs?
- Reorder group elements to a specific order
- display text inside circle on mousehover using d3.js
- d3 accessing nested data in grouped bar chart
- d3js zoom into plot
- Separate json data from html?
- How to add filter result to select menu
- d3.js in drupal 7
- d3 dependency loading before d3
- D3js collapse objects with duplicate properties
- Showing Percentages in dc.js Row Charts
- d3js: adding same type of elements with different data
- How to put two 1D frequency distribution charts next to each other?
- Rotate tree diagram on d3.js v5 (from horizental to vertical)
- Add circles to a line
- hide one of the axes of a parallel-coordinates plot
- D3 : get value from the array
- nvd3 pie outerRadius