0.1.0 • Published 2 years ago

sankey-plus-test v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

sankey-plus

NOT READY FOR USE YET - CONTAINS BUGS :)

A JavaScript library for computing and drawing Sankey graphs.

Based on the D3-Sankey and D3-Sankey-Circular(https://github.com/tomshanley/d3-sankey-circular) libraries, and the experiments in my Observable notebook.

This library enhances those to provide:

  • Handling for circular links
  • Routing of long links to avoid overlapping nodes
  • Improved layout
  • Option to use d3.scaleLinear to size nodes and links, to ensure consistent scaling over different charts
  • New API that uses a config object

API Reference

Import SankeyChart from sankeyPlus.js:

import { SankeyChart } from './sankeyPlus.js'

Create a new chart:

let chart = new SankeyChart(element, config);
  • element: An HTML element, such as a DIV, which will contain the SVG element created by SankeyChart.draw()
  • config: an options object containing the configuration for the sankey chart.

To render the chart, call the draw() method, which creates an SVG element in the specified HTML element:

chart.draw();

Config options:

The config options has the structure:

let config = {
    //main options
    nodes: {
        //data and options for nodes
    },
    links: {
        //data and options for links
    },
    arrows: {
        //data and options for links
    }
}

Main

OptionDescriptionDefaultMandatory
justifyTitleLeftNo
idIf id is specified, sets the node id accessor to the specified functiond => d.nameNo
iterationsThe number of relaxation iterations32No
extentText[0, 0,, 1, 1]No
paddingPadding around the chart, in pixels20No
widthWidth of the chart1000No
heightHeight of the chart500No
useManualScaleTBC True/FalseTrueNo

Nodes

OptionDescriptionDefaultMandatory
dataAn array of nodes objects, which must contain a property for the ID that is specified in the id option (the default is name).NAYes
widthWidth of the node, in pixels25No
maxHeightIf useManualScale is enabled, then the maximum height of the node for the scale's range75No
paddingThe ideal vertical space between each node25No
minPaddingThe minimum vertical space allowed between each node25No
virtualPaddingThe vertical space between nodes and any virtual links which are routed around nodes7No
sortA function that determines how nodes are ordered from left to right. This applies where there are circular links in the graph, so the 'first' nodes is arbitary. The sort will be overridden by the nodes order based on in incoming/outgoing links.nullNo
setPositionsIf true, then the nodes' positions aren't optimised vertical.falseNo

Links

OptionDescriptionDefaultMandatory
dataAn array of links objects, which must contain a source, target and value property. The source and target must refer to a node's ID.NAYes
circularGapText5No
circularLinkPortionTopBottomThe portion of the Height that should be reserved for drawing circular links0.4No
circularLinkPortionLeftRightThe portion of the Width that should be reserved for drawing circular links0.1No
useVirtualRoutesTextTextNo
virtualPaddingTextTextNo
baseRadiusTextTextNo
verticalMarginTextTextNo
opacityOpacity of the link stroke, from 0 to 11No
virtualLinkTypeTextTextNo

Arrows

OptionDescriptionDefaultMandatory
enabledSelect whether to show arrows. True/FalsefalseNo
colorColor of the arrow. Can use Hex code or CSS color nameDarkSlateGreyNo
lengthLength of the arrow, in pixels10No
gapLength of the gap between each arrow, in pixels25No
headSizewidth of the arrow head, in pixels4No