Skip to content

Browser (JavaScript)

predictygraph-js (npm package predictygraph) renders .pdg diagrams to SVG in the browser, with the same layout, routing and themes as pdg. It has no runtime dependencies (27 kB gzipped).

Script tag

<script src="https://cdn.jsdelivr.net/npm/predictygraph"></script>

<pre class="pdg">
graph dir=LR
pod "Pod" -> sched "Scheduler" -> gpu "GPU"
</pre>

On page load every pre.pdg, div.pdg and code.language-pdg element is replaced with its SVG, so fenced ```pdg blocks from Markdown renderers work as-is. A diagram with an error is replaced with the error message.

ES module

npm install predictygraph
import { render } from "predictygraph";

element.innerHTML = render(source, { theme: "nord" });

API

  • render(source, opts?) returns the SVG as a string. Throws PdgSyntaxError (with .line) on invalid input.
  • run(opts?) renders matching elements in the page. opts.selector overrides the default selector; opts.nodes takes elements directly.
  • initialize(opts) sets defaults for later calls. startOnLoad: false turns off the automatic run on page load.
  • listThemes() returns the built-in theme names.

Options for render and initialize:

  • theme: a built-in theme; overrides theme= in the diagram.
  • css: extra stylesheet text (string or array), applied after the theme.
  • resolve(path, kind): maps stylesheet and image paths in the diagram to CSS text or an image URL. Only data: and http(s): image URLs are used.
  • measurer: custom text measurement ({ width(text, family, size, bold) }). The default measures with a canvas, so labels fit the page's fonts.
  • t: animation time in seconds for diagrams with animate.

Differences from the command line

Local font files, --target presets and PNG/APNG output are only in pdg.