我想使用 Java EE 应用程序服务器端生成的数据显示图形。所以我的问题是如何将一些数据结构(JSONObject)放入以下代码的“元素”部分:
$(function(){ // on dom ready
var cy = cytoscape({
container: document.getElementById('cy'),
style: cytoscape.stylesheet()
.selector('node')
.css({
'content': 'data(id)'
})
.selector('edge')
.css({
'target-arrow-shape': 'triangle',
'width': 4,
'line-color': '#ddd',
'target-arrow-color': '#ddd'
})
.selector('.highlighted')
.css({
'background-color': '#61bffc',
'line-color': '#61bffc',
'target-arrow-color': '#61bffc',
'transition-property': 'background-color, line-color, target-arrow-color',
'transition-duration': '0.5s'
}),
elements: {
nodes: [
{ data: { id: 'a' } },
{ data: { id: 'b' } },
{ data: { id: 'c' } },
{ data: { id: 'd' } },
{ data: { id: 'e' } }
],
edges: [
{ data: { id: 'a"e', weight: 1, source: 'a', target: 'e' } },
{ data: { id: 'ab', weight: 3, source: 'a', target: 'b' } },
{ data: { id: 'be', weight: 4, source: 'b', target: 'e' } },
{ data: { id: 'bc', weight: 5, source: 'b', target: 'c' } },
{ data: { id: 'ce', weight: 6, source: 'c', target: 'e' } },
{ data: { id: 'cd', weight: 2, source: 'c', target: 'd' } },
{ data: { id: 'de', weight: 7, source: 'd', target: 'e' } }
]
},
layout: {
name: 'breadthfirst',
directed: true,
roots: '#a',
padding: 10 }});
完整示例可以在 here 找到。我不介意在页面上使用具有图形逻辑的 scriptlet 来调用一些 java 代码,这将获得我所需的数据。那么如何将结果放在“元素”下以实现动态生成图(节点和边)呢?
最佳答案
这是一个带有右大括号的简单错误。变化:
layout: { name: 'breadthfirst', directed: true, roots: '#a', padding: 10 }});
至:
layout: { name: 'breadthfirst', directed: true, roots: '#a', padding: 10 }}); });
即在末尾添加});,图表就会显示出来。
关于java - Cytoscape.js - 如何显示 Java 中生成的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26432369/