I am looking to implement mousewheel zoom functionality for my sunburst visualization:
I have made changes to the following code snippet:
var zoom = d3.behavior.zoom()
.scaleExtent([1, 10])
.on("zoom", zoomed);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width/2 + "," + height * .52 + ")")
.call(zoom);
function zoomed() {
svg.attr('transform', 'translate(' + d3.event.transform.x + ',' + d3.event.transform.y + ') scale(' + d3.event.scale + ')');
}
Initially, everything appears fine on page load. However, when using the mousewheel to scroll, the sunburst ends up in the upper left corner:
https://i.sstatic.net/6B6xG.jpg
To address this issue, I modified the code as follows:
function zoomed() {
svg.attr("transform", "translate(" + width/2 + "," + height * .52 + ")scale(" + d3.event.scale + ")");
}
The sunburst now remains centered, but unfortunately does not zoom to the cursor position and restricts further zoom out capability.
Any pointers on how I can achieve zooming to the cursor position (as shown in the original code) without the sunburst disappearing into the upper left corner?
To view the complete code, please refer to this JSFiddle link. While it may not work here, running it locally should demonstrate the desired behavior.