Gathering detailed insights and metrics for @simwrapper/d3-sankey-diagram
Gathering detailed insights and metrics for @simwrapper/d3-sankey-diagram
Gathering detailed insights and metrics for @simwrapper/d3-sankey-diagram
Gathering detailed insights and metrics for @simwrapper/d3-sankey-diagram
npm install @simwrapper/d3-sankey-diagram
71.6
Supply Chain
99.1
Quality
75
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
135 Commits
1 Branches
Updated on 22 Sept 2022
Minified
Minified + Gzipped
JavaScript (99.73%)
Emacs Lisp (0.27%)
Cumulative downloads
Total Downloads
Last day
200%
6
Compared to previous day
Last week
-4.8%
20
Compared to previous week
Last month
146.3%
101
Compared to previous month
Last year
-2.1%
521
Compared to previous year
11
16
1
Sankey diagrams show flows between processes or relationships between sets. This library, is a reusable d3 diagram featuring:
See the demo for examples of these.
d3-sankey-diagram versions v0.6 and up are based on d3 v4.
Install using npm if you are using browserify or the like:
1npm install d3-sankey-diagram
Or download the standalone bundle and include in your page as
1<script src="d3-sankey-diagram.js" charset="utf-8"></script>
1var layout = d3.sankey() 2 .extent([[100, 10], [840, 580]]); 3 4var diagram = d3.sankeyDiagram() 5 .linkColor(function(d) { return d.color; }); 6 7d3.json('uk_energy.json', function(energy) { 8 layout.ordering(energy.order); 9 d3.select('#sankey') 10 .datum(layout(energy)) 11 .call(diagram); 12});
Try more live examples.
If you use the Jupyter notebook, try ipysankeywidget.
d3-sankey-diagram
works both in node (using jsdom) and in the browser.
Thanks for your interest in contributing! To get started see CONTRIBUTING.md and our code of conduct. We have a Roadmap showing what we are working on, and you can browse the list of good first issues for ideas.
d3-sankey-diagram is a JavaScript library for creating Sankey diagrams using d3. See the live examples to get an idea of what it does.
The main high-level components:
Lower-level components:
# sankey()
Creates a new Sankey layout component.
# layout(arguments...)
Apply the layout to the given arguments. The arguments are arbitrary; they are simply propagated to the accessor functions nodes and links.
1var layout = d3.sankey(); 2 3var graph = layout({ 4 nodes: [ 5 {"id": "a", "title": "Source"}, 6 {"id": "b", "title": "Stage 1"} 7 ], 8 links: [ 9 {"source": "a", "target": "b", "type": "x", "value": 2} 10 ] 11}); 12// Resulting graph object can be used as datum for d3.sankeyDiagram() below
# layout.nodes([nodes])
If nodes is specified, sets the nodes accessor to the specified function and returns this layout. If nodes is not specified, return the current accessor, which defaults to:
1function nodes(graph) { 2 return graph.nodes; 3}
# layout.links([links])
If links is specified, sets the links accessor to the specified function and returns this layout. If links is not specified, return the current accessor, which defaults to:
1function links(graph) { 2 return graph.links; 3}
# layout.nodeId([nodeId])
If nodeId is specified, sets the node id accessor to the specified function and returns this layout. If nodeId is not specified, return the current accessor, which defaults to:
1function nodeId(d) { 2 return d.id; 3}
# layout.sourceId([sourceId])
# layout.targetId([targetId])
If sourceId/targetId is specified, sets the link source/target id accessor to the specified function and returns this layout. If sourceId/targetId is not specified, return the current accessor, which defaults to:
1function sourceId (d) { 2 return { 3 id: typeof d.source === 'object' ? d.source.id : d.source, 4 port: typeof d.sourcePort === 'object' ? d.sourcePort.id : d.sourcePort 5 } 6} 7// similarly for targetId
See below for more discussion of ports. If this accessor returns a string, it is
interpreted as the node id and the port is set to undefined
.
# layout.nodeBackwards([nodeBackwards])
If nodeBackwards is specified, sets the node direction accessor to the specified function and returns this layout. If nodeBackwards is not specified, return the current accessor, which defaults to:
1function nodeBackwards(d) { 2 return d.direction && d.direction.toLowerCase() === 'l'; 3}
# layout.linkValue([linkValue])
If linkValue is specified, sets the link value accessor to the specified function and returns this layout. If linkValue is not specified, return the current accessor, which defaults to:
1function linkValue(d) { 2 return d.value; 3}
# layout.linkType([linkType])
If linkType is specified, sets the link type accessor to the specified function and returns this layout. If linkType is not specified, return the current accessor, which defaults to:
1function linkType(d) { 2 return d.type; 3}
# layout.ordering([ordering])
If ordering is specified, sets the node ordering to the specified value and
returns this layout. If ordering is not specified, return the current value, which defaults to null
.
When ordering is null, the node ordering will be calculated automatically.
When ordering is specified, it is used directly and no rank assignment or ordering algorithm takes place. The ordering structure has three nested lists: ordering is a list of layers, each of which is a list of bands, each of which is a list of node ids. For example,
1[ 2 [ ["layer 1 band 1"], ["layer 1 band 2"] ], 3 [ ["layer 2 band 1"], ["layer 2 band 2"] ], 4 ... 5]
# layout.rankSets([rankSets])
If rankSets is specified, sets the rank sets to the specified value and
returns this layout. If rankSets is not specified, return the current value,
which defaults to []
.
Rank sets are optional constraints to keep nodes in the same layer. Each entry has the form
1{ 2 type: 'same|min', // optional, default 'min' 3 nodes: [node ids], // required 4}
# layout.sortPorts([sortPorts])
If sortPorts is specified, sets the port sorting function to the specified function and returns this layout. If sortPorts is not specified, return the current value, which defaults to:
1function sortPorts(a, b) {
2 return a.id.localeCompare(b.id)
3}
Note: in a future version this may be changed to sort ports to avoid crossings by default.
# layout.extent([extent])
If extent is specified, sets this layout’s extent to the specified array of points [[x0, y0], [x1, y1]], where [x0, y0] is the top-left corner and [x1, y1] is the bottom-right corner, and returns this tile layout. If extent is not specified, returns the current layout extent.
# layout.size([size])
If size is specified, sets this layout’s size to the specified two-element array of numbers [width, height] and returns this layout. If size is not specified, returns the current layout size. This is a convenience method equivalent to setting the extent to [[0, 0], [width, height]].
# diagram.scale([scale])
If scale is specified as a number, sets the layout's scale (from data units to
pixels). If scale is null
, the scale will be reset and automatically
calculated the next time the diagram is called, to achieve the desired
whitespace fraction (below). If scale is not specified, return the current
scale.
# diagram.whitespace([whitespace])
If whitespace is specified as a number, sets the layout's whitespace fraction, used when automatically calculating a scale (above). If whitespace is not specified, return the current whitespace.
# diagram.nodePosition([nodePosition])
If nodePosition is specified, use the specified function to directly set node
positions, bypassing the layout algorithm (link positions and shapes are still
calculated). If nodePosition is not specified, return the current
function, which defaults to null
.
# sankeyDiagram()
Creates a new Sankey diagram component.
# diagram(selection)
Apply the diagram to a selection, which should be an svg
element.
1var diagram = d3.sankeyDiagram(); 2d3.select('#sankey') 3 .datum(sankey) 4 .call(diagram);
The Sankey data is taken from the selection's bound data, which should be a graph object, as generated by d3.sankey.
# diagram.margin({ [top], [right], [bottom], [left] })
If called with an argument, set the margins of the diagram, otherwise return the current value.
# diagram.nodeTitle([nodeTitle])
If called with an argument, set the node title to the specified function, otherwise return the current function, which defaults to:
1function nodeTitle(d) { 2 return d.title !== undefined ? d.title : d.id; 3}
# diagram.nodeValue([nodeValue])
If called with an argument, set the node value getter to the specified function, otherwise return the current function, which defaults to:
1function nodeValue(d) { 2 return null; 3}
The node value is shown with an SVG text element within the node body, only when
the nodeWidth
is greater than zero.
# diagram.linkTitle([linkTitle])
If called with an argument, set the link title to the specified function, otherwise return the current function, which defaults to:
1const fmt = d3.format('.3s') 2function linkTitle(d) { 3 const parts = [] 4 const sourceTitle = nodeTitle(d.source) 5 const targetTitle = nodeTitle(d.target) 6 const matTitle = d.type 7 8 parts.push(`${sourceTitle} → ${targetTitle}`) 9 if (matTitle) parts.push(matTitle) 10 parts.push(fmt(d.value)) 11 return parts.join('\n') 12}
The link title is displayed in an SVG title
element, visible on hover.
To make it easier to customise this function to your data, you can use
d3.sankeyLinkTitle
to generate new functions:
# sankeyLinkTitle(nodeTitle, typeTitle, fmt)
Generates a function similar to the one above, with custom accessors for the
node title nodeTitle
, link-type title typeTitle
and number format fmt
.
# diagram.linkLabel([linkLabel])
If called with an argument, set the link label to the specified function, otherwise return the current function, which defaults to:
1function linkLabel(d) { 2 return null 3}
The link label is displayed in an SVG text
element, so unlike the /title/, it
is visible all the time.
# diagram.linkColor([linkColor])
If linkColor is specified, sets the link color accessor to the specified function, otherwise return the current accessor, which defaults to:
1function linkColor(d) { 2 return null; 3}
# diagram.linkMinWidth([linkMinWidth])
If linkMinWidth is specified, sets the minimum link width accessor to the
specified function, otherwise return the current accessor, which by default
returns 1
.
# diagram.groups([groups])
If groups is specified, sets the list of node groups to the specified value. If groups is not specified, return the current list. Node groups display a box around the specified nodes, and should be given in the following format:
1[ 2 { title: "Group title to be displayed above nodes", 3 nodes: ["nodeid1", "nodeid", ...] 4 } 5]
# diagram.on(type[, listener])
Adds or removes an event listener for the specified type. The type string
is one of selectNode
, selectLink
or selectGroup
. The listener is invoked
with the context as the element and one argument, the corresponding data.
If listener is not specified, returns the currently-assigned listener for the specified type, if any.
Run the tests:
1npm test
MIT licence.
type
and value
attributes to link
objects
(thanks @harisbal)fromElsewhere
and toElsewhere
attributes. These can be useful for
simplifying diagrams by avoiding uninteresting flows while still showing how
nodes balance.diagram.linkLabel
. (#2)d3-sankey-diagram.esm.js
: An ES moduled3-sankey-diagram.cjs.js
: A CommonJS module for use with NodeJSd3-sankey-diagram.umd.js
: A UMD module for use in <script>
tagsd3-sankey-diagram.min.js
: A minified version of the UMD modulediagram.nodeValue
to set the format to show
them.diagram.nodeValue
.No vulnerabilities found.
No security vulnerabilities found.