2

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.

kdheepak
  • 1,274
  • 11
  • 22
  • Ooh, cool, I've always wanted this! The `zoom` objects are really complicated, and I don't remember how they work. You can find them in your `ax` object, as `ax.zoom`, `ax.zoom_x` and `ax.zoom_y`. I wish I remembered better what they do, but to find out I would start digging through the js code here: https://github.com/mpld3/mpld3/blob/73473c9ffd8ea36a1912244e664fdb7ce391fd8b/src/core/axes.js#L171 – Abraham D Flaxman Sep 30 '16 at 22:22
  • Thanks for the reply! I've starting digging into the code and I'm hitting some walls. I'll comment here once I have a better idea of what is going on. It'll be good to have this in mpld3 :) – kdheepak Oct 02 '16 at 01:51
  • Okay I have a version working! :) A few more kinks to iron out and it should be good to go. Do you think it should do in the main repo? I think it'll be good if we created a user created plugins repo in the organization. Thoughts? – kdheepak Oct 02 '16 at 04:03
  • Cool, I love it! Go for main repo, either as an example or as an additional plug-in, depending on how complex and general it ended up being. – Abraham D Flaxman Oct 03 '16 at 05:40
  • I've shared the code at the following link on [Github](https://github.com/kdheepak/mpld3_plugins/blob/master/mpld3_plugins/plugins/networkxd3forcelayout.py). I'll definitely submit a pull request for an example right now, and once it's more modular I'll submit another for the plugin. Thanks for all your help! – kdheepak Oct 03 '16 at 16:27

1 Answers1

1

I've posted a working example here for anyone searching for something similar.

kdheepak
  • 1,274
  • 11
  • 22
  • Link-only answers should have a relevant synopsis of the linked content in the answer body. The graph is also available [here](https://mpld3.github.io/examples/networkxd3forcelayout.html). – ggorlen May 17 '20 at 19:41
  • Thanks for linking to that. I think I submitted that PR to their examples after I wrote the blog post. – kdheepak May 18 '20 at 18:09