Skip to content
  1. Examples

Shape

This example shows how to use the shape attribute to change the shape of edges in a graph. It consists of four keys: head, tail, body and style.

ts
import Ogma, {
  RawGraph,
  EdgeStyle,
  EdgeExtremity,
  EdgeType
} from '@linkurious/ogma';

const TYPE: EdgeType[] = ['line', 'triangle'];
const STYLE: EdgeStyle[] = ['plain', 'dashed'];
const HEAD: EdgeExtremity[] = [null, 'arrow'];
const TAIL: EdgeExtremity[] = [null, 'arrow'];

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

ogma.styles.addEdgeRule({
  shape: {
    body: () => TYPE[(Math.random() * TYPE.length) | 0],
    style: () => STYLE[(Math.random() * STYLE.length) | 0],
    head: () => HEAD[(Math.random() * HEAD.length) | 0],
    tail: () => TAIL[(Math.random() * TAIL.length) | 0]
  }
});

const graph = await ogma.generate.grid({ rows: 4, columns: 4 });
graph.nodes.forEach(node => {
  node.attributes!.radius = 1;
});
graph.edges.push({ source: '5', target: '6' }, { source: '9', target: '10' });
await ogma.setGraph(graph);
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;
}