This is a proof-of-concept & demo for embedding HTML in Graphviz SVG graphs.
Click here for the demo.
Copy graphEmbed.ts into your project,
and add ts-graphviz1 and wasm-graphviz2 as dependencies.
I considered publishing an NPM package, but
- it's a lot more work than I initially expected;
- the code is short, simple, and contained;
- small adjustments may be required for advanced use-cases.
With that in mind, just letting people copy it seemed a better choice than adding yet another NPM dependency.
We're using Bun for management.
Run bun install to install the dependencies, then bun dev to start the server.
The demo is a very basic Svelte app, and all the code is in App.svelte.
Graphviz can't render HTML so it can't directly embed it in the graph. But it does give us several features that let us do it ourselves:
- per-node width and height can be percisely specified in the DOT language;
- Graphviz renders to a stable SVG;
- the resulting SVG includes metadata, such as node
idattributes.
So our solution includes the following steps:
- render the relevant HTML embeddings outside the graph;
- get their sizes using
getBoundingClientRect(); - set the node sizes in the DOT graph;
- render the graph;
- embed our HTML elements into SVG using
<foreignObject>elements.
For the extra details you can read the code. I tried to document all the relevant parts.