All versions of this documentation
X

Color

The node color is controlled through the color attribute. Multiple color formats are available: hexadecimal, RGB(A), and CSS color names.

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

var COLORS = ['#161344','#3f1c4c','#632654','#86315b','#a93c63','#cd476a','#f35371'];

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

// Create a rule that assigns a color to each node using a function.
// This function must be deterministic as it will be called each time the node is painted.
ogma.styles.addNodeRule({
  color: function (n) { return COLORS[n.getId() % COLORS.length]; }
});

// Generate a random graph
ogma.generate.random( { nodes: 50, edges: 0 } )
  .then ( function (graph) {
    // Assign this graph to Ogma.
    ogma.setGraph(graph);
    ogma.view.locateGraph();

    // Color the node '0' in orange.
    ogma.getNode('0').setAttributes({color: 'orange'});

    // Color the nodes who have a coordinate x above 90 in purple.
    ogma.getNodes().filter(function (n) {
      return (n.getAttribute('x') > 90);
    }).setAttributes({
      color: 'purple'
    });
  });

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