All versions of this documentation
X

Count nodes and edges

Get the number of nodes and edges in the graph.

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; }
    .info {
      position: absolute;
      color: #fff;
      background: #141229;
      font-size: 12px;
      font-family: monospace;
      padding: 5px;
    }
    .info.n { top: 0; left: 0; }
    .info.e { top: 20px; left: 0; }
  </style>
</head>
<body>
  <div id="graph-container"></div>
  <div id="info_nodes" class="info n"></div>
  <div id="info_edges" class="info e"></div>

<script>
'use strict';

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

// Generate a random graph
ogma.generate.random({
  nodes: 7,
  edges: 10
}).then(function(g) {
    // Assign the graph to Ogma.
    ogma.setGraph(g);
    ogma.view.locateGraph();

    // Retrieve the number of nodes and edges.
    var nodes = ogma.getNodes();
    var edges = ogma.getEdges();

    // Update the html code.
    document.getElementById('info_nodes').textContent = 'Nodes: ' + nodes.size;
    document.getElementById('info_edges').textContent = 'Edges: ' + edges.size;
});

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