我希望在点击节点后突出显示传出边缘。现在,我有这样的代码:
style: cytoscape.stylesheet()
.selector('node')
.css({
'content': 'data(id)',
'background-color': '#4286f4'
})
.selector('edge.highlighted')
.css({
'line-color': 'black',
'target-arrow-color': '#b830f7'
})
.selector('edge')
.css({
'curve-style': 'bezier',
'target-arrow-shape': 'triangle',
'width': 4,
'line-color': '#4286f4',
'target-arrow-color': '#4286f4'
}),
//else parameters
});
cy.on('click', function(e){
var edges = cy.edges();
edges.removeClass('highlighted');
});
cy.on('click', 'node', function(e){
var id = e.target.id();
var outgoing = cy.edges("[source='" + id + "']")
outgoing.addClass('highlighted');
});
当“边缘”选择器未设置线条颜色时,它可以正常工作,但如果我将一些颜色设置为边缘 - 单击节点后不会应用新颜色。
答案 0 :(得分:1)
style: cytoscape.stylesheet()
.selector('node')
.css({
'content': 'data(id)',
'background-color': '#4286f4'
})
.selector('edge.highlighted')
.css({
'line-color': 'black',
'target-arrow-color': '#b830f7'
})
.selector('edge')
.css({
'curve-style': 'bezier',
'target-arrow-shape': 'triangle',
'width': 4,
'line-color': '#4286f4',
'target-arrow-color': '#4286f4'
}),
});
cy.on('click', function(e){
if (e.target === cy || e.target.group() == "edges") {
cy.edges().removeClass('highlighted');
}
else {
cy.edges("[source='" + e.target.id() + "']").addClass('highlighted');
}
});
我认为这应该可以正常工作,你同时调用两个点击方法,这可能会导致一些问题,请告诉我是否修复了这个问题:)。
如果您想要有两个单独的点击事件,可以写下:
cy.on('click', function(e){
if (e.target === cy || e.target.group() == "edges") {
cy.edges().removeClass('highlighted');
});
cy.on('click', 'node', function(e){
cy.edges("[source='" + e.target.id() + "']").addClass('highlighted');
});
答案 1 :(得分:0)
可以为线颜色添加特殊选择器:
.selector('edge.lines')
.css({
'line-color': '#4286f4',
'target-arrow-color': '#4286f4'
})
然后使用cy.edges()。classes('highlight')用突出显示的类替换edge'类,或者使用cy.edges()。classes('lines')将类设置为'lines'
答案 2 :(得分:0)
我在工作中添加了一些对我解决问题的代码至关重要的东西:
// Initialize cytoscape
cy = window.cy = cytoscape({
container: $('.cy'),
boxSelectionEnabled: false,
autounselectify: true,
layout: {
name: 'grid'
},
style: [
{
selector: 'node',
style: {
'shape': 'data(faveShape)',
'content': 'data(DisplayName)',
'height': 'data(faveHeight)',
'width': 'data(faveWidth)',
'background-color': 'data(faveColor)',
'line-color': '#a8eae5',
'font-family': 'Segoe UI,Helvetica Neue,Helvetica,Arial,Verdana',
'font-size': '15px',
}
},
{
selector: 'edge',
style: {
'label': 'data(myLabel)',
'curve-style': 'bezier',
'width': 5,
'opacity': 0.5,
'line-color': '#a8eae5',
'font-size': '12px',
'target-arrow-shape': 'triangle',
'target-arrow-color': '#a8eae5'
}
},
{
selector: '.autorotate',
style: {
'edge-text-rotation': 'autorotate'
}
},
{
selector: ".center-center",
style: {
"text-valign": "center",
"text-halign": "center"
}
},
{
selector: 'edge.highlighted',
style: {
'line-color': '#2a6cd6',
'target-arrow-color': '#2a6cd6',
'opacity': 0.7,
}
},
{
selector: 'edge.deactivate',
style: {
'opacity': 0.1,
}
},
{
selector: 'node.deactivated',
style: {
'opacity': 0.1,
}
}
],
});
// After some other functions where I select the nodes I want to display I
// empty the graph and then:
cy.add(jsonNew);
layout = cy.elements().layout({
name: 'concentric'
}).run();
cy.fit(cy.elements());
cy.center();;
cy.unbind('click');
cy.unbind('tapstart');
cy.unbind('tapend');
cy.bind('click ', 'node', function (evt) {
onTap(evt);
});
cy.bind('tapstart ', 'node', function (evt) {
cy.edges("[source = '" + evt.target.id()+"']").addClass('highlighted');
cy.edges("[source !='" + evt.target.id()+"']").addClass('deactivate');
// Here is a complicated algorithm to get all nodes not connected to the
// node which is currently held, I get all these nodes and then make
// them almost invisible. When the node is released I set remove the
// node.deactivated class from all nodes
});
cy.bind('tapend ', 'node', function (evt) {
cy.edges("[source ='"+evt.target.id()+"']").removeClass('highlighted');
cy.edges("[source !='"+evt.target.id()+"']").removeClass('deactivate');
});
cy.on()是绑定操作的同义词,因此这会导致很多错误,即使你的问题仍然存在,你必须取消绑定以前的绑定,否则代码执行两次或更多。
以下是cytoscape的整个初始化:
import csv
fin = open("File1.txt","r")
# skip the first line
next(fin)
amount_tx = {}
for line in fin:
# make the line into a list of the form ['x', 'y', 'z', 'a']
line = line.rstrip()
f = line.split("\t")
g = f[0].split()
# get the two variables necessary
txid = g[0]
amount = int(g[3])
# add to dictionary if not yet present
if txid not in amount_tx:
amount_tx[txid] = 0
amount_tx[txid] += amount
fin.close()
for txid in amount_tx:
print("{0}\t{1:d}\n".format(txid, amount_tx[txid]))
答案 3 :(得分:0)
您可以突出显示选定的节点和所有传出的边,并将节点连接到选定的节点
cy.on('tap', 'node', function(evt) {
const target: any = evt.target;
const node = target[0]._private.data;
console.log( 'tapped ' , node.name);
cy.elements().difference(target.outgoers()).not(target).addClass('semitransp');
target.addClass('highlight').outgoers().addClass('highlight');
});
您可以通过执行以下操作,通过单击cy(在图形外部)来取消选择所有节点和边线
cy.on('click',function(evt){
//select either edges or nodes to remove the styles
//var edges = cy.edges();
//var nodes = cy.nodes()
// edges.removeClass('semitransp');
// nodes.removeClass('semitransp');
//you can select all elements and remove the styles
cy.elements().removeClass('semitransp');
})
样式/ css详细信息供您参考
{
selector: 'node.highlight',
style: {
'border-color': '#FFF',
'border-width': '2px'
}
},
{
selector: 'node.semitransp',
style: { 'opacity': '0.5' }
},
{
selector: 'edge.highlight',
style: { 'mid-target-arrow-color': '#FFF' }
},
{
selector: 'edge.semitransp',
style: { 'opacity': '0.2' }
}