Module

DataViz.Layout.StateMachine

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

DataViz.Layout.StateMachine

State machine layout for visualization. Provides types and layout algorithms for rendering state machine diagrams with states as circles/ovals and transitions as curved arrows.

Example usage:

import DataViz.Layout.StateMachine (layout, StateMachine)

myMachine :: StateMachine Unit Unit
myMachine =
  { states:
      [ { id: "s0", label: "Start", isInitial: true, isFinal: false, extra: unit }
      , { id: "s1", label: "Running", isInitial: false, isFinal: false, extra: unit }
      , { id: "s2", label: "Done", isInitial: false, isFinal: true, extra: unit }
      ]
  , transitions:
      [ { from: "s0", to: "s1", label: "begin", extra: unit }
      , { from: "s1", to: "s1", label: "tick", extra: unit }
      , { from: "s1", to: "s2", label: "finish", extra: unit }
      ]
  }

rendered = layout myMachine
-- rendered.states contains positioned states
-- rendered.transitions contains arrow paths

Re-exports from DataViz.Layout.StateMachine.Layout

#LayoutConfig Source

type LayoutConfig = { arrowOffset :: Number, edgeCurvature :: Number, initialArrowLength :: Number, labelOffset :: Number, layoutRadius :: Number, margin :: Number, minPairedCurvature :: Number, parallelSeparation :: Number, selfLoopRadius :: Number, stateRadiusX :: Number, stateRadiusY :: Number }

Configuration for state machine layout

#layoutWithConfig Source

layoutWithConfig :: forall se te. LayoutConfig -> (LayoutConfig -> Array (State se) -> Array (LayoutState se)) -> StateMachine se te -> StateMachineLayout se te

Layout with custom configuration and layout strategy

#layout Source

layout :: forall se te. StateMachine se te -> StateMachineLayout se te

Layout a state machine with default configuration using circular layout

#gridLayout Source

gridLayout :: forall extra. LayoutConfig -> Array (State extra) -> Array (LayoutState extra)

Grid layout: arrange states in rows

#defaultConfig Source

defaultConfig :: LayoutConfig

Default layout configuration

#circularLayout Source

circularLayout :: forall extra. LayoutConfig -> Array (State extra) -> Array (LayoutState extra)

Circular layout: arrange states in a circle

Re-exports from DataViz.Layout.StateMachine.Path

#transitionPathD Source

transitionPathD :: TransitionPath -> String

Generate SVG path d attribute for a transition arrow Uses quadratic bezier curve

#stateFinalRing Source

stateFinalRing :: StatePosition -> Number -> { cx :: Number, cy :: Number, rx :: Number, ry :: Number }

Get the inner ring for a final state (double circle effect) Returns slightly smaller ellipse attributes

#stateEllipse Source

stateEllipse :: StatePosition -> { cx :: Number, cy :: Number, rx :: Number, ry :: Number }

Get ellipse attributes for a state Returns { cx, cy, rx, ry } ready for SVG ellipse element

#selfLoopPathD Source

selfLoopPathD :: TransitionPath -> String

Generate SVG path for a self-loop Uses an SVG arc for a clean circular loop The arc radius is computed from the distance between start and control points

#initialArrowPathD Source

initialArrowPathD :: { angle :: Number, x :: Number, y :: Number } -> Number -> String

Generate SVG path for the initial state arrow A simple horizontal arrow pointing right

#arrowheadPathD Source

arrowheadPathD :: Number -> Number -> Number -> Number -> String

Generate SVG path for an arrowhead at the end of a transition Creates a simple triangle pointing in the direction of the arrow

Re-exports from DataViz.Layout.StateMachine.Tree

#treeStrategy Source

treeStrategy :: forall extra. Induced String -> LayoutConfig -> Array (State extra) -> Array (LayoutState extra)

A positioning pass for layoutWithConfig, driven by an induced tree.

The tree is keyed by state id, because that is what State carries and what the caller will have built the graph from.

Re-exports from DataViz.Layout.StateMachine.Types

#TransitionPath Source

type TransitionPath = { angle :: Number, controlX :: Number, controlY :: Number, endX :: Number, endY :: Number, isSelfLoop :: Boolean, labelX :: Number, labelY :: Number, startX :: Number, startY :: Number }

Computed path for a transition arrow Uses quadratic bezier for curved arrows

#Transition Source

type Transition extra = { extra :: extra, from :: String, label :: String, to :: String }

A transition between states The extra field carries whatever the caller needs to keep attached to the edge itself — the triggering event, a guard, a refusal reason — so that a renderer can style or annotate an arrow without a side table keyed by from/to/label.

#StatePosition Source

type StatePosition = { cx :: Number, cy :: Number, rx :: Number, ry :: Number }

Computed position for a state

#StateMachineLayout Source

type StateMachineLayout stateExtra transitionExtra = { height :: Number, initialArrow :: { angle :: Number, x :: Number, y :: Number }, originX :: Number, originY :: Number, states :: Array (LayoutState stateExtra), transitions :: Array (LayoutTransition transitionExtra), width :: Number }

Complete layout output ready for rendering originX/originY are the top-left of the drawing, which is NOT always (0, 0): a self-loop or a transition label can sit left of or above the leftmost state, and a viewBox pinned to the origin clips them. Renderers should use all four as the viewBox.

#StateMachine Source

type StateMachine stateExtra transitionExtra = { states :: Array (State stateExtra), transitions :: Array (Transition transitionExtra) }

Complete state machine definition

#State Source

type State extra = { extra :: extra, id :: String, isFinal :: Boolean, isInitial :: Boolean, label :: String }

A state in the state machine The extra field allows attaching arbitrary data (e.g., phantom type info)

#LayoutTransition Source

type LayoutTransition extra = { path :: TransitionPath, transition :: Transition extra }

A transition with computed layout

#LayoutState Source

type LayoutState extra = { position :: StatePosition, state :: State extra }

A state with computed layout