2

我正在创建一个用于将 NetworkX Graph 转换为 Force Layout 的 mpld3 插件。我在理解 mpld3 中轴上的缩放如何工作以及如何将其转换为力布局图时遇到了一些麻烦。

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()

以上是我能够在笔记本中运行的最小工作示例。我为当前包含图形的组元素添加了缩放回调,因此如果鼠标悬停在节点上,图形将缩放。当我使用自定义工具栏上的缩放时,如何让它工作。这是创建强制布局插件的正确方法吗?我也在这里发布过,但对于这个问题,SO 可能是一个更好的地方。

4

1 回答 1

1

我在这里发布了一个工作示例,供任何搜索类似内容的人使用。

于 2016-10-02T22:07:18.833 回答