Just got a solution for those who have a similar desire.
adapted from the solution [here][1], thanks Stephan!
left click set neighbors visible, right-click set node invisible.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
boxSelectionEnabled: false,
autounselectify: true,
style: [{
selector: 'node',
css: {
'content': 'data(id)',
'text-valign': 'center',
'text-halign': 'center',
'height': '60px',
'width': '60px',
'border-color': 'black',
'border-opacity': '1',
'border-width': '10px',
'shape': 'data(shape)'
}
},
{
selector: '$node > node',
css: {
'padding-top': '10px',
'padding-left': '10px',
'padding-bottom': '10px',
'padding-right': '10px',
'text-valign': 'top',
'text-halign': 'center',
'background-color': '#bbb'
}
},
{
selector: 'edge',
css: {
'target-arrow-shape': 'triangle'
}
},
{
selector: ':selected',
css: {
'background-color': 'black',
'line-color': 'black',
'target-arrow-color': 'black',
'source-arrow-color': 'black'
}
},
{
selector: '.hidden',
css: {
'display': 'none'
}
}
],
elements: {
nodes: [{
data: {
id: 'n0',
shape: 'ellipse'
},
},
{
data: {
id: 'n1',
shape: 'ellipse'
},
},
{
data: {
id: 'n2',
shape: 'ellipse'
},
},
{
data: {
id: 'n3',
shape: 'ellipse'
},
},
{
data: {
id: 'n4',
shape: 'ellipse'
},
},
{
data: {
id: 'n5',
shape: 'ellipse'
},
},
{
data: {
id: 'n6',
shape: 'rectangle'
},
},
{
data: {
id: 'n7',
shape: 'ellipse'
},
},
{
data: {
id: 'n8',
shape: 'rectangle'
},
},
{
data: {
id: 'n9',
shape: 'ellipse'
},
},
{
data: {
id: 'n10',
shape: 'ellipse'
},
},
{
data: {
id: 'n11',
shape: 'ellipse'
},
},
{
data: {
id: 'n12',
shape: 'ellipse'
},
},
{
data: {
id: 'n13',
shape: 'rectangle'
},
},
{
data: {
id: 'n14',
shape: 'ellipse'
},
},
{
data: {
id: 'n15',
shape: 'rectangle'
},
},
{
data: {
id: 'n16',
shape: 'ellipse'
},
}
],
edges: [{
data: {
source: 'n0',
target: 'n1'
},
},
{
data: {
source: 'n1',
target: 'n2'
},
},
{
data: {
source: 'n1',
target: 'n3'
},
},
{
data: {
source: 'n2',
target: 'n7'
},
},
{
data: {
source: 'n2',
target: 'n11'
},
},
{
data: {
source: 'n2',
target: 'n16'
},
},
{
data: {
source: 'n3',
target: 'n4'
},
},
{
data: {
source: 'n3',
target: 'n16'
},
},
{
data: {
source: 'n4',
target: 'n5'
},
},
{
data: {
source: 'n4',
target: 'n6'
},
},
{
data: {
source: 'n6',
target: 'n8'
},
},
{
data: {
source: 'n8',
target: 'n9'
},
},
{
data: {
source: 'n8',
target: 'n10'
},
},
{
data: {
source: 'n11',
target: 'n12'
},
},
{
data: {
source: 'n12',
target: 'n13'
},
},
{
data: {
source: 'n13',
target: 'n14'
},
},
{
data: {
source: 'n13',
target: 'n15'
},
},
]
},
layout: {
name: 'dagre',
padding: 5
}
});
cy.nodes('[id != "n0"]').classes('hidden');
cy.nodes().on('click', function () {
this.outgoers().each(function(ele) {
if (ele.hidden()) {
}
ele.classes("");
})
;
})
cy.nodes().on('cxttap', function () {
this.classes('hidden')});
<!-- language: lang-css -->
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 100%;
width: 75%;
position: absolute;
left: 0;
top: 0;
float: left;
}
<!-- language: lang-html -->
<html>
<head>
<link href="style.css" rel="stylesheet" />
<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://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.17/cytoscape.min.js"></script>
<script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
<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>
<script src="code.js"></script>
</body>
</html>
<!-- end snippet -->
[1]: https://stackoverflow.com/questions/65917054/in-cytoscape-js-how-do-i-select-the-set-of-visible-nodes-that-have-no-visible-e
Your approach is good, it just needs some adjustments for your desired result. I'd add a filter for every non ellipse node and remove the nodes with no visible connected edges. I use the function `.hidden()` for that, it returns true if every edge is hidden and false if there is at least one visible edge:
```
cy.nodes('[shape="ellipse"]').addClass("hidden");
cy.nodes('[shape!="ellipse"]').each(function(node) {
if (node.connectedEdges().hidden()) {
node.addClass("hidden");
}
});
```
This works because of the style I added. Nodes with a display value of none hide their edges automatically, so there is no need to hide edges manually:
```
{
selector: '.hidden',
css: {
'display': 'none'
}
}
```
I tried this out in this dagre graph within a click event listener. I also changed the addClass to toggleClass, you can remove and add the classes with repeated clicks that way:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
boxSelectionEnabled: false,
autounselectify: true,
style: [{
selector: 'node',
css: {
'content': 'data(id)',
'text-valign': 'center',
'text-halign': 'center',
'height': '60px',
'width': '60px',
'border-color': 'black',
'border-opacity': '1',
'border-width': '10px',
'shape': 'data(shape)'
}
},
{
selector: '$node > node',
css: {
'padding-top': '10px',
'padding-left': '10px',
'padding-bottom': '10px',
'padding-right': '10px',
'text-valign': 'top',
'text-halign': 'center',
'background-color': '#bbb'
}
},
{
selector: 'edge',
css: {
'target-arrow-shape': 'triangle'
}
},
{
selector: ':selected',
css: {
'background-color': 'black',
'line-color': 'black',
'target-arrow-color': 'black',
'source-arrow-color': 'black'
}
},
{
selector: '.hidden',
css: {
'display': 'none'
}
}
],
elements: {
nodes: [{
data: {
id: 'n0',
shape: 'ellipse'
}
},
{
data: {
id: 'n1',
shape: 'ellipse'
}
},
{
data: {
id: 'n2',
shape: 'ellipse'
}
},
{
data: {
id: 'n3',
shape: 'ellipse'
}
},
{
data: {
id: 'n4',
shape: 'ellipse'
}
},
{
data: {
id: 'n5',
shape: 'ellipse'
}
},
{
data: {
id: 'n6',
shape: 'rectangle'
}
},
{
data: {
id: 'n7',
shape: 'ellipse'
}
},
{
data: {
id: 'n8',
shape: 'rectangle'
}
},
{
data: {
id: 'n9',
shape: 'ellipse'
}
},
{
data: {
id: 'n10',
shape: 'ellipse'
}
},
{
data: {
id: 'n11',
shape: 'ellipse'
}
},
{
data: {
id: 'n12',
shape: 'ellipse'
}
},
{
data: {
id: 'n13',
shape: 'rectangle'
}
},
{
data: {
id: 'n14',
shape: 'ellipse'
}
},
{
data: {
id: 'n15',
shape: 'rectangle'
}
},
{
data: {
id: 'n16',
shape: 'ellipse'
}
}
],
edges: [{
data: {
source: 'n0',
target: 'n1'
}
},
{
data: {
source: 'n1',
target: 'n2'
}
},
{
data: {
source: 'n1',
target: 'n3'
}
},
{
data: {
source: 'n2',
target: 'n7'
}
},
{
data: {
source: 'n2',
target: 'n11'
}
},
{
data: {
source: 'n2',
target: 'n16'
}
},
{
data: {
source: 'n3',
target: 'n4'
}
},
{
data: {
source: 'n3',
target: 'n16'
}
},
{
data: {
source: 'n4',
target: 'n5'
}
},
{
data: {
source: 'n4',
target: 'n6'
}
},
{
data: {
source: 'n6',
target: 'n8'
}
},
{
data: {
source: 'n8',
target: 'n9'
}
},
{
data: {
source: 'n8',
target: 'n10'
}
},
{
data: {
source: 'n11',
target: 'n12'
}
},
{
data: {
source: 'n12',
target: 'n13'
}
},
{
data: {
source: 'n13',
target: 'n14'
}
},
{
data: {
source: 'n13',
target: 'n15'
}
},
]
},
layout: {
name: 'dagre',
padding: 5
}
});
cy.unbind("click");
cy.bind("click", function(event) {
cy.nodes('[shape!="ellipse"]').toggleClass("hidden");
cy.nodes('[shape="ellipse"]').each(function(node) {
if (node.connectedEdges().hidden()) {
node.toggleClass("hidden");
}
});
});
<!-- language: lang-css -->
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 100%;
width: 75%;
position: absolute;
left: 0;
top: 0;
float: left;
}
<!-- language: lang-html -->
<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://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.17/cytoscape.min.js"></script>
<script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
<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>
<!-- end snippet -->