gpt4 book ai didi

javascript - 适用于 mpld3 的 NetworkX D3 强制布局插件

转载 作者:行者123 更新时间:2023-11-28 05:29:57 25 4
gpt4 key购买 nike

我正在创建一个 mpld3 插件,用于将 NetworkX 图形转换为强制布局。我在理解 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()

上面是我能够在笔记本中运行的最小工作示例。我向当前包含图形的组元素添加了缩放回调,因此如果鼠标悬停在节点上,图形将会缩放。当我使用自定义工具栏上的缩放功能时,如何让它发挥作用。这是创建力布局插件的正确方法吗?我还发布了here但也许 SO 是解决这个问题的更好地方。

最佳答案

我发布了一个工作示例 here对于任何搜索类似内容的人。

关于javascript - 适用于 mpld3 的 NetworkX D3 强制布局插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39783547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com