I have implemented an autocomplete feature in my force directed graph to highlight selected nodes by coloring them red. Now, I am looking to implement a "zoom" functionality where the window magnifies to 400% the size of the selected node and centers it within the view.
Below are snippets of the relevant code sections: (or you can directly access the jsFiddle demo I created.)
Firstly, here is the code used to create the svg
element:
var w = 4000,
h = 3000;
var vis = d3.select("#mysvg")
.append("svg:svg")
.attr("width", "100%")
.attr("height", "100%")
.attr("id","svg")
.attr("pointer-events", "all")
.attr("viewBox","0 0 "+w+" "+h)
.attr("perserveAspectRatio","xMinYMid")
.append('svg:g')
.call(d3.behavior.zoom().on("zoom", redraw))
.append('svg:g');
Additionally, here's an example function for redrawing the graph upon normal zoom:
function redraw() {
trans=d3.event.translate;
scale=d3.event.scale;
vis.attr("transform",
"translate(" + trans + ")"
+ " scale(" + scale + ")");
}
Displayed below are the nodes of the graph:
vis.selectAll("g.node")
.data(nodes, function(d) {return d.id;})
.enter().append("g")
.append("circle")
.attr("id", function(d){return "circle-node-"+ d.id})
.attr("fill","white")
.attr("r","50px")
.attr("stroke", "black")
.attr("stroke-width","2px");
Lastly, here's the implementation of the autocomplete feature:
$(function() {
$( "#tags" ).autocomplete({
source: nodes; //...
select: function( event, ui){
// ...
vis.selectAll("#circle-node-"+ui.item.value)
.transition()
.attr("fill", "red")
}
})
});
I have provided minimal code snippets to avoid overwhelming with information. Apologies if anything was overlooked.
Update: To see my progress so far, check out this jsFiddle demonstration.