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 🙏

© 2024 – Pkg Stats / Ryan Hefner

d3.chart.sankey

v0.4.0

Published

Reusable D3 Sankey diagram using d3.Chart

Downloads

229

Readme

d3.chart.sankey

npm version Dependency Status

Reusable D3 Sankey diagram using d3.Chart.

Demos: energy | product | interactive

Usage

Include d3, sankey and d3.chart before d3.chart.sankey:

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://rawgit.com/newrelic-forks/d3-plugins-sankey/v1.1.0/sankey.js"></script>
<script src="https://rawgit.com/misoproject/d3.chart/master/d3.chart.min.js"></script>
<script src="d3.chart.sankey.min.js"></script>

Then build the chart:

var chart = d3.select("#chart").append("svg").chart("Sankey");
chart.draw({nodes: [/*...*/], links: [/*...*/]});

The chart object supports various ways to customize the diagram:

chart
  .nodeWidth(24)    // width of node
  .nodePadding(8)   // vertical space between nodes
  .iterations(32)   // number of layout iterations
  .spread(false)    // whether to spread nodes vertically after layout
  .name(function(n) { return n.name; })               // node label
  .colorNodes('red')                                  // color for nodes
  .colorNodes(d3.chart.category20c())                 // ... or a color scale
  .colorNodes(function(name, node) { return 'red'; }) // ... or a function
  .colorLinks('yellow')                               // color for labels
  .colorLinks(function(link) { /* ... */ })           // ... or a function
  .alignLabel('start')                                // align node labels: start, end, auto
  .alignLabel((n) => n.x > 100 ? 'end' : 'start');    // ... or a function

The spread option does not exist in D3's Sankey plugin and can make some diagrams clearer. When enabled, nodes are distributed over the full height. This may work best when the number of iterations is set to zero.

The following events are emitted on the chart: node:mouseover, node:mouseout, node:click, node:dblclick, link:mouseover, link:mouseout, link:click, link:dblclick. For example:

chart.on('node:click', function(node) {
  alert('Clicked on ' + node.name);
});

AMD, CommonJS, ...

This chart can also be loaded as a module:

var d3 = require('d3');
var Sankey = require('d3.chart.sankey');

var svg = d3.select('svg');
var chart = new Sankey(svg);

Chart types

There are three chart types: Sankey, Sankey.Selection and Sankey.Path. The last two charts add the notion of a selection, which is set on mouseover when hovering a node or path, or when the selection method is called on the chart (which accepts an array of nodes and links). Sankey.Path expands the selection to connected nodes and links.

When loading as a module, you can also access these charts as properties, so instead of new Sankey(g) you'd use new Sankey.Selection(g);

Special theming

If you add a property to a node object named "id", d3.chart.sankey will add an HTML attribute named "data-node-id" with the value of the "id" property to the node's <g> tag.

This allows CSS and non-D3 JavaScript code to target the node with a selector. For example, in CSS:

[data-node-id="some-id"] rect {
  fill: red;
}

... or in JavaScript...

document.querySelectorAll('[data-node-id="some-id"]');

Building

You'll need Node.js, Grunt and Bower. To fetch required dependencies, run the following commands from the root of this repository:

$ npm install
$ bower install

After this, the project can be built by invoking Grunt from within this repository:

$ grunt

To build the project along with a minified version of the library, run grunt release.