I'm in the process of creating a mpld3 plugin for converting a NetworkX Graph to a Force Layout. I'm having some trouble understanding how the zoom on the axes works in mpld3 and how I can get it to translate to the force layout graph.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import mpld3
from mpld3 import plugins, utils
from networkx.readwrite.json_graph import node_link_data
class NetworkXD3ForceLayoutView(plugins.PluginBase):
"""A simple plugin showing how multiple axes can be linked"""
JAVASCRIPT = """
mpld3.register_plugin("networkxd3forcelayoutview", NetworkXD3ForceLayoutViewPlugin);
NetworkXD3ForceLayoutViewPlugin.prototype = Object.create(mpld3.Plugin.prototype);
NetworkXD3ForceLayoutViewPlugin.prototype.constructor = NetworkXD3ForceLayoutViewPlugin;
NetworkXD3ForceLayoutViewPlugin.prototype.requiredProps = ["graph", "charge", "linkDistance", "gravity"];
function NetworkXD3ForceLayoutViewPlugin(fig, props){
mpld3.Plugin.call(this, fig, props);
};
var color = d3.scale.category20();
NetworkXD3ForceLayoutViewPlugin.prototype.draw = function(){
var zoom = d3.behavior.zoom();
var height = this.fig.height
var width = this.fig.width
var graph = this.props.graph
var gravity = this.props.gravity.toFixed()
var charge = this.props.charge.toFixed()
var linkDistance = this.props.linkDistance.toFixed()
console.log(graph)
var ax = this.fig.axes[0] // axis required for zoomx and zoomy presumably?
var g = d3.select('.mpld3-axes').append('g') // This is right?
var vis = g.append('svg')
.attr('width', this.width)
.attr('height', this.height);
force = d3.layout.force()
.gravity(gravity)
.charge(charge)
.linkDistance(linkDistance)
.nodes(graph.nodes)
.links(graph.links)
.size([width, height])
.start()
var link = vis.selectAll("line.link")
.data(graph.links)
.enter().append("svg:line")
.attr("class", "link")
.attr("stroke", "black")
.style("stroke-width", function(d) { return Math.sqrt(d.value); })
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
var node = vis.selectAll("circle.node")
.data(graph.nodes)
.enter().append("svg:circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 5)
.style("fill", function(d) { return d.color; })
.call(force.drag);
node.append("svg:title")
.text(function(d) { return d.name; });
vis.style("opacity", 1e-6)
.transition()
.duration(1000)
.style("opacity", 1);
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
zoom.on("zoom", function() {
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
})
g.call(zoom)
};
"""
def __init__(self, G, gravity=0.5, link_distance=20, charge=-10):
self.dict_ = {"type": "networkxd3forcelayoutview",
"graph": node_link_data(G),
"gravity": gravity,
"charge": charge,
"linkDistance": link_distance}
fig, ax = plt.subplots(1, 1)
# scatter periods and amplitudes
np.random.seed(0)
import networkx as nx
G=nx.Graph()
G.add_node(1, color='red')
G.add_edge(1,2)
plugins.connect(fig, NetworkXD3ForceLayoutView(G))
mpld3.display()
The above is a minimum working example that I was able to run in a notebook. I'd added a zoom callback to the group element that contains the graph currently, so the graph will zoom if the mouse if over a node. How do I get it to work when I use the zoom on the custom toolbar. Is this the right approach to creating a force layout plugin? I've also posted here but it might be that SO is a better place for this question.