Skip to content

Commit

Permalink
Merge branch 'ports'
Browse files Browse the repository at this point in the history
  • Loading branch information
ricklupton committed Nov 3, 2017
2 parents b04cae5 + 81ef7ba commit 5fbe9e1
Show file tree
Hide file tree
Showing 65 changed files with 5,279 additions and 1,331 deletions.
6 changes: 6 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((js2-mode
(flycheck-checker . javascript-standard)))

59 changes: 3 additions & 56 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,57 +1,4 @@
/lib

.tern-port
/build
/docs

# Created by https://www.gitignore.io/api/node,linux

### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history


### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

/d3-sankey-diagram.js
/node_modules
.tern-port
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

See the **[demo](https://ricklupton.github.io/d3-sankey-diagram)** for examples of these.

d3-sankey-diagram versions v0.5 and up are based on d3 v4.

## Installation

Install using npm if you are using browserify or the like:
Expand All @@ -25,17 +27,21 @@ Or download the [standalone bundle](https://github.com/ricklupton/d3-sankey-diag
## Usage

```js
var diagram = sankeyDiagram()
.width(1000)
.height(600)
.margins({ left: 100, right: 160, top: 10, bottom: 10 })
.nodeTitle(function(d) { return d.data.title !== undefined ? d.data.title : d.id; })
.linkTypeTitle(function(d) { return d.data.title; })
.linkColor(function(d) { return d.data.color; });
var layout = d3.sankey()
.extent([[100, 10], [840, 580]]);

var linkTitle = d3.sankeyLinkTitle(function(d) { return d.title; },
function(d) { return d.title; },
d3.format('.3s'));

var diagram = d3.sankeyDiagram()
.linkTitle(linkTitle)
.linkColor(function(d) { return d.color; });

d3.json('uk_energy.json', function(energy) {
layout.ordering(energy.order);
d3.select('#sankey')
.datum(energy)
.datum(layout(energy))
.call(diagram);
});
```
Expand All @@ -45,11 +51,7 @@ Try more [live examples](https://ricklupton.github.io/d3-sankey-diagram).
If you use the Jupyter notebook, try
[ipysankeywidget](https://github.com/ricklupton/ipysankeywidget).

`d3-sankey-diagram` works both in node (using jsdom) and in the browser. To use
jsdom, transitions must be disabled using
```js
diagram.duration(null);
```
`d3-sankey-diagram` works both in node (using jsdom) and in the browser.

## Documentation

Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
var sankeyDiagram = require('./lib/diagram').default;
export {default as sankey} from './src/sankey.js'
export {default as sankeyPositionJustified} from './src/sankeyLayout/verticalJustified.js'
export {default as sankeyPositionRelaxation} from './src/sankeyLayout/verticalRelaxation.js'

module.exports = sankeyDiagram;
export {default as sankeyLink} from './src/linkPath.js'
export {default as sankeyNode} from './src/node.js'

export {default as sankeyDiagram, linkTitleGenerator as sankeyLinkTitle} from './src/diagram.js'
83 changes: 47 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
{
"name": "d3-sankey-diagram",
"version": "0.4.4",
"description": "Sankey diagram d3 component",
"main": "index.js",
"directories": {
"test": "test"
"version": "0.5.0",
"description": "Sankey diagram d3 plugin",
"author": "Rick Lupton",
"keywords": [
"d3",
"d3-module",
"sankey",
"diagram"
],
"license": "MIT",
"main": "build/d3-sankey-diagram.js",
"jsnext:main": "index",
"homepage": "https://github.com/ricklupton/d3-sankey-diagram",
"repository": {
"type": "git",
"url": "https://github.com/ricklupton/d3-sankey-diagram.git"
},
"scripts": {
"pretest": "rm -rf build && mkdir build && rollup -c",
"test": "tape -r ./test/buble-register -r reify 'test/**/*-test.js' && standard index.js src test",
"test:watch": "tape-watch -r ../test/buble-register -r reify 'test/**/*-test.js'",
"prepublishOnly": "npm run test && uglifyjs build/d3-sankey-diagram.js -c -m -o build/d3-sankey-diagram.min.js",
"postpublish": "zip -j build/d3-sankey-diagram.zip -- LICENSE README.md build/d3-sankey-diagram.js build/d3-sankey-diagram.min.js"
},
"dependencies": {
"babel-runtime": "^6.9.2",
"d3": "~3.5.16",
"defined": "^1.0.0",
"graphlib": "~2.1.0",
"sankey-layout": "~0.2.5"
"d3-array": "^1.0.2",
"d3-collection": "^1.0.2",
"d3-dispatch": "^1.0.3",
"d3-format": "^1.1.1",
"d3-interpolate": "^1.1.3",
"d3-selection": "^1.0.3",
"d3-transition": "^1.0.4",
"graphlib": "~2.1.0"
},
"devDependencies": {
"almost-equal": "~1.0.0",
"babel-cli": "~6.5.1",
"babel-core": "~6.5.2",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "~6.5.0",
"babel-tape-runner": "^2.0.1",
"babelify": "~7.2.0",
"browserify": "~13.0.0",
"browserify-global-shim": "^1.0.3",
"defined": "~1.0.0",
"jsdoc": "~3.4.0",
"jsdom": "^9.4.1",
"tape": "^4.5.1",
"tape-run": "^2.1.3"
},
"scripts": {
"prepublish": "npm run build && npm run bundle",
"build": "babel src -d lib",
"bundle": "browserify --standalone sankeyDiagram -t babelify -t [ browserify-global-shim --d3 d3 ] index.js > d3-sankey-diagram.js",
"test": "browserify -t babelify test/test-*.js | tape-run",
"test:node": "babel-tape-runner 'test/**/*.js'",
"jsdoc": "jsdoc --package package.json -r lib/ -d docs"
},
"author": "Rick Lupton",
"repository": "https://github.com/ricklupton/d3-sankey-diagram",
"license": "MIT"
"almost-equal": "^1.1.0",
"babel-eslint": "^7.1.1",
"buble": "^0.15.2",
"d3-scale": "^1.0.6",
"faucet": "0.0.1",
"jsdom": "^11.1.0",
"reify": "^0.4.4",
"rollup": "0.27",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-commonjs": "^7.0.0",
"rollup-plugin-node-resolve": "^2.0.0",
"standard": "^8.6.0",
"tape": "4",
"tape-watch": "^2.2.4",
"uglify-js": "2"
}
}
41 changes: 41 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Rollup plugins
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import buble from 'rollup-plugin-buble'

export default {
entry: 'index.js',
dest: 'build/d3-sankey-diagram.js',
format: 'umd',
moduleName: 'd3',
globals: {
'd3-collection': 'd3',
'd3-array': 'd3',
'd3-selection': 'd3',
'd3-transition': 'd3',
'd3-dispatch': 'd3',
'd3-format': 'd3',
'd3-interpolate': 'd3'
},
external: [
'd3-collection',
'd3-array',
'd3-selection',
'd3-transition',
'd3-dispatch',
'd3-format',
'd3-interpolate'
],
plugins: [
resolve({
jsnext: true,
main: true
}),
commonjs({
namedExports: {
'node_modules/graphlib/index.js': ['Graph', 'alg']
}
}),
buble()
]
}
7 changes: 7 additions & 0 deletions src/assignRanks/.dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((js2-mode
(flycheck-javascript-standard-executable . "/home/rick/ownCloud/devel/d3-sankey-diagram/node_modules/.bin/standard"))
(flycheck-checker . javascript-standard)))

92 changes: 92 additions & 0 deletions src/assignRanks/grouped-graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Graph } from 'graphlib'
import { map } from 'd3-collection'
/**
* Create a new graph where nodes in the same rank set are merged into one node.
*
* Depends on the "backwards" attribute of the nodes in G, and the "delta"
* atribute of the edges.
*
*/
export default function groupedGraph (G, rankSets = []) {
// Not multigraph because this is only used for calculating ranks
const GG = new Graph({directed: true})
if (G.nodes().length === 0) return GG

// Make sure there is a minimum-rank set
rankSets = ensureSmin(G, rankSets)

// Construct map of node ids to the set they are in, if any
const nodeSets = map()
var set
var id
var i
var j
for (i = 0; i < rankSets.length; ++i) {
set = rankSets[i]
if (!set.nodes || set.nodes.length === 0) continue
id = '' + i
for (j = 0; j < set.nodes.length; ++j) {
nodeSets.set(set.nodes[j], id)
}
GG.setNode(id, { type: set.type, nodes: set.nodes })
}

// use i to keep counting new ids
var nodes = G.nodes()
G.nodes().forEach(u => {
const d = G.node(u)
if (!nodeSets.has(u)) {
id = '' + (i++)
set = { type: 'same', nodes: [u] }
nodeSets.set(u, id)
GG.setNode(id, set)
}
})

// Add edges between nodes/groups
G.edges().forEach(e => {
const d = G.edge(e)
const sourceSet = nodeSets.get(e.v)
const targetSet = nodeSets.get(e.w)

// Minimum edge length depends on direction of nodes:
// -> to -> : 1
// -> to <- : 0
// <- to -> : 0 (in opposite direction??)
// <- to <- : 1 in opposite direction
const edge = GG.edge(sourceSet, targetSet) || { delta: 0 }
if (sourceSet === targetSet) {
edge.delta = 0
GG.setEdge(sourceSet, targetSet, edge)
} else if (G.node(e.v).backwards) {
edge.delta = Math.max(edge.delta, G.node(e.w).backwards ? 1 : 0)
GG.setEdge(targetSet, sourceSet, edge)
} else {
edge.delta = Math.max(edge.delta, G.node(e.w).backwards ? 0 : 1)
GG.setEdge(sourceSet, targetSet, edge)
}
})

return GG
}

// export function linkDelta (nodeBackwards, link) {
// if (nodeBackwards(link.source)) {
// return nodeBackwards(link.target) ? 1 : 0
// } else {
// return nodeBackwards(link.target) ? 0 : 1
// }
// }

function ensureSmin (G, rankSets) {
for (var i = 0; i < rankSets.length; ++i) {
if (rankSets[i].type === 'min') {
return rankSets // ok
}
}

// find the first sourceSet node, or else use the first node
var sources = G.sources()
var n0 = sources.length ? sources[0] : G.nodes()[0]
return [{ type: 'min', nodes: [ n0 ] }].concat(rankSets)
}
Loading

0 comments on commit 5fbe9e1

Please sign in to comment.