score:1
You are almost there. I have illustrated both ways to add elements in this codepen. The problem with the first approach is that svgNode
is not a node
, but a nodeList
. You can add each node in the list with a loop, as you can see in the code:
...
var toAdd = svgNode.childNodes;
for (var i = 0; i < svgNode.childElementCount; i++){
svg.node().appendChild(toAdd[i]);
}
...
Regarding the second approach, I cannot say that there is anything wrong. I have imported a path from a different svg file and it is displayed in the lower part. The only detail that I changed is that I set the size of the svg element explicitly, not as 100%
. Is it possible that it was not rendered because it was outside the visible part?
score:3
Just as a complement to the accepted answer: if you want to a pure D3 solution, you can use selection.append
with a function. In your case:
d3.xml(svgfile).then(function(xml) {
var svgNode = d3.select(xml).select("#MyGroup");
svg.append(() => svgNode.node()) ;
});
Or even shorter:
d3.xml(svgfile).then(function(xml) {
svg.append(() => d3.select(xml).select("#MyGroup").node()) ;
});
Here is a demo using the Wikipedia SVG linked in the other answer:
var svgfile = "https://simple.wikipedia.org/static/images/mobile/copyright/wikipedia-wordmark-en.svg";
var svg = d3.select('body').append("svg")
.attr("width", '120')
.attr("height", '20');
d3.xml(svgfile).then(function(xml) {
var svgNode = d3.select(xml).select("#Wikipedia");
svg.append(() => svgNode.node());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- How can I add an external group to an existing svg with d3.js?
- How to update the fill color on existing svg elements with d3.js?
- How to move elements along with svg group
- How to add external svg file (by D3.js) to Leaflet map
- How can I add graphs/data to Rickshaw with d3.js?
- How can I clone and modify an existing group into a selection?
- How to use D3 force layout with existing SVG elements as nodes
- How can I make a SVG group follow a gradient in a circle
- How to add links from CSV file to SVG elements generated with D3?
- How to apply plotly.js to an existing svg group <g> as target instead of applying it to a target <div> element?
- How to add back to DOM an existing D3 SVG chart?
- How do you update an existing svg element with the d3v5 join syntax
- How can I scale my map to fit my svg size with d3 and geojson path data
- With D3, how can I avoid SVG graph links being rendered over nodes?
- How can I apply a clipPath in SVG with multiple paths and NOT clip out the area between the paths?
- How can we add legend's value beside of legend with proper alignment and different color in dc.js
- How can I add labels to my diagram with d3.js?
- How can I make 2 different elements in a SVG group have different drag behaviours?
- How to modify tags of an already existing svg object with data from json file
- D3: How can I cause the stroke of an SVG path generated with D3 to be clipped to the interior of the same path?
- How can i group elements that are bound to different data (to be used with forcesimulatoin) in D3 so that I can sort them
- How can I keep existing SVG shapes instead of replacing them?
- How to add Zoom + Drag Behaviour to an external SVG loaded into D3.js?
- how can we add text to a svg circle in d3js
- How can I properly aggregate / group multiple line graphs into one overall graph with d3.js when the x-values aren't matching exactly?
- How to add a tooltip to existing svg circles?
- How can I add text to SVG elements in D3 using the example below
- How to add two elements (circle and text) to SVG g element with D3js?
- d3js - How to update an existing svg path in a simple d3 line chart with new data flowing through?
- How can I toggle the value of an svg text element with D3.js when user clicks the text?
More Query from same tag
- Using d3 SVG Scale without translate
- Access key/value in a paired-bar-chart - D3.js
- a variable is being set as Nan at one place, but correctly at another location
- How to display two graph generated from separate code from “observablehq” on one browser window
- Best way to arrange tick text using d3
- Append external svg using d3 and Webpack in IE11
- reading CSV content in ejs
- How to get the axis ticks of Plotly's scatter plot to be shown in Latex using Python? --- [Python, Plotly, Latex Representation]
- dc.js geochoropleth map scaling
- Remove svg onclick for d3.js wordcloud
- Make d3 svg path clickable (in Google Maps Overlay)
- d3 liquid chart bugs - clipping path jarring
- How to center title over SVG chart with d3.js?
- Test with cypress graphics created with d3.js
- Make responsive D3 Hierarchical bar chart and show tooltip
- Get value from json value key pair with D3
- Chained transitions don't trigger step-by-step
- D3 Key Function
- Sorting bar charts in D3
- D3 Chart Legend Labels getting overlapped
- d3.js / javascript efficient code
- Responsive D3 Smooth Slider - change slider width on window resize
- Text label hides in circle d3
- Populating a select list of connected nodes in d3.js
- How to create a D3 Bubble Chart in straigth line path?
- D3 tooltip won't read my data
- D3 directed graph nodes as rectangle
- When integrate elasticsearch with d3.js, "Uncaught TypeError: Cannot read property 'Client' of undefined" I get this error
- d3.js - Force-Layout boundary issue V4
- TopoJSON map not rendering properly using D3