Weakly connected components
Click on a node to highlight its weakly-connected component.
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',
options: { interactions: { selection: { enabled: false }}}
});
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
ogma.generate.random({
nodes: 100,
edges: 50
})
.then(function(graph) { return ogma.setGraph(graph); })
.then(function () { return ogma.layouts.forceLink(); })
.then(function() { return ogma.view.locateGraph(); })
.then(function() {
ogma.getConnectedComponents().forEach(function(component) {
component.setAttribute('color', getRandomColor());
});
});
ogma.events.onClick(function(evt) {
ogma.clearSelection();
if (evt.target && evt.target.isNode) {
evt.target.getConnectedComponent().setSelected(true);
}
});
</script>
</body>
</html>