A namespace cannot be assigned by attr, it's a side effect of element creation. You need an html div so you need to tell d3 that by calling the element xhtml:div, once you do that, d3 will do the rest.
var group = d3.select("#package"); var fo = group.append("foreignObject").attr("x", 15).attr("y", 15).attr("width", 190).attr("height", 90); fo.append("xhtml:div").attr("style", "width:190px; height:90px; overflow-y:auto").text("Thiggfis the dgdexsgsggs wish to fit insidegssgsgs");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <p id="p"></p> <svg width="220" height="120" viewBox="0 0 220 120" id="package"> <rect x="10" y="10" width="200" height="100" fill="none" stroke="black"/> </svg>
Here's a simple example of a
foreignObject used to insert HTML markup into an SVG:
<svg width="220" height="120" viewBox="0 0 220 120"> <rect x="10" y="10" width="200" height="100" fill="none" stroke="black" /> <foreignObject x="15" y="15" width="190" height="90"> <div xmlns="http://www.w3.org/1999/xhtml" style="width:190px; height:90px; overflow-y:auto"><b>This</b> is the <i>text</i> I wish to fit inside <code>rect</code></div> </foreignObject> </svg>
- How to pack text inside svg rect
- How can I dynamically resize an SVG rect based on text width?
- How do I use d3.js to display text in an SVG rect only if the text will fit?
- How to give padding on top/bottom/left/right to text that is contained inside a rect
- How To Align Text Inside SVG In D3.js
- How to wrap the text inside the SVG polygon without cutting the text
- How to center text in a rect element in d3?
- D3js : How to convert svg text to path?
- How to match text width to circle size in D3 circle pack
- D3, SVG and textPath: How to move the text down?
- How to right/end align text along an textPath inside an arc using d3.js?
- How to center 90º rotated text in SVG
- How can I center text on a D3.js SVG grid rect?
- How to fill an image inside my svg circles in d3.js
- Changing size of rect to fit inside text
- How do I select an SVG inside an `object` tag?
- How to wrap text in SVG vertically in D3.js?
- How to transform (rotate) svg text element without changing coordinates?
- d3.js / svg - how to dynamically append text to my arcs
- Add text in rect svg and append it to arc in donut chart
- How to Hide Overlapping Text of Labels in D3 Zoomable Pack Layout?
- How to prevent SVG text from flowing out of its circle?
- How to remove svg inside a div
- In D3, how does one place text inside shapes that already have text attached to them?
- D3 How to zoom in on SVG text that stays within an SVG Rectangle?
- D3 SVG Move zoom to rect inside <g> having different angle
- Using D3 to generate SVG Pie Chart how to vertically align text
More Query from same tag
- Map reducing a collection of coordinate pairs
- Unable to append images over nodes in d3.js
- using CSV instead of TSV data in d3.js
- Dimple Filter for X Value
- d3 table coming out of the div boundary
- d3js chart transition not functioning properly
- Text being covered by paths in d3 pie chart
- D3.js: How to select data from graph's brushed area?
- Preserve d3 zoom state when changing out SVG contents?
- Remove old points from line graph in d3
- Can we change the padding property of pack layout with a transition?
- D3.js Bar chart - Aligning labels with custom bar width
- Load new page using d3
- D3 .on sends 3 arguments to a callback function
- Color scale not working in treemap in d3
- Delaying load of d3 element within loop
- "export 'mouse' (imported as 'd3') was not found in 'd3'
- d3.js dynamic time series like shifting effect
- Prevent d3.js from showing same dates multiple times in x-axis
- brush extents not working as expected
- How to get the path string for D3 Axis
- Crossfiltering of JSON data by the most recent time
- Change the size of SVG according to device size in D3.js?
- Different shapes according to data in D3 force layout
- creating a line chart using C3
- Why does D3 not update the text
- D3.js v4.0.0-alpha.35 Transitions not working
- Add event listener when a `<div>` tag changes size on screen
- Zooming bar-graph with d3.js