Skip to content
  1. Examples

Text

This example shows how the text attribute can be used to add text to edges in a graph. Texts have font, color, size, backgroun color and other properties, providing a wide range of customization options.

ts
import Ogma from '@linkurious/ogma';

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

// Add a rule that specifies the Edge text style. The text to display is dynamically composed each time the edge is painted.
ogma.styles.addEdgeRule({
  text: {
    maxLineLength: 140, // truncate
    size: 12,
    color: 'black',
    minVisibleSize: 0,
    content: e => 'Edge ' + e.getId()
  }
});

const graph = await ogma.generate.random({ nodes: 25, edges: 20 });
await ogma.setGraph(graph);
await ogma.layouts.force({ locate: true });

// hide edge texts
//ogma.styles.setEdgeTextsVisibility(false);
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;
}