Module

DataViz.Layout.Abacus

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

The abacus diagram. A flow (nodes, and links that carry a quantity of some kind) drawn with counted units where a Sankey has ribbons of a width: each node is a small grid of equal units, ten to a line unless told otherwise; one unit is worth a declared amount; a unit's kind (its colour, when drawn) is the kind of what flows. The links are wires that say only that two nodes are joined. It is Neurath's rule for Isotype (more of a thing is shown by more signs, never by a bigger sign) on a Sankey's graph.

  ▪▪▪▪▪▪▪▪▪▪        ▪▪▪▪▪▪▪▪▪▪
  ▪▪▪▪▪▪▪▪▪▪ ────── ▪▪▪▪▪▪▪▫▫▫ ────── ▪▪▪▪▪▪▪
  ▪▪▪▪▪                    ╲
  a source                  ╲──────── ▫▫▫

The columns, the order down a column and the links that close a cycle are the Sankey's (computeLayoutWithConfig on the same rows with config.sankey, or computeLayoutWithLanes when wires have lanes): the abacus keeps them and replaces the sizes, since a node here is as large as its units.

The laws (the test suite holds the layout to each):

  • A node's total is the larger of what comes in and what goes out.
  • A node's kinds, and what arises (Composition; IncomingAndArising unless told otherwise). What comes in, by kind, is in the node at its own amount, Carried. If more goes out than comes in, the rest (outgoing - incoming) arises in the node: a kind has rest * (outgoing of that kind / outgoing) of it, Arising. So the quantities sum to the total, a node that nothing comes into is all Arising, and a node where no more goes out than comes in is all Carried.
  • How many units. With n = total / worth: a total of nothing is one Empty; 0 < n < 1 is one Part, of the node's largest share; otherwise the node has round n units, all Whole (a half rounds up).
  • Which kinds get them. Those round n units are shared out by largest remainder: a share (a kind, carried or arising) of quantity q, worth w = q / worth units, gets floor w or floor w + 1 of them, the extra ones going to the kinds with the largest fractions w - floor w (of two equal fractions, to the larger kind). So the kinds' units sum to the node's, and no kind is more than one unit from its share. A kind worth less than one unit may get none.
  • Places. A unit lies inside its node's box, which is exactly the box around the node's units; no two units of a node overlap; no two nodes overlap.
  • Wires. One for each ordered pair of different nodes that links of a quantity join, from the right side of its source to the left side of its target. It turns back exactly when the Sankey set those links aside as back-edges, so an input with no cycle has none that do. A link from a node to itself is no wire: its node returnsToItself.
  • A wire's line keeps clear (wireSegments). Every wire first runs level to the right side of its source's column. The line of a wire to the next column (Onward), of a wire with lanes (Laned: one that passes over one or more columns, when lanes are on, as they are unless told otherwise) and of a wire that turns back (TurningBack: down the space after its source's column, along a channel of its own below every node, up the space before its target's column) goes through no node's box and through no node's room for a name (labelRoom, nodeMinWidth). Not promised: two wires may cross, or run together, in the space between columns; with WithoutLanes a wire over a column goes straight through it; and a caller's nodeLayer can put a forward wire's target in its source's column or before it, where nothing is promised either.
  • The extent holds every wire's line.
let links = kindedBy _.s rows                     -- a kind for each row
let worth = niceWorthFor 40 links                 -- the largest node at most 40 units
let laid = computeLayout links (defaultAbacusConfig worth)
laid.nodes <#> \node -> node.units <#> \unit -> … -- a square at (unit.x, unit.y)
laid.wires <#> wirePath                           -- a thin stroke

#computeLayout Source

computeLayout :: forall kind. Ord kind => Array (AbacusLink kind) -> AbacusConfig -> AbacusLayout kind

The abacus of links. See the module's own account for what it keeps of the Sankey's layout and for its laws.

#throughput Source

throughput :: forall kind. Ord kind => Composition -> Array (AbacusLink kind) -> Array (Throughput kind)

What passes through each node, in the order the links first name the nodes. A link from a node to itself counts once as coming in and once as going out, as it does in the Sankey.

#unitMarks Source

unitMarks :: forall kind. UnitWorth -> Array (KindQuantity kind) -> Array (UnitMark kind)

The units of a node whose kinds are byKind (the largest first, as throughput gives them), in order: every unit of the first kind, then every unit of the second, and so on. See the module's laws.

#niceWorth Source

niceWorth :: Int -> Number -> UnitWorth

The least worth of the form 1, 2 or 5 times a power of ten at which largest is no more than most units. niceWorth 40 337.0 is UnitWorth 10.0. With nothing to count (largest not above zero) it is 1.

#niceWorthFor Source

niceWorthFor :: forall kind. Ord kind => Int -> Array (AbacusLink kind) -> UnitWorth

niceWorth for the largest node of links: at that worth no node has more than most units.

#wireSegments Source

wireSegments :: forall kind. AbacusWire kind -> Array WireSegment

A wire's line, piece by piece. Every wire starts level, to the right side of its source's column. Onward: then one S across to its target's side. Laned: then an S across each space between columns and level through each lane, and a last S into its target's side. TurningBack: on to down, down to the channel at under, along it to up, up, and level into its target's side, each of the four turns rounded by turn.

#wirePath Source

wirePath :: forall kind. AbacusWire kind -> String

A wire's line as an SVG path, to be stroked thin.

Re-exports from DataViz.Layout.Abacus.Types

#WireSegment Source

data WireSegment

A piece of a wire's line: straight from one point to another, or a cubic curve from the first point to the last by two control points.

Constructors

Instances

#WireCourse Source

data WireCourse

How a wire runs. Every wire leaves the right side of its source at from and enters the left side of its target at to, and first runs level to the right side of its source's column.

Onward: from leaves, the right side of its source's column, across to its target, which is in the next column (or, with WithoutLanes, in any later one). Laned: its target is two or more columns on; from leaves it goes through a lane in each column it passes, in order, crossing from one column to the next in the space between them. TurningBack: it closes a cycle (the Sankey set it aside as a back-edge); it goes down at down, in the space after its source's column, back along a channel at under, below every node, and up at up, in the space before its target's column. turn: how far its four turns are rounded.

Constructors

Instances

#UnitWorth Source

newtype UnitWorth

What one unit is worth, in the measure of the links' quantities. It is the reader's choice, as a map's scale is; niceWorth gives a default.

Constructors

Instances

#UnitMark Source

data UnitMark kind

What a unit says. Whole: one unit's worth of a kind, carried into the node or arising in it. Part: an amount of that kind which is more than nothing and less than one unit; drawn smaller. Empty: nothing; drawn open.

Constructors

Instances

#Throughput Source

type Throughput kind = { byKind :: Array (KindQuantity kind), name :: String, total :: Number }

What passes through a node: total is the larger of what comes in and what goes out, and byKind is the total by kind and origin as the Composition shares it out, the largest first (of equal quantities, kinds in the order the links first name them, and carried before arising). The quantities of byKind sum to total.

#Tally Source

data Tally

Whether units are set in groups, so that many can still be counted. TalliedBy { every, gap }: after every every units, across and down, gap more is left than between two units of one group.

Constructors

Instances

#Point Source

type Point = { x :: Number, y :: Number }

#Origin Source

data Origin

Whether an amount came into a node (Carried) or first appears in it (Arising): see Composition.

Constructors

Instances

#Lanes Source

data Lanes

Whether a wire that passes over one or more columns is given a lane: a place kept for it in each column it passes, between the nodes there, so that it runs through no node and no name. gap is the room a lane takes in a column, top to bottom; the wire runs along its middle.

Constructors

Instances

#LaneSlot Source

type LaneSlot = { layer :: Int, x0 :: Number, x1 :: Number, y :: Number }

A lane's place in one column: the wire enters at (x0, y), the column's left side, and leaves at (x1, y), its right side.

#LabelRoom Source

type LabelRoom = { above :: Number, below :: Number }

Room kept clear around a node's units for what the caller writes there (a name, a sign): above the first line of units and below the last.

#KindQuantity Source

type KindQuantity kind = { kind :: kind, origin :: Origin, quantity :: Number }

#Extent Source

type Extent = { x0 :: Number, x1 :: Number, y0 :: Number, y1 :: Number }

#Composition Source

data Composition

Which kinds a node's units are, and which of them were carried in and which arose in the node, where what comes in and what goes out differ. The node's total is the larger of the two whichever is chosen.

  • IncomingAndArising (the default): what comes in, by kind, at its own amounts, Carried; if more goes out than comes in, the rest is what arises in the node: it is shared among the kinds of what goes out, in their proportions, and is Arising. So a node that nothing comes into (a source) is all Arising.
  • IncomingScaled: the kinds of what comes in, each scaled by total / incoming so that they sum to the total, all Carried; a node that nothing comes into has the kinds of what goes out, Arising.
  • LargerSide: the kinds of whichever side is larger (what comes in, when they are equal): Carried, but Arising where nothing comes in.

Constructors

Instances

#AbacusWire Source

type AbacusWire kind = { course :: WireCourse, from :: Point, kind :: kind, links :: Array Int, source :: String, target :: String, to :: Point }

A wire says only that two nodes are joined, and which way: there is one for each pair of different nodes that links of a quantity join in one direction, however many such links there are. links: their places in the input. kind: the kind of the largest of them. from, to: its two ends.

#AbacusUnit Source

type AbacusUnit kind = { mark :: UnitMark kind, size :: Number, x :: Number, y :: Number }

A unit's place: its top left corner and its side. A Part and an Empty have the same place as a Whole; drawing them smaller or open is the caller's.

#AbacusNode Source

type AbacusNode kind = { layer :: Int, name :: String, returnsToItself :: Boolean, total :: Number, units :: Array (AbacusUnit kind), x0 :: Number, x1 :: Number, y0 :: Number, y1 :: Number }

A node. Its box (x0, y0, x1, y1) is exactly the box around its units. layer: its column, as the Sankey numbers them. total: see Throughput. returnsToItself: some link of a quantity runs from this node to itself; it is drawn as a mark on the node and has no wire.

#AbacusLayout Source

type AbacusLayout kind = { extent :: Extent, nodes :: Array (AbacusNode kind), wires :: Array (AbacusWire kind), worth :: UnitWorth }

nodes: in the order of the Sankey's own nodes for the same rows, so that what is given by a node's place there (a colour, say) is the same in both pictures. wires: in the order the links first join each pair. worth: what a unit is worth, to be said beside the picture. extent: the box around everything, label room, lanes and the wires that turn back included; such a wire may run to the left of origin or to the right of the last column.

#AbacusConfig Source

type AbacusConfig = { backWireDip :: Number, backWireGap :: Number, composition :: Composition, labelRoom :: LabelRoom, lanes :: Lanes, layerGap :: Number, nodeGap :: Number, nodeMinWidth :: String -> Number, origin :: Point, sankey :: SankeyConfig, tally :: Tally, unitGap :: Number, unitSize :: Number, unitsPerLine :: Int, worth :: UnitWorth }

worth: what one unit is worth. unitsPerLine: how many units to a line before the next line starts. unitSize: a unit's side. unitGap: between one unit and the next, across and down. tally: grouping.

composition: see Composition. lanes: see Lanes.

nodeGap: between one node and the next below it (beyond the label room). layerGap: between the widest node of one column and the left of the next column. labelRoom: see LabelRoom. nodeMinWidth: a node's least width in its column, by name (room for its label); the units keep to the left of it. backWireDip: from the bottom of the lowest node (its label room counted) to the channel of the first wire that turns back, and the most a wire's turns are rounded by. backWireGap: from one such channel to the next.

origin: the top left corner of the picture.

sankey: the Sankey layout that says which column a node is in, the order of the nodes (and of the lanes) down a column, and which links turn back. Its alignment, nodeLayer and nodeSort mean here what they mean there. Its sizes (extent, nodeWidth, nodePadding) reach the abacus only through the order that the Sankey's relaxation settles on.

#turnsBack Source

turnsBack :: forall kind. AbacusWire kind -> Boolean

#ofOneKind Source

ofOneKind :: Array LinkCSVRow -> Array (AbacusLink Unit)

A Sankey's rows where nothing tells one flow from another.

#kindedBy Source

kindedBy :: forall kind. (LinkCSVRow -> kind) -> Array LinkCSVRow -> Array (AbacusLink kind)

A Sankey's rows, each given a kind.

#defaultAbacusConfig Source

defaultAbacusConfig :: UnitWorth -> AbacusConfig

Ten units to a line, each 4 square and 1 apart, in no groups; kinds by what comes in and what arises; lanes, each 6 deep; columns 20 apart, nodes 12 apart with no room kept for labels; the Sankey's own defaults for a chart 960 by 600.