Documentation
¶
Overview ¶
Package graph holds the graph widgets of the Tango self-drawn UI: a node-link diagram canvas (Diagram), an entity-relationship preset built on it (ER) and a git commit graph (GitGraph).
A Diagram draws an application-owned *Graph: nodes (built-in templates or a custom painter) joined by straight, orthogonal or Bézier edges with arrows, labels and port anchors. The canvas pans (drag the background, Space+drag, the wheel, two-finger touch), zooms (Ctrl/⌘+wheel, pinch, toolbar buttons), selects (click, Shift/⌘+click, Shift+drag marquee), drags nodes (optionally snapped to a grid) and shows a minimap. Only the visible part of the graph is drawn, so graphs of thousands of nodes stay smooth.
g := &graph.Graph{Nodes: nodes, Edges: edges}
v := graph.Diagram(c, g).Height(480).Grid(16).Snap().MiniMap().AutoLayout(graph.LayoutOptions{}).Show()
if id, ok := v.DoubleClicked(); ok { open(id) }
Layout computes a layered (Sugiyama-style) placement: cycle removal, longest-path layering, barycentric crossing reduction and an iterative coordinate assignment; edges spanning several layers get bend points.
graph.Layout(g, graph.LayoutOptions{Direction: graph.LeftRight})
ER draws database tables (a name header and one row per column with its type and primary / foreign key marks) and connects foreign keys from the referencing column row to the referenced one:
graph.ER(c, tables, relations).Height(560).Show()
GitGraph lays commits out in lanes (LayoutCommits: branch lines, merge curves, per-branch colours) and shows them in a virtual list with ref badges, author, date and hash columns, hover and selection. Only the rows in view are built, so histories of tens of thousands of commits scroll smoothly.
graph.GitGraph(c, commits).Selected(&hash).Height(420).Show()
Geometry: the diagram keeps its left-to-right geometry in right-to-left scopes (a diagram's coordinates are data, like a chart's), while the toolbar and minimap move to the mirrored corners; GitGraph mirrors its columns with the scope like any list.
Experimental: this package's API may change in any 0.x release (see docs/release.md, "API 稳定性").
Index ¶
- func Layout(g *Graph, o LayoutOptions)
- type Arrow
- type Column
- type Commit
- type DiagramView
- func (v *DiagramView) CenterOn(id string)
- func (v *DiagramView) Clicked() (string, bool)
- func (v *DiagramView) ContextMenu() (node string, pos geom.Point, ok bool)
- func (v *DiagramView) DeleteRequested() bool
- func (v *DiagramView) DoubleClicked() (string, bool)
- func (v *DiagramView) EdgeClicked() (int, bool)
- func (v *DiagramView) FitView()
- func (v *DiagramView) Hovered() (string, bool)
- func (v *DiagramView) HoveredEdge() (int, bool)
- func (v *DiagramView) Moved() bool
- func (v *DiagramView) ScreenRect(id string) (geom.Rect, bool)
- func (v *DiagramView) Select(ids ...string)
- func (v *DiagramView) Selected() []string
- func (v *DiagramView) SelectedEdges() []int
- func (v *DiagramView) Zoom() float32
- func (v *DiagramView) ZoomTo(k float32)
- type DiagramW
- func (w *DiagramW) A11yLabel(s string) *DiagramW
- func (w *DiagramW) AutoLayout(o LayoutOptions) *DiagramW
- func (w *DiagramW) Edges(k EdgeKind) *DiagramW
- func (w *DiagramW) Grid(size float32) *DiagramW
- func (w *DiagramW) Height(h float32) *DiagramW
- func (w *DiagramW) Key(k any) *DiagramW
- func (w *DiagramW) Layout(o ...layout.Option) *DiagramW
- func (w *DiagramW) MarqueeDrag() *DiagramW
- func (w *DiagramW) MiniMap() *DiagramW
- func (w *DiagramW) NoToolbar() *DiagramW
- func (w *DiagramW) ReadOnly() *DiagramW
- func (w *DiagramW) Selection(ids *[]string) *DiagramW
- func (w *DiagramW) Show() *DiagramView
- func (w *DiagramW) Sides(s Side) *DiagramW
- func (w *DiagramW) Snap() *DiagramW
- func (w *DiagramW) Style(fn func(*Style)) *DiagramW
- func (w *DiagramW) ZoomRange(lo, hi float32) *DiagramW
- type Direction
- type Edge
- type EdgeKind
- type GitColumns
- type GitGraphView
- type GitGraphW
- func (w *GitGraphW) A11yLabel(s string) *GitGraphW
- func (w *GitGraphW) Columns(cols GitColumns) *GitGraphW
- func (w *GitGraphW) Height(h float32) *GitGraphW
- func (w *GitGraphW) Key(k any) *GitGraphW
- func (w *GitGraphW) LaneWidth(px float32, maxLanes int) *GitGraphW
- func (w *GitGraphW) Layout(o ...layout.Option) *GitGraphW
- func (w *GitGraphW) RowHeight(h float32) *GitGraphW
- func (w *GitGraphW) Selected(hash *string) *GitGraphW
- func (w *GitGraphW) Show() *GitGraphView
- func (w *GitGraphW) Style(fn func(*Style)) *GitGraphW
- type GitLayout
- type GitLine
- type GitRow
- type Graph
- type LayoutOptions
- type LinePart
- type Node
- type NodePainter
- type NodeState
- type Port
- type Ref
- type RefKind
- type Relation
- type Shape
- type Side
- type Style
- type Table
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Layout ¶
func Layout(g *Graph, o LayoutOptions)
Layout places every node of g with a layered (Sugiyama-style) layout and sets the bend points of edges that span several ranks:
- cycles are broken by reversing DFS back edges;
- nodes get the longest-path rank, sources pulled next to their successors;
- long edges are split by virtual nodes, one per crossed rank;
- barycentric down / up sweeps order each rank, keeping the order with the fewest crossings;
- positions across the ranks are refined towards the neighbours' average under the minimum-gap constraints (an exact weighted isotonic fit per rank), which straightens long edges.
The result's top-left is at the origin. Nodes with a zero Size get o.DefaultSize. Self loops and edges to missing nodes are ignored.
Types ¶
type Column ¶
type Column struct {
Name string
Type string
PK bool // part of the primary key
FK bool // references another table (set automatically for Relation.FromColumn)
}
Column is one column of a Table.
type Commit ¶
type Commit struct {
Hash string
Parents []string
Refs []Ref
Author string
Time time.Time
Subject string
}
Commit is one entry of a GitGraph, newest first (git log order: every commit above its parents).
type DiagramView ¶
DiagramView is a shown diagram: its box plus what happened this frame. The action methods (FitView, ZoomTo, CenterOn, Select) take effect on the next frame.
func (*DiagramView) CenterOn ¶
func (v *DiagramView) CenterOn(id string)
CenterOn pans so node id is in the middle of the canvas.
func (*DiagramView) Clicked ¶
func (v *DiagramView) Clicked() (string, bool)
Clicked returns the node clicked (pressed and released without dragging) this frame.
func (*DiagramView) ContextMenu ¶
func (v *DiagramView) ContextMenu() (node string, pos geom.Point, ok bool)
ContextMenu reports a secondary click this frame: the node under it (empty for the background) and the window position.
func (*DiagramView) DeleteRequested ¶
func (v *DiagramView) DeleteRequested() bool
DeleteRequested reports Delete / Backspace with a selection this frame; removing the selected nodes and edges is up to the application.
func (*DiagramView) DoubleClicked ¶
func (v *DiagramView) DoubleClicked() (string, bool)
DoubleClicked returns the node double-clicked this frame.
func (*DiagramView) EdgeClicked ¶
func (v *DiagramView) EdgeClicked() (int, bool)
EdgeClicked returns the index of the edge clicked this frame.
func (*DiagramView) FitView ¶
func (v *DiagramView) FitView()
FitView fits the whole graph into the canvas.
func (*DiagramView) Hovered ¶
func (v *DiagramView) Hovered() (string, bool)
Hovered returns the node under the pointer.
func (*DiagramView) HoveredEdge ¶
func (v *DiagramView) HoveredEdge() (int, bool)
HoveredEdge returns the index of the edge under the pointer.
func (*DiagramView) Moved ¶
func (v *DiagramView) Moved() bool
Moved reports whether nodes were dragged or moved by keys this frame.
func (*DiagramView) ScreenRect ¶
func (v *DiagramView) ScreenRect(id string) (geom.Rect, bool)
ScreenRect returns where node id was drawn in the last frame (window coordinates), for anchoring popovers and menus.
func (*DiagramView) Select ¶
func (v *DiagramView) Select(ids ...string)
Select replaces the node selection.
func (*DiagramView) Selected ¶
func (v *DiagramView) Selected() []string
Selected returns the selected node IDs in graph order.
func (*DiagramView) SelectedEdges ¶
func (v *DiagramView) SelectedEdges() []int
SelectedEdges returns the indices of the selected edges.
func (*DiagramView) Zoom ¶
func (v *DiagramView) Zoom() float32
Zoom returns the zoom factor (screen pixels per graph unit).
func (*DiagramView) ZoomTo ¶
func (v *DiagramView) ZoomTo(k float32)
ZoomTo sets the zoom factor about the canvas centre.
type DiagramW ¶
type DiagramW struct {
// contains filtered or unexported fields
}
DiagramW builds a node-link diagram canvas; configure it with the modifiers and finish with Show.
func Diagram ¶
Diagram starts a diagram of g. The graph is owned by the caller; the diagram writes node positions (dragging, AutoLayout, keyboard moves) and the measured sizes of nodes with a zero Size back into it.
func ER ¶
ER starts an entity-relationship diagram of tables. Each table is a node with a name header and one row per column (key marks, name, type); relations run from the referencing column row to the referenced one. The graph is kept between frames and laid out left to right whenever the tables or relations change; dragged positions are kept otherwise. All Diagram modifiers apply (Height, MiniMap, Edges ...); the selection holds table names.
func (*DiagramW) A11yLabel ¶
A11yLabel sets the accessible name of the canvas (default Locale.Graph.Diagram).
func (*DiagramW) AutoLayout ¶
func (w *DiagramW) AutoLayout(o LayoutOptions) *DiagramW
AutoLayout lays the graph out with Layout whenever its set of nodes and edges changes (and on the first Show), then fits the view. The toolbar gets a button that lays it out again.
func (*DiagramW) Height ¶
Height sets the canvas height (default 480; the width follows the parent).
func (*DiagramW) Key ¶
Key gives the diagram a stable identity (viewport, selection) when widgets are added or removed around it.
func (*DiagramW) MarqueeDrag ¶
MarqueeDrag makes a drag on the background draw a selection marquee (Space+drag or a two-finger drag pans). By default the background drag pans and Shift+drag draws the marquee.
func (*DiagramW) MiniMap ¶
MiniMap shows an overview of the whole graph in the bottom end corner; pressing or dragging in it moves the view.
func (*DiagramW) ReadOnly ¶
ReadOnly disables node dragging and keyboard moves (panning, zooming and selection still work).
func (*DiagramW) Selection ¶
Selection binds the selected node IDs to *ids: the diagram reads it every frame and writes it on change.
func (*DiagramW) Show ¶
func (w *DiagramW) Show() *DiagramView
Show builds the diagram and handles this frame's input.
func (*DiagramW) Sides ¶
Sides sets where edge ends without a fixed port attach: SideHorizontal (left / right), SideVertical (top / bottom) or SideAuto (whichever side faces the other end). The default follows the AutoLayout direction.
type Direction ¶
type Direction uint8
Direction is the flow direction of a layered layout: the side ranks follow each other towards.
type Edge ¶
type Edge struct {
ID string // optional; used for edge selection (defaults to "From->To#index")
From, To string
FromPort string
ToPort string
Label string
Kind EdgeKind
Arrow Arrow
Dashed bool
Status string // accent colour like Node.Status
// Bends are intermediate points in graph units. Layout sets them for
// edges that span several ranks; dragging an end node clears them.
// Orthogonal edges jog through them along the flow.
Bends []geom.Point
Data any
}
Edge joins two nodes, optionally at named ports.
type GitColumns ¶
type GitColumns uint8
GitColumns selects the text columns of a GitGraph besides the graph and the subject.
const ( ColAuthor GitColumns = 1 << iota ColDate ColHash ColAll = ColAuthor | ColDate | ColHash )
type GitGraphView ¶
GitGraphView is a shown commit graph and what happened this frame.
func (*GitGraphView) Clicked ¶
func (v *GitGraphView) Clicked() (Commit, bool)
Clicked returns the commit clicked this frame.
func (*GitGraphView) DoubleClicked ¶
func (v *GitGraphView) DoubleClicked() (Commit, bool)
DoubleClicked returns the commit double-clicked this frame.
func (*GitGraphView) Hovered ¶
func (v *GitGraphView) Hovered() (Commit, bool)
Hovered returns the commit under the pointer.
func (*GitGraphView) Layout ¶
func (v *GitGraphView) Layout() *GitLayout
Layout returns the lane layout of the shown commits.
func (*GitGraphView) ScrollTo ¶
func (v *GitGraphView) ScrollTo(hash string)
ScrollTo scrolls commit hash into view on the next frame.
type GitGraphW ¶
type GitGraphW struct {
// contains filtered or unexported fields
}
GitGraphW builds a commit graph list; configure it with the modifiers and finish with Show.
func GitGraph ¶
GitGraph starts a commit graph of commits (newest first, every commit above its parents, as git log prints them).
func (*GitGraphW) Columns ¶
func (w *GitGraphW) Columns(cols GitColumns) *GitGraphW
Columns chooses the text columns (default ColAll).
func (*GitGraphW) LaneWidth ¶
LaneWidth sets the lane spacing (default 16) and how many lanes are shown before the graph column is clipped (default 12).
func (*GitGraphW) Show ¶
func (w *GitGraphW) Show() *GitGraphView
Show builds the list and handles this frame's input.
type GitLayout ¶
GitLayout is the lane layout of a commit list.
func LayoutCommits ¶
LayoutCommits assigns lanes to commits (newest first) the way git log --graph does: a commit takes the lane that waits for it (the leftmost if several lines meet there; the others end in it), its first parent continues that lane, further parents join the lane already waiting for them or get a free one, and lanes are reused once free. Each new branch line gets the next colour. Parents that are not in the list end their line at the bottom of the row. It runs in O(commits × lanes).
type GitRow ¶
type GitRow struct {
Lane int // lane of the commit dot
Color int
Merge bool // more than one parent
Lines []GitLine
}
GitRow is the graph cell of one commit.
type Graph ¶
Graph is the data a Diagram draws. The application owns it; the diagram writes node positions (dragging, auto layout) and measured sizes back.
type LayoutOptions ¶
type LayoutOptions struct {
Direction Direction
RankSep float32 // gap between ranks in graph units (default 64)
NodeSep float32 // gap between neighbouring nodes of a rank (default 32)
// Sweeps is the number of crossing-reduction sweeps (each one down
// and one up; default 12).
Sweeps int
// DefaultSize is used for nodes whose Size is zero (default 140×40).
DefaultSize geom.Size
}
LayoutOptions configure Layout.
type Node ¶
type Node struct {
ID string
Label string
Detail string // second line of the built-in template
Icon *icon.Icon // leading icon of the built-in template
Status string // "success" | "warning" | "error" | "info" | "processing": accent colour
Shape Shape
Pos geom.Point // top-left corner
Size geom.Size // zero: measured from the label by the built-in template (stored back on Show)
Ports []Port
// Draw replaces the built-in template. The selection outline is still
// drawn around the node.
Draw NodePainter
// Fixed nodes cannot be dragged (they can still be selected).
Fixed bool
Data any
}
Node is one vertex of a Graph. Positions and sizes are in graph units (one unit is one logical pixel at zoom 1).
type NodePainter ¶
NodePainter draws a node in screen space: r is the node box already scaled by s.Zoom, so sizes in graph units multiply by s.Zoom.
type NodeState ¶
type NodeState struct {
Zoom float32 // screen pixels per graph unit
Selected bool
Hovered bool
Theme *theme.Theme
Style *Style
}
NodeState is what a node painter needs besides its rectangle.
type Port ¶
type Port struct {
ID string
Side Side
// Offset is the distance in graph units from the start of the side
// (its left end for top / bottom, its top for left / right); zero or
// less means the middle of the side.
Offset float32
}
Port is a named anchor on a node's border that edges attach to.
type Side ¶
type Side uint8
Side is the side of a node a port (or an edge end) sits on.
const ( SideAuto Side = iota // the side facing the other end of the edge SideTop // top edge, Offset from the left SideRight // right edge, Offset from the top SideBottom // bottom edge, Offset from the left SideLeft // left edge, Offset from the top SideHorizontal // left or right, whichever faces the other end; Offset from the top SideVertical // top or bottom, whichever faces the other end; Offset from the left )
type Style ¶
type Style struct {
Canvas theme.Color // diagram background
GridDot theme.Color // background grid dots
NodeBg theme.Color
NodeBorder theme.Color
NodeText theme.Color
NodeDetail theme.Color // second line, ER column types
NodeRadius float32
NodeShadow []paint.Shadow
Font float32 // node label size at zoom 1
FontSM float32 // detail / column / label size at zoom 1
Selection theme.Color // outline of selected nodes and edges
Hover theme.Color // border of the hovered node
Edge theme.Color
EdgeWidth float32 // screen pixels
LabelBg theme.Color
LabelText theme.Color
Marquee theme.Color // marquee fill
MarqueeBorder theme.Color
MiniBg theme.Color
MiniBorder theme.Color
MiniNode theme.Color
MiniView theme.Color // visible-area fill
MiniViewBorder theme.Color
ToolbarBg theme.Color
ToolbarShadow []paint.Shadow
// ER tables.
HeaderBg theme.Color
HeaderText theme.Color
RowHover theme.Color
KeyPK theme.Color // primary-key mark
KeyFK theme.Color // foreign-key mark
// GitGraph.
Lanes []theme.Color // branch colours, cycled
RowSel theme.Color // selected row background
RowSelText theme.Color
Hash theme.Color
Subject theme.Color
Meta theme.Color // author / date
}
Style holds the colours and metrics of the graph widgets. StyleOf derives it from the theme tokens, so it follows the design system and its dark variant; tweak it per widget with the Style modifiers.