我怎样才能将一个Cytoscape网络保存为一个扁平的JSON文件?
cytoscape.js示例通常将底层网络数据存储为一个平面data.json文件(例如,colas图,也请参阅https://js.cytoscape.org/#notation/elements-json)。
这与将图形保存为Cytoscape文件时获得的分组或键控格式形成了鲜明对比(在Cytoscape:file > Export > Network to File > Cytoscape JSON (*.cyjs))中)。
备注: 文档表示我可以使用cy.json()导出图形的JSON表示,但我不知道如何调用这个命令(相对于javascript而言,我是个新手)。
发布于 2021-08-12 08:56:05
您只需调用.json()来获取一个json对象,然后按您的意愿下载它。
这里有一个例子。我认为StackOverflow正在阻止下载文件,但是如果您复制并粘贴这段代码,它应该是工作的。
document.addEventListener("DOMContentLoaded", function() {
var cy = (window.cy = cytoscape({
container: document.getElementById("cy"),
style: [{
selector: "node",
style: {
content: "data(id)"
}
},
{
selector: "edge",
style: {
"curve-style": "bezier",
"target-arrow-shape": "triangle"
}
}
],
elements: {
nodes: [{
data: {
id: "a"
}
}, {
data: {
id: "b"
}
}],
edges: [{
data: {
id: "ab",
source: "a",
target: "b"
}
}]
},
layout: {
name: "grid"
}
}));
cy.ready(function() {
const json = cy.json(); // take json from cytoscape
// download json as you want
const data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json));
const a = document.createElement('a');
a.href = 'data:' + data;
a.download = 'data.json';
a.innerHTML = 'download JSON';
const container = document.getElementById('container');
container.appendChild(a);
});
});body {
font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
font-size: 14px
}
#cy {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
z-index: 1;
}
h1 {
opacity: 0.5;
font-size: 1em;
font-weight: bold;
}<head>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
</head>
<body>
<div id="cy"></div>
<div id="container" style="position:absolute;top:10;left:10;z-index:1"></div>
</body>
https://stackoverflow.com/questions/68230498
复制相似问题