Neighborhood highlight
Hover a node with the mouse. Its direct node neighbors become bigger and red, and neighbors or neighbors become orange.
Open in a new window.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../build/ogma.min.js"></script>
<style>
#graph-container { top: 0; bottom: 0; left: 0; right: 0; position: absolute; margin: 0; overflow: hidden; }
</style>
</head>
<body>
<div id="graph-container"></div>
<script>
'use strict';
var ogma = new Ogma({
container: 'graph-container'
});
ogma.createClass('sourceNode');
// Create a class for 1st step neighbors
ogma.createClass('neighbor1', {
nodeAttributes: {
color: 'red',
radius: 7,
icon: {
color: 'white',
content: "1",
}
},
edgeAttributes: {
color: 'red'
}
});
// Create a class for 1st step neighbors
ogma.createClass('neighbor2', {
nodeAttributes: {
color: 'orange',
radius: 7,
icon: {
color: 'white',
content: "2",
}
},
edgeAttributes: {
color: 'orange'
}
});
ogma.generate.erdosRenyi({nodes:20, p:0.1}).then(function(graph) {
ogma.setGraph(graph);
ogma.view.locateGraph();
});
ogma.events.onHover(function (ref) {
if (!ref.target.isNode) return;
var node = ref.target;
node.addClass('sourceNode');
node.getAdjacentNodes().addClass('neighbor1');
node.getAdjacentEdges().addClass('neighbor1');
node.getAdjacentNodes().getAdjacentNodes().filter(function (n) { return n !== node; }).addClass('neighbor2');
node.getAdjacentNodes().getAdjacentEdges().addClass('neighbor2');
console.log('nodes with a distance 1 ', ogma.getNodesByClassName('neighbor1'));
});
ogma.events.onUnhover(function (ref) {
if (!ref.target.isNode) return;
var node = ref.target;
node.removeClass('sourceNode');
node.getAdjacentNodes().removeClass('neighbor1');
node.getAdjacentEdges().removeClass('neighbor1');
// recreate the traversal to remove the class
node.getAdjacentNodes().getAdjacentNodes().removeClass('neighbor2');
node.getAdjacentNodes().getAdjacentEdges().removeClass('neighbor2');
// We can also remove the class to all nodes who belong to it
// ogma.getNodesByClassName('neighbor2').removeClass('neighbor2');
// ogma.getEdgesByClassName('neighbor2').removeClass('neighbor2');
});
</script>
</body>
</html>