javascript 如何使用 sigma.js 显示来自 JSON 对象的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23273216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display data from a JSON object with sigma.js
提问by mnolde
I would like to read node/edges information for a graph (sigma.js) from a JSON variable. While it's quite easy to use an external JSON file as input via a JSON parser, I could not find a way to use a JSON object directly defined in the JavaScript code. There seems to exist a method called 'read', but I could not get it to work (Source: https://github.com/jacomyal/sigma.js/wiki/Graph-API). Could someone give me a hint?
我想从 JSON 变量中读取图 (sigma.js) 的节点/边信息。虽然通过 JSON 解析器使用外部 JSON 文件作为输入非常容易,但我找不到使用 JavaScript 代码中直接定义的 JSON 对象的方法。似乎存在一种名为“读取”的方法,但我无法让它工作(来源:https: //github.com/jacomyal/sigma.js/wiki/Graph-API)。有人可以给我一个提示吗?
This is what I would like to have:
这就是我想要的:
data = {
"nodes": [
{
"id": "n0",
"label": "A node",
"x": 0,
"y": 0,
"size": 3
},
{
"id": "n1",
"label": "Another node",
"x": 3,
"y": 1,
"size": 2
},
],
"edges": [
{
"id": "e0",
"source": "n0",
"target": "n1"
},
]
}
sigma.parsers.json( data , {
container: 'container',
settings: {
defaultNodeColor: '#ec5148'
}
});
Thank you, cheers, Michael
谢谢你,干杯,迈克尔
回答by commesan
Add your data as follows:
添加您的数据如下:
s = new sigma({
graph: data,
container: 'container',
settings: {
defaultNodeColor: '#ec5148'
}
});
See also: json-is-not-read-by-sigma-js