Skip to content
  1. Examples

Concentric

This example shows how to use the concentric layout to position nodes according to their distance from a given node.

js
import Ogma from '@linkurious/ogma';

const ogma = new Ogma({
  container: 'graph-container'
});

ogma.generate
  .barabasiAlbert({
    nodes: 100,
    m0: 10,
    m: 1
  })
  .then(graph => {
    ogma.setGraph(graph);

    // Set node size to order them by size.
    let i = 2;
    ogma.getNodes().forEach(node => {
      node.setAttributes({ radius: i });
      // node.size = i;
      i += 0.1;
    });

    ogma.layouts
      .concentric({
        centralNode: 0,
        allowOverlap: false,
        sortBy: 'radius' // or any node property
      })
      .then(() => {
        console.log('done');
        ogma.view.locateGraph({
          easing: 'linear',
          duration: 300
        });
      });
  });
html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />

    <link type="text/css" rel="stylesheet" href="styles.css">
  </head>
  <body>
    <div id="graph-container"></div>
    <script src="index.js"></script>
  </body>
</html>
css
#graph-container {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
  margin: 0;
  overflow: hidden;
}