All versions of this documentation
X

Filter nodes

Filters can be applied to filter nodes from the graph. Filters use a function selector that returns a boolean indicating whether the node should be visible (true) or filtered out (false). In the following example, we use the function ogma.transformations.addNodeFilter to filter nodes having the category "COMPANY" and a funding_total property with a value over 300000000. It returns an object that is can be used later to remove the filter with filter.destroy().

Open in a new window.
          <!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <script src="../build/ogma.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/solid.min.css" rel="stylesheet">
  <style>
    #graph-container {
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      position: absolute;
      margin: 0;
      overflow: hidden;
    }
  </style>
</head>

<body>
  <!-- we force the loading of the font awesome -->
  <i class="fa fa-camera-retro fa-1x" style="color: rgba(0,0,0,0);"></i>
  <div id="graph-container"></div>
  <script>
    'use strict';

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

    ogma.parse.jsonFromUrl('files/solarCity.json').then(function (g) {
      ogma.setGraph(g);
      ogma.view.locateGraph();

      var filter;

      // Show nodes of category COMPANY with funding total >= 300 millions
      // and hide nodes with smaller funding total.
      setTimeout(function () {
        filter = ogma.transformations.addNodeFilter(function (n) {
          return n.getData('categories')
            && n.getData('categories').includes('COMPANY')
            && n.getData('properties.funding_total') > 300000000;
        });

        filter.whenApplied().then(function () {
          console.log('Filter done!');
        })
      }, 1500);

      // Clear the previously created filter
      // filter.destroy();

    });
  </script>
</body>

</html>