JavaScript is required

Using relation-graph in HTML

@relation-graph/html is the official distribution for plain HTML, Vanilla JavaScript, jQuery, Angular, and other browser environments. It uses regular DOM, has no React/Vue/Svelte dependency, and creates no Shadow Root.

The recommended entry point is the asynchronous factory:

const graph = await createRelationGraph(host, config);

When the factory resolves, the core instance, initial data, layout, and first render are ready.

Direct UMD usage

The following file runs from any static server:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>relation-graph HTML</title>
  <link rel="stylesheet"
        href="https://unpkg.com/@relation-graph/html@3.1.2/style.css">
  <style>
    html, body {height: 100%; margin: 0;}
    #graph {width: 100%; height: 100%;}
  </style>
</head>
<body>
  <div id="graph"></div>
  <script src="https://unpkg.com/@relation-graph/html@3.1.2/relation-graph.umd.js"></script>
  <script>
    RelationGraph.createRelationGraph('#graph', {
      options: {
        layout: {layoutName: 'center'},
        defaultNodeWidth: 120,
        defaultNodeHeight: 48
      },
      data: {
        rootId: 'root',
        nodes: [
          {id: 'root', text: 'Relation Graph'},
          {id: 'html', text: 'HTML'}
        ],
        lines: [
          {id: 'root-html', from: 'root', to: 'html', text: 'runs in'}
        ]
      },
      events: {
        onNodeClick(node) {
          console.log('node:', node.id);
        }
      }
    }).then(graph => {
      window.graph = graph;
    });
  </script>
</body>
</html>

The UMD global is RelationGraph. Pin a release number in production instead of using latest.

Native browser ESM

<link rel="stylesheet"
      href="https://unpkg.com/@relation-graph/html@3.1.2/style.css">
<div id="graph" style="width:100%;height:600px"></div>

<script type="module">
  import {
    createRelationGraph,
    RGLineShape
  } from 'https://unpkg.com/@relation-graph/html@3.1.2/relation-graph.mjs';

  const graph = await createRelationGraph('#graph', {
    options: {
      layout: {layoutName: 'tree', from: 'left'},
      defaultLineShape: RGLineShape.StandardCurve
    },
    data: {
      rootId: 'a',
      nodes: [{id: 'a', text: 'A'}, {id: 'b', text: 'B'}],
      lines: [{id: 'a-b', from: 'a', to: 'b'}]
    }
  });

  graph.zoomToFit();
</script>

npm and build tools

npm install @relation-graph/html
import {createRelationGraph} from '@relation-graph/html';
import '@relation-graph/html/style.css';

const graph = await createRelationGraph(document.querySelector('#graph'), {
  options: {layout: {layoutName: 'center'}},
  data: {nodes: [], lines: []},
  ui: {toolbar: true}
});

Updates and teardown

await graph.setJsonData(nextData);
graph.updateOptions({backgroundColor: '#f7f8fa'});
graph.updateNode('node-1', {text: 'Updated'});
await graph.refresh();

// Release resources when a page, route, or dialog unmounts.
graph.destroy();

destroy() releases DOM listeners, ResizeObserver instances, plugins, layers, subscriptions, and core resources. Destroy the previous graph before mounting another graph into the same host.

Next steps