javascript - VISJS : save manipulated data to json

标签 javascript vis.js vis.js-network

我正在使用 vis.js 在网页上创建网络图。

我的需求是将操作图以 JSON 格式存储到数据库,从网络图中导出 json。

我没有找到任何相关文档,是否可以导出带有操纵数据的 vis.js 网络进行存储(以 JSON 或可转换为 JSON 的形式)?

最佳答案

至于数据,这是我提取它进行存储的怪异方法(我将尝试切断与问题无关的代码位):

// get nodes and edges
var nodes = network.body.data.nodes._data; // contains id, label, x,y, custom per-node options and doesn't contain options from options.nodes; presumably contains option values set when network was created, not current ones (it is so for x,y)
// network.body.nodes[id].nodeOptions shows options from options.nodes but not custom per-node options (same for edges and network.body.edges[id].edgeOptions)
// network.body.nodes contain much more stuff (x,y, default stuff)
//# look for a suitable getter
var edges = network.body.data.edges._data; // map; for edges to/from? certain node use network.getConnectedNodes(id)
// network.body.data.edges._data is a hash of { id: , from: , to: }

// get node positions
var positions = network.getPositions(),
    nodeIds = Object.keys(nodes);

// get data describing nodes, edges and options for storage
var storedEdges = [], storedEdge, storedNodes = [], storedNode;
var indexIds = {}, idIndex = 1, end;

for(var nodeId in nodes) {
    // nodes[nodeId].x is the initial value, positions[nodeId].x is the current one
    if(positions[nodeId]) { // undefined for hidden
        nodes[nodeId].x = positions[nodeId].x;
        nodes[nodeId].y = positions[nodeId].y;
    }
    storedNode = copyObjectProperties(nodes[nodeId]);

    // don't store id unless that breaks connected edges
    if(!network.getConnectedEdges(nodeId).length)
        storedNode.id = undefined;
    // substitute generated ids with no semantics with simple indices
    if(/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/.exec(storedNode.id)) {

        while(nodes[idIndex])
            idIndex++;
        indexIds[storedNode.id] = idIndex; // remember the given index
        storedNode.id = idIndex; // substitute with an index
        idIndex++;
    }

    storedNodes.push(storedNode);
}
for(var edgeId in edges) {
    storedEdge = copyObjectProperties(edges[edgeId]);
    storedEdge.id = undefined; // then strip id

    // change from/to in accord to the substitution above (for nodes' ids)
    for(end of ["from","to"])
        storedEdge[end] = indexIds[storedEdge[end]] || storedEdge[end];

    storedEdges.push(storedEdge);
}

dataAndOptions = {
    data: { nodes: storedNodes, edges: storedEdges },
    options: storedOptions
};

var dataAndOptionsText = JSON.stringify(dataAndOptions,"",4)
    .replace(/ {4}/gm,"\t").replace(/},\n\t\t\t{/gm,"},{");

和助手定义:

// helper for storing options
var copyObjectProperties = function(obj) {
    return JSON.parse(JSON.stringify(obj));
};

有关更多上下文,请参阅 my plugin对于 TiddlyWiki Classic(saveDataAndOptions 方法)。这不是最新版本,但我会在某个时间更新它。

至于网络选项(如果已更改),我 haven't figured一个不错的方式。

关于javascript - VISJS : save manipulated data to json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40489700/

相关文章:

vis.js - 在vis-network中,层次布局中的水平顺序是如何确定的?

javascript - 检测容器,同时在其上移动元素

javascript - AngularFire 0.9 解决了 UI 路由器问题

css - Vis Network + Vuetify Tabs - 网络不会填满屏幕

javascript - 如何在jade中访问js数组的数据

javascript - 在 ReactJS 中的 VisJS 网络图的 Canvas 上添加多个节点框选择器

javascript - JQuery 中的 nextUntil 切换条件

javascript - 如何使用 javascript 通过上传 csv/json 文件来填充 parse.com 表

javascript - 修改 vis.js 网络中选定的特定节点的样式

javascript - 如何限制 vis.js 网络的缩放?