Reputation: 90
This question covers exactly the same topic as this one already asked and answered:
I have also tried to implemented the code that it is in this link http://bl.ocks.org/linssen/7352810.
The reason I am asking here is because the solutions are given using version 3 of D3.js and none of those solve my problem. This is my current implementation regarding the zooming.
let translateVar = [0,0];
let scaleVar = 1;
function create_pan_zoomable_svg() {
let svg = d3.select("body")
.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.style("background-color", "#eeeeee")
.call(_zoom).on("dblclick.zoom", null)
.append("g");
d3.select("#zoom_in").on('click', function() { _zoom.scaleBy(svg, 2)});
d3.select("#zoom_out").on('click', function() { _zoom.scaleBy(svg, 0.5)});
return svg;
}
var _zoom = d3.zoom()
.on("zoom", function() {
translateVar[0] = d3.event.transform.x;
translateVar[1] = d3.event.transform.y;
scaleVar = d3.event.transform.k;
svg.attr('transform', 'translate(' + translateVar[0] + ',' + translateVar[1] + ') scale(' + scaleVar + ')');
});
I found this post d3.js V4 Button Zoom Implementation acting weird, thinking that it might solve my problem, but adapting the code to mine, turns out that the wheelmouse-based zoom is no longer working and just the buttons. This is the adapted code from such post.
function create_pan_zoomable_svg() {
let zoom = d3.zoom().on("zoom", zoomed);
let svg = d3.select("body")
.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.style("background-color", "#eeeeee")
.append("g").call(zoom).on("dblclick.zoom", null);
d3.select("#zoom_in").on("click", function() {
zoom.scaleBy(svg.transition().duration(750), 1.2);
});
d3.select("#zoom_out").on("click", function() {
zoom.scaleBy(svg.transition().duration(750), 0.8);
});
return svg;
}
function zoomed() {
svg.attr("transform", d3.event.transform);
}
Another problem is that if I were to put the append("g")
after calling the zoom
both based zooms will work but they will not keep track of the translation.
Please visit for diagnosing the problem: https://jsfiddle.net/vxhuzyp2/5/
Any help would be much appreciated.
Upvotes: 2
Views: 8560
Reputation: 28633
Follow the comment and read through the confusion of naming a var svg and and creating an svg
element.
let zoom = d3.zoom().on("zoom", zoomed);
let svg = d3.select("svg")
.attr("width", "100%")
.attr("height", "100%")
.style("background-color", "#eeeeee")
.call(zoom)
.on("dblclick.zoom", null);
let g = svg.append("g");
//d3.selectAll("#zoom-section i").on("click", zoomClick);
g.append("circle").attr("r", 50).attr("fill", "red");
d3.select("#zoom_in").on("click", function() {
zoom.scaleBy(svg.transition().duration(750), 1.2);
});
d3.select("#zoom_out").on("click", function() {
zoom.scaleBy(svg.transition().duration(750), 0.8);
});
function zoomed() {
g.attr("transform", d3.event.transform);
}
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<button id="zoom_in">+</button>
<button id="zoom_out">-</button>
Upvotes: 12