Skip to content

Commit

Permalink
Start version of component Sankey D3 (SKD3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabric-io-rodrigues committed Sep 3, 2017
1 parent dd183fa commit e409efa
Show file tree
Hide file tree
Showing 15 changed files with 2,557 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parserOptions:
sourceType: "module"

env:
es6: true

extends:
"eslint:recommended"

rules:
no-cond-assign: 0
no-constant-condition: 0
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.DS_Store*
build/
node_modules
.idea
*.swp
*~
*.log
ehthumbs.db
Icon?
Thumbs.db
bower_components
coverage
test-results.xml
*.orig
/tmp/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.sublime-*
build/*.zip
test/
134 changes: 134 additions & 0 deletions GruntFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
module.exports = function(grunt) {
var _pkg = grunt.file.readJSON('package.json');

// allows autoprefixer to work on older node_js versions
require('es6-promise').polyfill();

//Project configuration.
grunt.initConfig({
pkg: _pkg,
concat: {
css: {
options: {
separator: '\n',
banner: '/* skd3 version ' + _pkg.version + ' (' + _pkg.url + ') ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
src: [
'src/css/*.css'
],
dest: 'build/sk.d3.css'
},
js: {
options: {
separator: '',
banner: '/* skd3 version ' + _pkg.version + ' (' + _pkg.url + ') ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */\n' + '(function(){\n',
footer: '\nsk.version = "' + _pkg.version + '";\n})();',
sourceMap: true,
sourceMapName: 'build/sk.d3.js.map',
sourceMapStyle: 'embed'
},
src: [
'src/init.js',
//Include d3-sankey core
'node_modules/d3-sankey/build/d3-sankey.min.js',
//Include d3-tip
'node_modules/d3-tip/index.js',
'src/core.js',
'src/utils.js',
// example to exclude files: '!src/models/excludeMe*'
],
dest: 'build/sk.d3.js'
}
},
uglify: {
options: {
sourceMap: true,
sourceMapIncludeSources : true,
sourceMapIn : 'build/sk.d3.js.map',
banner: '/* skd3 version ' + _pkg.version + ' (' + _pkg.url + ') ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
js: {
files: {
'build/sk.d3.min.js': ['build/sk.d3.js']
}
}
},
replace: {
version: {
src: [
'package.js'
],
overwrite: true,
replacements: [{
from: /(version?\s?=?\:?\s\')([\d\.]*)\'/gi,
to: '$1' + _pkg.version + "'"
}]
}
},
jshint: {
foo: {
src: "src/**/*.js"
},
options: {
jshintrc: '.jshintrc'
}
},
watch: {
js: {
files: ["src/**/*.js"],
tasks: ['concat']
}
},
copy: {
css: {
files: [
{ src: 'src/sk.d3.css', dest: 'build/sk.d3.css' }
]
}
},
postcss: {
options: {
processors: [
require('autoprefixer')({
browsers: [
'last 2 versions',
'last 3 iOS versions',
'last 2 safari versions',
'ie >= 9']
})
]
},
dist: {
src: 'build/sk.d3.css'
}
},
cssmin: {
options: {
sourceMap: true
},
dist: {
files: {
'build/sk.d3.min.css' : ['build/sk.d3.css']
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-postcss');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-text-replace');

//grunt.registerTask('default', ['concat', 'copy', 'postcss']);
grunt.registerTask('production', ['concat', 'uglify', 'copy', 'postcss', 'cssmin', 'replace']);
grunt.registerTask('default', ['production']);
grunt.registerTask('release', ['production']);
grunt.registerTask('lint', ['jshint']);
};
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License
The MIT License (MIT)

Copyright (c) 2017 Fabrício Rodrigues
Copyright (c) 2017 Fabr�cio Rodrigues

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,113 @@
# skd3
Sankey Diagram made easy. A javascript library that extends the popular D3.js/d3-sankey to enable fast and beautiful.

[![Build Status](https://travis-ci.org/fabriciorhs/skd3.svg?branch=master)](https://travis-ci.org/fabriciorhs/skd3)
[![Dependencies Status](https://david-dm.org/fabriciorhs/skd3.svg)](https://david-dm.org/fabriciorhs/skd3)

<sup>Inspired by the work of Mike Bostock's [`d3-sankey`](http://github.com/d3/d3-sankey)</sup>

>As a proposal to simplify the generation of the sankey chart
Sankey diagrams visualize the directed flow between nodes in an acyclic network. For example, this diagram shows a possible scenario of UK energy production and consumption in 2050:

<img alt="Sankey diagram" src="https://raw.githubusercontent.com/fabriciorhs/skd3/master/img/energy.png" width="960">

Source: Department of Energy & Climate Change, from: [`Mike Bostock - json sample`](https://bl.ocks.org/mbostock/ca9a0bb7ba204d12974bca90acc507c0).

## Dependencies

SKD3 requires [d3.js](http://d3js.org/) version **[4.9.0](https://github.com/d3/d3/releases/tag/v4.9.0)** and later.

## Installing

If you use NPM, `npm install skd3`. Otherwise, download the [latest release](https://github.com/fabriciorhs/skd3/releases/latest). You can also load directly from [unpkg.com](https://cdn.rawgit.com/):

```html
<script src="https://cdn.rawgit.com/fabriciorhs/skd3/master/build/sk.d3.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/fabriciorhs/skd3/master/build/sk.d3.min.css" />
<style>
#sankeyChart {
height: 500px;
width: 960px;
}
</style>

<div id="sankeyChart"/>
```

## Usage

```js
var objSankey = sk.createSankey('#sankeyChart', configSankey, datajson);
```

Example of config:

```js
var configSankey = {
margin: { top: 10, left: 10, right: 10, bottom: 10 },
nodes: {
dynamicSizeFontNode: {
enabled: true,
minSize: 14,
maxSize: 30
},
canDragNodes: true,
colors: d3.scaleOrdinal(d3.schemeCategory10)
},
links: {
formatValue: function(val) {
return d3.format(",.0f")(val) + ' TWh';
}
},
tooltip: {
infoDiv: true,
labelSource: 'Input:',
labelTarget: 'Output:'
}
}
```

Example data json:

```js
var datajson = {nodes: [
{"id": "Alice"},
{"id": "Bob"},
{"id": "Carol"}
],
links: [
{"source": "Alice", "target": "Bob"},
{"source": "Bob", "target": "Carol"}
]};
```


## Update links values using d3`s transitions:

```js
objSankey.updateData(new_dataJson);
```

Result:

<img alt="Sankey transitions" src="https://raw.githubusercontent.com/fabriciorhs/skd3/master/img/sankey_transitions.gif" width="400">


## Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/fabriciorhs/skd3/issues/new)

## Author

**Fabricio Rodrigues**

+ [github/fabriciorhs](https://github.com/fabriciorhs)
+ [twitter/fabriciorhs](http://twitter.com/fabriciorhs)

## License

Copyright © 2017 [Fabricio Rodrigues](https://github.com/fabriciorhs)
Released under the MIT license.

***
3 changes: 3 additions & 0 deletions examples/data_energyjson.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Sankey Diagram</title>
<body>
<div id="chart"/>
<script src="https://unpkg.com/d3@4.10.2"></script>
<script src="../build/sk.d3.min.js"></script>
<link rel="stylesheet" type="text/css" href="../build/sk.d3.min.css" />
<script src="data_energyjson.js"></script>
<style>
#chart {
height: 500px;
width: 960px;
}
</style>
<script>
var configSankey = {
margin: { top: 10, left: 10, right: 10, bottom: 10 },
nodes: {
dynamicSizeFontNode: {
enabled: true,
minSize: 14,
maxSize: 30
},
canDragNodes: true,
colors: d3.scaleOrdinal(d3.schemeCategory10)
},
links: {
formatValue: function(val) {
return d3.format(",.0f")(val) + ' TWh';
}
},
tooltip: {
infoDiv: true,
labelSource: 'Input:',
labelTarget: 'Output:'
}
}

var objSankey = sk.createSankey('#chart', configSankey, energyjson);
</script>
</body>
</html>
Binary file added img/energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/sankey_transitions.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e409efa

Please sign in to comment.