我想在某个事件之后更改一些特定边的标签。 最好的方法是什么?
我需要去除这些边缘,更改标签标签,然后再次将其添加到cytoscape吗?
最好的方法是“刷新图”。
谢谢!
答案 0 :(得分:2)
这是编辑任何cy元素标签的简单方法:
// unbind first to prevent issues with binding conflicts
cy.unbind('click');
// change label of node to new text
cy.bind('click', 'node', function (evt) {
var target = evt.target;
target.data('label', 'new node label');
});
// change label of node to new text
cy.bind('click', 'edge', function (evt) {
var target = evt.target;
target.data('label', 'new edge label');
});
这是一个工作示范:
var cy = (window.cy = cytoscape({
container: document.getElementById("cy"),
boxSelectionEnabled: false,
autounselectify: true,
style: [{
selector: "node",
css: {
"label": "data(label)",
"text-valign": "center",
"text-halign": "center",
"height": "60px",
"width": "60px"
}
},
{
selector: "edge",
css: {
"target-arrow-shape": "triangle"
}
},
{
selector: "edge[label]",
css: {
label: "data(label)",
"text-rotation": "autorotate",
"text-margin-x": "0px",
"text-margin-y": "0px"
}
}
],
elements: {
nodes: [{
data: {
id: "Peter",
label: "Peter"
}
},
{
data: {
id: "Claire",
label: "Claire"
}
},
{
data: {
id: "Mike",
label: "Mike"
}
},
{
data: {
id: "Rosa",
label: "Rosa"
}
}
],
edges: [{
data: {
source: "Peter",
target: "Claire",
label: "edge 01"
}
},
{
data: {
source: "Claire",
target: "Mike",
label: "edge 02"
}
},
{
data: {
source: "Mike",
target: "Rosa",
label: "edge 03"
}
},
{
data: {
source: "Rosa",
target: "Peter",
label: "edge 04"
}
}
]
},
layout: {
name: "circle"
}
}));
// This is the important part
cy.unbind('click');
cy.bind('click', 'node', function(evt) {
var target = evt.target;
target.data('label', 'new node label');
});
cy.bind('click', 'edge', function(evt) {
var target = evt.target;
target.data('label', 'new edge label');
});
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 100%;
width: 75%;
position: absolute;
left: 0;
top: 0;
float: left;
}
<html>
<head>
<meta charset=utf-8 />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
<script src="https://unpkg.com/cytoscape@3.3.0/dist/cytoscape.min.js"></script>
</head>
<body>
<div id="cy"></div>
</body>
</html>
现在您要做的就是调用edges数组上的.data()函数(一个接一个)。