All versions of this documentation
X

Load graph

We initialize Ogma with a graph. The graph is generated with random nodes and edges.

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';

// Custom function that generates a graph.
function randomGraph(N, E) {
  var g = {nodes:[], edges:[]};

  for (var i = 0; i < N; i++) {
    g.nodes.push({
      id: 'n' + i,
      attributes: {
        x: Math.random() * 1000,
        y: Math.random() * 1000,
        text: 'n' + i,
        radius: 5 + 10 * Math.random()
      }
    });
  }

  for (var i = 0; i < E; i++) {
    g.edges.push({
      id: 'e' + i,
      source: 'n' + (Math.random() * N | 0),
      target: 'n' + (Math.random() * N | 0),
      attributes: { text: 'edge' + i}
    });
  }

  return g;
}

// Generate a random graph with the randomGraph function defined above.
var g = randomGraph(10, 10);

var ogma = new Ogma({
  graph: g, // load the graph at init
  container: 'graph-container'
});

// Alternatively:
//var ogma = new Ogma({
//  container: 'graph-container'
//});
// ogma.setGraph(g);
//// or:
// ogma.addNodes(g.nodes);
// ogma.addEdges(g.edges);

</script>
</body>
</html>