Flow diagram
Directed diagrams laid out in ranks, with orthogonal edges and card nodes.
Loading the live example…
import (
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components/flow"
)
type projectPlan struct {
state flow.State // layout and camera
selected string
}
func (p *projectPlan) View(c *ui.Context) {
card := func(title, detail string) func(*ui.Context) {
return func(c *ui.Context) {
flow.Card(c, flow.CardOptions{Title: title, Description: detail, Width: 200})
}
}
data := flow.Data{
Nodes: []flow.Node{
{ID: "survey", Label: "Survey", Content: card("Survey", "Collect requirements")},
{ID: "design", Label: "Design", Content: card("Design", "Sketch the approach")},
{ID: "prototype", Label: "Prototype", Content: card("Prototype", "Try the riskiest part")},
{ID: "review", Label: "Review", Content: card("Review", "Decide what ships")},
{ID: "ship", Label: "Ship", Content: card("Ship", "Release and announce")},
},
Edges: []flow.Edge{
{Source: "survey", Target: "design"},
{Source: "survey", Target: "prototype", Dashed: true, Label: "informs"},
{Source: "design", Target: "review"},
{Source: "prototype", Target: "review"},
{Source: "review", Target: "ship"},
},
}
flow.View(c, &p.state, data, flow.Options{
Label: "Project plan",
Selected: p.selected,
Select: func(id string) { p.selected = id },
})
}flow.View ranks nodes so edges stay short, orders each rank to reduce crossings and routes edges between ranks. Nodes build their own content, or use flow.Card, whose regions are slots. Click a node to select it; the application owns selection and opening.
API
View
func View(c *ui.Context, state *State, data Data, opts Options) ui.ElementView builds the diagram. Drag empty space or two fingers on a touchpad to pan, use the wheel or a pinch to zoom around the pointer, and double-click empty space to zoom in, or out with Shift. Nodes take the keyboard focus in data order when the application can select or open them, and the camera follows the focused node.
pkg.go.devData
Node
Node is one card of the diagram. ID must be nonempty, unique and stable across frames; later duplicates are ignored. Label is its accessible name. Content builds the card; Card provides a styled default.
Positive Width and Height fix the node's size; otherwise the layout measures what Content builds, laying the diagram out again when that size changes. Muted fades the node and its edges, as for skipped or inactive steps.
Prop
Type
Edge
Edge points from Source to Target. Edges to unknown nodes and self-loops are ignored; edges closing a cycle are drawn against the layout's direction. Label, when set, sits on the line. Zero Color and Width use the theme's soft line, 1.5 DIPs wide.
Prop
Type
Options
Options supplies presentation and application-owned behavior.
Label is the accessible description (a node and edge count by default). Height sets a fixed height; zero fills the parent. NodeGap separates nodes within a rank (default 40) and RankGap the ranks (default 72).
Selected is the application's selected node, which the view rings and whose edges it emphasizes. Select is called on the UI thread when a node is clicked or activated from the keyboard, and with "" when empty space is clicked; Open when a node is double-clicked. Empty replaces the view when there are no nodes (EmptyText, default "—", otherwise). Overlay builds content in the top trailing corner above the diagram, such as zoom controls.
Prop
Type
Card
func Card(c *ui.Context, opts CardOptions) ui.ElementCard builds the default node face for Node.Content: a softly shadowed, rounded surface with optional header, leading, body, trailing and footer content.
pkg.go.devCardOptions
CardOptions describes a Card; every region is a slot. Width defaults to 240 DIPs. Header builds a small row at the top, such as a status; Leading builds content before the body, such as an icon tile; Body builds the main content, or else Title and Description show, each on one line; Trailing builds content after the body, such as a badge or menu; Footer builds a small muted row at the bottom, such as counts. A nonzero Border replaces the theme's soft border color, as for a status.
Prop
Type
State
type State struct {
// contains filtered or unexported fields
}State retains measured sizes, layout and camera between frames. Keep one per diagram; its zero value is ready to use. Nil state uses element-local storage.
Methods
func (s *State) Fit()Fit moves the camera to show the whole diagram.
func (s *State) Position(id string) (x, y float32, ok bool)Position returns the top left corner of the node with id in diagram coordinates, and false if it is not laid out.
func (s *State) Ready() boolReady reports whether every node has been measured, laid out and shown.
func (s *State) Reveal(id string)Reveal moves the camera to center the node with id, if it is shown.
func (s *State) Zoom() float32Zoom returns the camera's scale.
func (s *State) ZoomIn()ZoomIn and ZoomOut scale the view around its center.
func (s *State) ZoomOut()