Module

DataViz.Layout.Sankey.Compute

Package
purescript-hylograph-layout
Repository
afcondon/purescript-hylograph-layout

DataViz.Layout.Sankey.Compute

Pure PureScript implementation of Sankey diagram layout computation. Uses State monad to thread graph model through all layout functions. Starts with Array SankeyLinkInput and generates nodes dynamically.

Reference: https://github.com/d3/d3-sankey (ISC License)

Cycles. d3-sankey throws on a cycle. Here a depth-first search sets aside, as a back-edge, every link that returns to a node still on the search's path: the search starts from each node in the order the rows first name it, and follows a node's links in the same order (of their targets' first naming). So a graph that is one cycle, with no node without a link in, starts from the node named first, and the link back to it is the one set aside. A link from a node to itself is always a back-edge. The rest is a DAG and is laid out as d3-sankey would lay it out; the back-edges keep their widths and a place on their nodes (computeBackEdgeBreadths), and DataViz.Layout.Sankey.BackEdge routes them under the chart.

Size. Through 0.6.0 every step looked nodes and links up by scanning arrays (find, filter inside loops and inside sort comparators), so relaxation was cubic or worse in practice: 677 links, with lanes, took 20 to 30 s. They are now looked up in maps built once a step, with the same arithmetic in the same order, so every position is what it was, to the bit. The rows are folded in without State, and the back-edge search keeps its own stack: both overflowed the JS stack at about 3,000 rows or 3,000 nodes deep, which lanes reach quickly.

#computeLayout Source

computeLayout :: Array LinkCSVRow -> Number -> Number -> SankeyLayoutResult

Main entry point: Compute Sankey layout with default configuration Takes array of link inputs with named nodes (from CSV parsing)

#computeLayoutWithConfig Source

computeLayoutWithConfig :: Array LinkCSVRow -> SankeyConfig -> SankeyLayoutResult

Compute Sankey layout with custom configuration This is the main algorithm using State monad to thread the graph model

#Scale Source

type Scale = { columnPitch :: Number, pixelsPerUnit :: Number }

A scale fixed by the caller, where extent would otherwise fit the chart to a box: pixelsPerUnit is the height of one unit of value, and columnPitch the distance from the left of one column to the left of the next.

#configAtScale Source

configAtScale :: Scale -> Array LinkCSVRow -> SankeyConfig -> SankeyConfig

The configuration whose extent gives the chart that scale: it keeps extent.x0 and extent.y0 and sets extent.x1 and extent.y1, so that the fullest column is exactly full and the columns are columnPitch apart. Lay out with the result:

computeLayoutWithConfig rows (configAtScale scale rows config)

which is computeLayoutAtScale scale rows config, but for reading the graph twice. It runs the layout as far as the columns (no relaxation), which is all the extent depends on. The scale comes out to within rounding: the layout still divides the height it is given by the fullest column.

#computeLayoutAtScale Source

computeLayoutAtScale :: Scale -> Array LinkCSVRow -> SankeyConfig -> SankeyLayoutResult

computeLayoutWithConfig rows (configAtScale scale rows config), the same to the bit, reading the graph once.