Skip to content
  1. Examples

Background

This example shows how to use the backgroundColor attribute on nodes and edges texts.

ts
import Ogma from '@linkurious/ogma';

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

ogma.styles.addNodeRule({
  radius: 3,
  color: '#1499ff',
  text: {
    content: n => 'Node ' + n.getId(),
    color: '#000',
    backgroundColor: 'inherit', // values: named, hex, rgb, rgba, 'inherit' (will inherit node color)
    padding: 3,
    minVisibleSize: 5,
    margin: 20
  }
});

ogma.styles.setHoveredNodeAttributes({
  text: {
    color: '#fff',
    backgroundColor: '#000'
  }
});

ogma.styles.setSelectedNodeAttributes({
  text: {
    color: '#fff',
    backgroundColor: '#000'
  }
});

ogma.styles.addEdgeRule({
  color: '#ccc',
  text: {
    content: e => 'Edge ' + e.getId(),
    color: '#fff',
    backgroundColor: 'inherit', // values: named, hex, rgb, rgba, 'inherit' (will inherit node color)
    minVisibleSize: 3,
    padding: 2
  }
});

ogma.styles.setHoveredEdgeAttributes({
  text: {
    color: '#fff',
    backgroundColor: '#000'
  }
});

ogma.styles.setSelectedEdgeAttributes({
  text: {
    color: '#fff',
    backgroundColor: '#000'
  }
});

const graph = await ogma.generate.random({ nodes: 6, edges: 6 });
await ogma.setGraph(graph);
await ogma.view.locateGraph();
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 type="module" src="index.ts"></script>
  </body>
</html>
css
#graph-container {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
  margin: 0;
  overflow: hidden;
}