Skip to content
  1. Examples

Grid

This example shows how to generate a grid using the grid method.

ts
import Ogma from '@linkurious/ogma';

const COLORS = [
  '#161344',
  '#3f1c4c',
  '#632654',
  '#86315b',
  '#a93c63',
  '#cd476a',
  '#f35371'
];
const ROWS = 10;
const COLUMNS = 7;

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

const graph = await ogma.generate.grid({ rows: ROWS, columns: COLUMNS });

// Assign the graph to Ogma
await ogma.setGraph(graph);
await ogma.view.locateGraph();

document.getElementById('load')?.remove();

// Color by column
const colorValues = [];
for (let i = 0; i < COLUMNS; i++) {
  for (let j = 0; j < ROWS; j++) {
    const n = j * COLUMNS + i;
    colorValues.push(COLORS[i % COLORS.length]);
  }
}
ogma.getNodes().setAttributes(
  colorValues.map(color => ({ color })),
  250
);
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>
    <span id="load">Loading...</span>
    <script src="index.ts"></script>
  </body>
</html>
css
#graph-container {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
  margin: 0;
  overflow: hidden;
}
#load {
  position: absolute;
  top: 0;
  left: 0;
  padding: 5px;
  color: #fff;
  background: #141229;
  font-size: 12px;
  font-family: monospace;
}