npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

elkjs-svg

v0.2.1

Published

Simple SVG renderer for elkjs

Downloads

11,084

Readme

elkjs-svg

A SVG generator for JSON graphs laid out with elkjs that requires no further dependencies. ELK handles the conversion of ELK JSON to "layouted JSON". Elkjs-svg converts the layouted JSON to SVG.

Usage

If you want to use it in the browser, consider using browserify.

To use it from node.js:

npm install elkjs-svg

Put this code into test.js:

const ELK = require('elkjs')
const elksvg = require('elkjs-svg');

var graph = {
  "id": "root",
  "layoutOptions": {
    "elk.algorithm": "layered"
  },
  "children": [
    {"id": "n1", "width": 30, "height": 30},
    {"id": "n2", "width": 30, "height": 30},
    {"id": "n3", "width": 30, "height": 30}
  ],
  "edges": [
    {"id": "e1", "sources": ["n1"], "targets": ["n2"]},
    {"id": "e2", "sources": ["n1"], "targets": ["n3"]}
  ]
}

const elk = new ELK()
elk.layout(graph)
  .then(data => {
    var renderer = new elksvg.Renderer();
    var svg = renderer.toSvg(data);
    console.log(svg);
  })

This will be the output in the terminal:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="104" height="104">
  <defs>
    <style type="text/css">
      ...
    </style>
    <marker>...</marker>
  </defs>
  <g>
    <polyline points="42,27 62,27" id="e1" class="edge" />
    <polyline points="42,37 52,37 52,77 62,77" id="e2" class="edge" />
    <rect id="n1" class="node" x="12" y="17" width="30" height="30" />
    <rect id="n2" class="node" x="62" y="12" width="30" height="30" />
    <rect id="n3" class="node" x="62" y="62" width="30" height="30" />
  </g>
</svg>

Which when opened in a browser looks like this:

Elkjs SVG example image

Customizing CSS and defs

The generated SVG elements can be styled using css. A simple style definition is already included and used as default. Each SVG element's id equals the id in the json graph. Additionally, nodes, edges, ports and labels receive a class attribute equal to their type (e.g. .node).

Custom styles and svg definitions can be specified as follows:

const ELK = require('elkjs')
const elksvg = require('elkjs-svg');

var graph = {
  "id": "root",
  "layoutOptions": {
    "elk.algorithm": "layered"
  },
  "children": [
    {"id": "n1", "width": 30, "height": 30},
    {"id": "n2", "width": 30, "height": 30},
    {"id": "n3", "width": 30, "height": 30}
  ],
  "edges": [
    {"id": "e1", "sources": ["n1"], "targets": ["n2"]},
    {"id": "e2", "sources": ["n1"], "targets": ["n3"]}
  ]
}

const elk = new ELK()
elk.layout(graph)
    .then(data => {
      var renderer = new elksvg.Renderer();
      var svg = renderer.toSvg(
        data,
        styles=`
          rect {
            opacity: 0.8;
            fill: #6094CC;
            stroke-width: 1;
            stroke: #222222;
          }
        `, 
        defs=`
          <marker id="node">
            <path d="0,7 L10,4 L0,1 L0,7" style="" />
          </marker>
        `
      );
      console.log(svg);
    })

To remove all the default styling and defs, set them to en empty string while calling toSvg().

Specify custom attributes, classes and styles

It is possible to specify further attributes, classes, and styles as part of the json graph. A node definition like this

[...]
{
  "id": "node1",
  "class": ["myClass", "otherClass"],
  "attributes": {
    "data-foo": "bar",
    "rx": 5
  },
  "style": "fill: #ddd;"
}

results in a corresponding svg element

<rect id="node1" class="myClass otherClass node" x="12" y="12" width="0" height="0" style="fill: #ddd;" data-foo="bar" rx="5" />

Running tests

The test runner goes through all files in test/testcases/*.json, renders them with ELK, and compares the result with the SVG with the same name.

Run all the tests by typing npm test in the project root.

To run just one test use ONLY_TEST="simple" npm test, where "simple" is the name of the json files you want to render.

To add a new testcase, put a new json files into test/testcases, and use the test-render script to generate a new SVG:

env ONLY_RENDER="your-testcase" npm run test-render | awk "NR>4" > test/testcases/your-testcase.svg

This tells the test-render script to only render your-testcase, removes the first four lines (which are output from npm), and writes the resulting SVG to the path you specify. [/Elkjs SVG exampl( imagee.svg]:)