我对 JavaScript 和 cytoscape.js 库都很陌生。我一直在玩示例代码,但我被一些基本的东西困住了。
本质上,我想要做的是设置我的基本选项和样式,然后能够从我的服务器返回的 JSON 结构中加载我的节点和边。我知道我可以在“就绪”函数中使用 cy.load,如下所示:
$(loadCy = function(){
options = {
showOverlay: false,
minZoom: 0.5,
maxZoom: 2,
style: cytoscape.stylesheet()
.selector('node')
.css({
'content': 'data(name)',
'font-family': 'helvetica',
'font-size': 14,
'text-outline-width': 3,
'text-outline-color': '#888',
'text-valign': 'center',
'color': '#fff',
'width': 'mapData(weight, 30, 80, 20, 50)',
'height': 'mapData(height, 0, 200, 10, 45)',
'border-color': '#fff'
})
.selector(':selected')
.css({
'background-color': '#000',
'line-color': '#000',
'target-arrow-color': '#000',
'text-outline-color': '#000'
})
.selector('edge')
.css({
'width': 2,
'target-arrow-shape': 'triangle'
})
,
ready: function(){
cy = this;
cy.load({
nodes: [
{ data: { id: "n1" } },
{ data: { id: "n2" } }
],
edges: [
{ data: { id: "e1", source: "n1", target: "n2" } }
]
})
}
};
$('#cy').cytoscape(options);
});
但我想知道是否有一种方法可以在文档上准备好其他所有设置,然后稍后,例如在按钮上单击访问 cy.load?
例如:
<script>
$("#button").click( function()
{
cy.load(<data returned from server>)
}
</script>
这是HTML代码。本质上,Cytoscape 在此 DIV 中绘制图形:
<body>
<div id="cy"></div>
</body>