简体   繁体   English

用Springy力导向图替换HTML canvas的内容

[英]Replacing contents of HTML canvas with Springy force-directed graph

I have a nice force-directed graph using the Springy force-directed graph layout library. 我有一个使用Springy力导向图布局库的不错的力导向图。 I've discovered that when I replace the graph with another via ajax (eg, after the user has changed some settings), both graphs occupy the same canvas. 我发现当我通过ajax将图形替换为另一个图形时(例如,在用户更改了某些设置之后),两个图形都占用相同的画布。

What I'm looking for: I need to get rid of the old graph completely, so the new graph is the only one present in the canvas. 我要寻找的是:我需要完全摆脱旧图形,因此新图形是画布中唯一的图形。

Here's a simplified usecase jsfiddle: http://jsfiddle.net/XPAqX/ 这是一个简化的用例jsfiddle: http : //jsfiddle.net/XPAqX/

// make a new graph
var graph = new Springy.Graph();

// make some nodes
var spruce = graph.newNode({label: 'Norway Spruce'});
var fir = graph.newNode({label: 'Sicilian Fir'});

// connect them with an edge
graph.newEdge(spruce, fir);


$('#my_canvas').springy({ graph: graph, nodeSelected: function(node) {
    alert(node.data.label);
} });

//now, I let the user update the dataset with ajax and re-render the graph. 

graph = null;
graph = new Springy.Graph();



// make some nodes
var kittens = graph.newNode({label: 'Furry Baby Cats'});
var puppies = graph.newNode({label: 'Fluffy Baby Dogs'});

// connect them with an edge
graph.newEdge(kittens,puppies);


$('#my_canvas').springy({ graph: graph });

Quick note: cross-posted as an issue on the springy github, no answers yet though: https://github.com/dhotson/springy/issues/47 快速说明:在Springy github上作为问题交叉发布,但尚无答案: https : //github.com/dhotson/springy/issues/47

The Springy Graph object is a little misleading. Springy Graph对象有点误导。 If you check out the sources you'll see it's just a access layer over the whole Springy context object (a signleton) that actually holds your nodes, so addNode does not add nodes to the Graph object but to a singleton Springy context object. 如果您查看源代码,您将看到它只是整个Springy上下文对象(符号)的访问层,该访问层实际上包含您的节点,因此addNode不会将节点添加到Graph对象,而不会添加到Singleton Springy上下文对象。

You'll have to use removeNode to remove the old nodes first, like I did here . 您必须先使用removeNode删除旧节点,就像我在这里所做的那样。

Just to make things clearer: Yes, Graph objects do contain lists of nodes, but those are passed to the renderer, and the renderer is a single instance for each canvas DOM node you 'springyfy'. 为了使事情更清楚:是的,Graph对象确实包含节点列表,但这些列表已传递给渲染器,并且渲染器是您“ springyfy”的每个画布DOM节点的单个实例。 Changing the Graph instance won't reset the renderer's internal context. 更改Graph实例不会重置渲染器的内部上下文。

A more complicated solution would be to modify the SpringyUI (jQuery plugin) code and handle a $('#canvas').springy('reset') method that resets the internal renderer. 一个更复杂的解决方案是修改SpringyUI (jQuery插件)代码并处理用于重置内部渲染器的$('#canvas')。springy('reset')方法。 Looking at the SpringyUI code I can say that won't be easy. 查看SpringyUI代码,我可以说这并不容易。

edit: (deleted all the wrong stuff of me) 编辑:(删除了我所有的错误资料)

to get a working solution: 获得可行的解决方案:

add one of the following clear function to the springyui.js 向springyui.js添加以下清除函数之一

this.clear = function(){
   for(var i=graph.nodes.length-1;i>=0;i--){
     graph.removeNode(graph.nodes[i]);
   }
}

or 要么

this.clear = function(){
    graph.nodeSet = {};
    graph.nodes = [];
    graph.edges = [];
    graph.adjacency = {};
    graph.notify();
}

use it like this: 像这样使用它:

initial: 初始:

//don't call this twice!!
var graph = new Springy.Graph();
var springy=jQuery('#yourcanvas').springy({graph: graph});

workflow: 工作流程:

function makeGraph(){
    springy.clear();
    //generate your graph hear
    graph.addNodes(...)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM