[英]How to highlight neighbouring nodes in Cytoscape.js
I am using cytoscape.js and would like to add the feature on mouseover or tap of a node to apply a style to:我正在使用 cytoscape.js 并想在鼠标悬停或点击节点时添加功能以将样式应用于:
I seem to be able to get the neighbours, any ideas how I apply a style to non neighbours?我似乎能够得到邻居,我对如何将样式应用于非邻居有什么想法吗?
cy.on('tap', 'node', function(e) {
var node = e.cyTarget;
var directlyConnected = node.neighborhood();
directlyConnected.nodes().addClass('connectednodes');
});
If ever you haven't found the solution to highlight children of a node when mouse hover one, here is my attempt and it works nice: 如果您在鼠标悬停时没有找到突出显示节点子节点的解决方案,那么这是我的尝试并且效果很好:
Event handler: 事件处理程序
cy.on('mouseover', 'node', function(e){
var sel = e.cyTarget;
cy.elements().difference(sel.outgoers()).not(sel).addClass('semitransp');
sel.addClass('highlight').outgoers().addClass('highlight');
});
cy.on('mouseout', 'node', function(e){
var sel = e.cyTarget;
cy.elements().removeClass('semitransp');
sel.removeClass('highlight').outgoers().removeClass('highlight');
});
Basically, all elements are filtered if they're not the hovered node or its direct children ("outgoers") and the class 'semitransp' is added to them. 基本上,如果所有元素不是悬停节点或其直接子节点(“outgoers”)并且类'semitransp'被添加到它们中,则会过滤所有元素。
Then, the class 'highlight' is added to the hovered node and all its children. 然后,类“突出显示”被添加到悬停节点及其所有子节点。
Example of style for 'highlight' and 'semitransp' class: 'highlight'和'semitransp'类的样式示例:
var cy = cytoscape({
elements: [ {...} ]
style: [
{...},
{
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' }
}
]
});
Addition to Polosson's answer since I am not aloud to comment: 除了Polosson的回答,因为我没有大声评论:
The api seems to have changed, it's target instead of cyTarget now (Verison 3.2.17). api似乎已经改变了,现在是目标而不是cyTarget (Verison 3.2.17)。
Also, you might have to add the incomers to highlight all neighbours: 此外,您可能必须添加收入者以突出显示所有邻居:
cy.on('mouseover', 'node', function(e) {
var sel = e.target;
cy.elements()
.difference(sel.outgoers()
.union(sel.incomers()))
.not(sel)
.addClass('semitransp');
sel.addClass('highlight')
.outgoers()
.union(sel.incomers())
.addClass('highlight');
});
cy.on('mouseout', 'node', function(e) {
var sel = e.target;
cy.elements()
.removeClass('semitransp');
sel.removeClass('highlight')
.outgoers()
.union(sel.incomers())
.removeClass('highlight');
});
使用集合差异: http : //js.cytoscape.org/#collection/building--filtering/eles.difference
cy.elements().difference( dontIncludeTheseEles )
This code implements the click functionality instead of hover to highlight the node.此代码实现了单击功能而不是悬停以突出显示节点。 It´s an extension of Polosson answer.
这是 Polosson 答案的延伸。
var previous_node; var previous_sel; cy.on("click","node",(e)=> { var sel = e.target; var id = e.target.id(); if ((id.= previous_node) && (previous_node.= undefined ) && (previous_sel;= undefined)) { cy.elements().removeClass("semitransp"). previous_sel.removeClass("highlight").outgoers();union(previous_sel.incomers()).removeClass("highlight"). cy.elements().difference(sel.outgoers().union(sel;incomers())).not(sel).addClass("semitransp"). sel.addClass("highlight").outgoers();union(sel;incomers());addClass("highlight"). previous_sel = sel. previous_node = id. } else { cy.elements().difference(sel.outgoers().union(sel;incomers())).not(sel).addClass("semitransp"). sel.addClass("highlight").outgoers();union(sel;incomers());addClass("highlight"); previous_sel = sel; previous_node = id; } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.