cytoscape.js 图的条件样式
Conditionnal styles for cytoscape.js graph
我想根据全局 Javascript 变量更改图表的样式。例如,假设我的边有 name
和 price
属性,我想根据全局 label_type
变量使边的标签不同:
let lable_type = 'I_want_name_labels'
switch(lable_type) {
case 'I_want_name_labels':
cy.style().selector('edge').style({'label': 'data(name)'});
break;
case 'I_want_price_labels':
cy.style().selector('edge').style({'label': 'data(price)'});
break;
}
上面的代码根本没有做任何事情(没有显示标签),我不太明白为什么。
我的边具有以下结构:
{
"data": {
"id": "node_node2",
"source": "node1",
"target": "node2",
"directed": true,
"name": "Baltazar",
"price": 1095.73
}
}
注意 :我尝试使用 cy.filter('edge').style({'label': 'data(name)'})
代替,但是 data
似乎无法通过这种方式访问,我收到了这个警告:
The style property `label: data(name)` is invalid
那么,如何使用 cytoscape.js 获得条件样式?我在这里错过了什么?
这是您要查找的行:
// .data() gets you all properties of the target element, .id() for example directly the id of the element
targetElement.style('label', targetElement.data('faveColor'));
这是一个关于如何初始化然后更改 nodes/edges 标签的工作演示:
var cy = (window.cy = cytoscape({
container: document.getElementById("cy"),
boxSelectionEnabled: false,
autounselectify: true,
style: [{
selector: "node",
css: {
"label": "data(id)",
"text-valign": "center",
"text-halign": "center",
"height": "60px",
"width": "100px",
"shape": "rectangle",
"background-color": "data(faveColor)"
}
},
{
selector: "edge",
css: {
"curve-style": "bezier",
"control-point-step-size": 40,
"target-arrow-shape": "triangle"
}
}
],
elements: {
nodes: [{
data: {
id: "Top",
faveColor: "#2763c4",
wants: "id"
}
},
{
data: {
id: "yes",
faveColor: "#37a32d",
wants: "id"
}
},
{
data: {
id: "no",
faveColor: "#2763c4",
wants: "id"
}
},
{
data: {
id: "Third",
faveColor: "#2763c4",
wants: "color"
}
},
{
data: {
id: "Fourth",
faveColor: "#56a9f7",
wants: "color"
}
}
],
edges: [{
data: {
source: "Top",
target: "yes"
}
},
{
data: {
source: "Top",
target: "no"
}
},
{
data: {
source: "no",
target: "Third"
}
},
{
data: {
source: "Third",
target: "Fourth"
}
},
{
data: {
source: "Fourth",
target: "Third"
}
}
]
},
layout: {
name: "dagre"
}
}));
cy.bind('click', 'node, edge', function(event) {
cy.nodes().each(function(ele, i, eles) {
ele.style('label', (ele.data('wants') == 'id') ? ele.data('id') : ele.data('faveColor'));
});
});
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 85%;
width: 100%;
float: right;
position: absolute;
}
<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>
<!-- cyposcape dagre -->
<script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
<script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>
<body>
<div id="cy"></div>
</body>
</html>
我想根据全局 Javascript 变量更改图表的样式。例如,假设我的边有 name
和 price
属性,我想根据全局 label_type
变量使边的标签不同:
let lable_type = 'I_want_name_labels'
switch(lable_type) {
case 'I_want_name_labels':
cy.style().selector('edge').style({'label': 'data(name)'});
break;
case 'I_want_price_labels':
cy.style().selector('edge').style({'label': 'data(price)'});
break;
}
上面的代码根本没有做任何事情(没有显示标签),我不太明白为什么。 我的边具有以下结构:
{
"data": {
"id": "node_node2",
"source": "node1",
"target": "node2",
"directed": true,
"name": "Baltazar",
"price": 1095.73
}
}
注意 :我尝试使用 cy.filter('edge').style({'label': 'data(name)'})
代替,但是 data
似乎无法通过这种方式访问,我收到了这个警告:
The style property `label: data(name)` is invalid
那么,如何使用 cytoscape.js 获得条件样式?我在这里错过了什么?
这是您要查找的行:
// .data() gets you all properties of the target element, .id() for example directly the id of the element
targetElement.style('label', targetElement.data('faveColor'));
这是一个关于如何初始化然后更改 nodes/edges 标签的工作演示:
var cy = (window.cy = cytoscape({
container: document.getElementById("cy"),
boxSelectionEnabled: false,
autounselectify: true,
style: [{
selector: "node",
css: {
"label": "data(id)",
"text-valign": "center",
"text-halign": "center",
"height": "60px",
"width": "100px",
"shape": "rectangle",
"background-color": "data(faveColor)"
}
},
{
selector: "edge",
css: {
"curve-style": "bezier",
"control-point-step-size": 40,
"target-arrow-shape": "triangle"
}
}
],
elements: {
nodes: [{
data: {
id: "Top",
faveColor: "#2763c4",
wants: "id"
}
},
{
data: {
id: "yes",
faveColor: "#37a32d",
wants: "id"
}
},
{
data: {
id: "no",
faveColor: "#2763c4",
wants: "id"
}
},
{
data: {
id: "Third",
faveColor: "#2763c4",
wants: "color"
}
},
{
data: {
id: "Fourth",
faveColor: "#56a9f7",
wants: "color"
}
}
],
edges: [{
data: {
source: "Top",
target: "yes"
}
},
{
data: {
source: "Top",
target: "no"
}
},
{
data: {
source: "no",
target: "Third"
}
},
{
data: {
source: "Third",
target: "Fourth"
}
},
{
data: {
source: "Fourth",
target: "Third"
}
}
]
},
layout: {
name: "dagre"
}
}));
cy.bind('click', 'node, edge', function(event) {
cy.nodes().each(function(ele, i, eles) {
ele.style('label', (ele.data('wants') == 'id') ? ele.data('id') : ele.data('faveColor'));
});
});
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 85%;
width: 100%;
float: right;
position: absolute;
}
<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>
<!-- cyposcape dagre -->
<script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
<script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>
<body>
<div id="cy"></div>
</body>
</html>