Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Highlight Neighbors Example
* ============================
*
* @Yomguithereal
*
* Highlighting neighbors of the clicked node using a rendering engine such
* as sigma.
*/
import Graph from 'graph';
import sigma from 'sigma';
// Creating our graph (we'll assume it's hydrated with external data)
const graph = new Graph(data);
// Instantiating sigma
const sigInst = new sigma(graph, {
settings: {
edgeColor: 'source'
}
});
// Click handler
function clickHandler({data: {node}}) {
// Creating a set of the node & its neighbors
const nodesToHighlight = new Set([node].concat(graph.neighbors(node)));
// Iterating through nodes
graph.forEachNode(node => {
const color = nodesToHighlight.has(node) ?
graph.getNodeAttribute(node, 'originalColor') :
'#ccc';
this.renderer = {
container: 'sigma-canvas',
type: 'canvas',
};
this.settings = {
sideMargin: 1,
singleHover: true,
minNodeSize: 0.5,
maxNodeSize: 16,
drawLabels: false,
defaultEdgeType: 'arrow',
};
this.sigma = new Sigma({
renderer: this.renderer,
settings: this.settings,
});
function NodeSelect(sigma) {
this.sigma = sigma;
this.color = '#5bc0de';
this.add = function (event) {
this.remove(event);
this.node = event.data.node;
this.prevColor = this.node.color;
this.node.color = this.color;
this.sigma.refresh();
};