score:3
Accepted answer
You are performing your zoom/pan transformation on the svg
element. It's usually applied on a g
element that wraps what you are zooming. To use a g
, in your case, you'll need to insert this element into your already existing svg
:
<!DOCTYPE html>
<html>
<head>
<script data-require="d3@4.0.0" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
var main_chart_svg = document.body;
d3.xml("https://upload.wikimedia.org/wikipedia/commons/a/a0/Circle_-_black_simple.svg", function(error, documentFragment) {
if (error) {
console.log(error);
return;
}
var svgNode = documentFragment
.getElementsByTagName("svg")[0];
main_chart_svg.appendChild(svgNode);
var svg = d3.select('svg'),
g = svg.append('g');
g.node().appendChild(svg.select('circle').node());
d3.selectAll('circle').style('fill', 'green'); //only in order to show, that selections work
//this is where my problem happens
d3.select('svg').call(d3.zoom().on("zoom", function() {
g.attr("transform", d3.event.transform)
}));
});
</script>
</body>
</html>
Alternatively, if you don't want to do that, you could apply the zoom directly to the circle:
<!DOCTYPE html>
<html>
<head>
<script data-require="d3@4.0.0" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
var main_chart_svg = document.body;
d3.xml("https://upload.wikimedia.org/wikipedia/commons/a/a0/Circle_-_black_simple.svg", function(error, documentFragment) {
if (error) {
console.log(error);
return;
}
var svgNode = documentFragment
.getElementsByTagName("svg")[0];
main_chart_svg.appendChild(svgNode);
var svg = d3.select('svg'),
c = d3.select('circle');
c.style('fill', 'green'); //only in order to show, that selections work
//this is where my problem happens
d3.select('svg').call(d3.zoom().on("zoom", function() {
c.attr("transform", d3.event.transform)
}));
});
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- How to add Zoom + Drag Behaviour to an external SVG loaded into D3.js?
- How to add external svg file (by D3.js) to Leaflet map
- How can I add an external group to an existing svg with d3.js?
- d3.js add zoom functionality to external svg file
- How to drag drop elements into svg group
- How to append an svg loaded by d3.xml into an svg
- How is external (cross-domain) JSON loaded into D3.js using Python/Flask/Jinja?
- How to add a tooltip to an svg graphic?
- How to add an image to an svg container using D3.js
- How to add border/outline/stroke to SVG elements in CSS?
- How to add in zero values into a time series in d3.js / JavaScript
- How to drag an svg group using d3.js drag behavior?
- How to add external javascript file in Ipython notebook
- How to add filled sections to SVG circles using d3.js
- D3.js - how to add zoom button with the default wheelmouse zoom behavior
- How can I convert SVG coordinates (x,y) defined relatively to an element into absolute coordinates/position?
- How to use mouse click and drag to zoom in D3
- how do i add a zoom in and zoom out button in a graph on d3
- how to add dragend event on svg element
- How to get background color of SVG converted properly into Canvas
- How to drag and drop a group of rectangles in svg in d3.js?
- How to add a force drag event in D3 and make the node stay where i leave it?
- Zoom to bounding box of path on externally loaded svg using D3
- How to get SVG circle to drag along SVG path?
- How to add text alternative for external images in SVG?
- D3.js v4 - how to add zoom button with the default wheelmouse zoom behavior
- How to add links from CSV file to SVG elements generated with D3?
- How to add labels into the arc of a chord diagram in d3.js
- How to add a (mouse)event to a XMLDocument of a SVG
- How do I add a transform attribute to svg subgroups in d3.js?
More Query from same tag
- D3 - SVG elements being positioned from body, not SVG container
- Hover on legend and hover on path effect in nvD3
- fill lines D3 js
- D3- Approach for Json nested key-value
- Update page JSON data get on drop down change
- Should I concatenate my dependencies on my Javascript library?
- Calculating the depth of nodes in force layout in d3
- Dimple.js Candlestick Chart
- Angular with D3 js
- How to word wrap legend labels in d3
- NVD3 MultiBarChart Stacked Display with Non-Normalized Data
- make a circle progress bar, but animate arc.endAngle (angle >180 degree) stops working in d3.js
- Is it possible to feed a Danfojs Series object to D3js data() binding method?
- d3.js radar chart with images provided in data
- c3.js how to get a onclick event for DataGroups
- Prevent image rotation into SVG circle on d3 radial tree
- How to add drop shadow to d3.js pie or donut chart
- How to make D3 Streamgraph vertically
- d3 compound interest debt
- How to append a new div element that is sibling of first div created using d3?
- d3.js Linking multi-line chart with bar chart on mouseover
- D3 Modify Path's y
- How to hierarchically append "composed" elements based on elements' attributes using D3 d3js?
- SVG CSS classes overriding each other in d3
- Display one day with d3.slider
- bar chart in d3 using mysql database
- D3 Dimple - How to show multiple dimple charts on same page?
- Why don't the circles in this d3 example move?
- Concept Map Network - Node Issue
- How to stop/pause a SPECIFIC force in d3?