Skip to content

NodeGraph

KeyAction
/ kPan up
/ jPan down
/ hPan left
/ lPan right
EnterSelect the focused node (fires onSelect)
cCenter viewport on the focused node

The focused node is NodeGraphData.FocusID. Set it before calling SetData to control which node the viewport centers on initially.

SetData computes the layout immediately and sets a needsCenter flag — the viewport centers on the first draw when terminal dimensions are available. Use SetFit(true) to center on the bounding box of all nodes instead of the focused node.

NodeGraph preview

NodeGraph is a 2D node graph visualization component.

Constructor func NewNodeGraph() *NodeGraph

Usage

data := components.NewNodeGraphData()
data.AddNode(&components.GraphNode{ID: "api", Label: "API", Status: "running"})
data.AddNode(&components.GraphNode{ID: "db", Label: "Database", Status: "running"})
data.AddEdge(&components.GraphEdge{From: "api", To: "db", Label: "queries"})
data.FocusID = "api"
graph := components.NewNodeGraph().
SetData(data).
SetShowEdgeLabels(true).
SetFit(true)
fmt.Println(graph != nil)

Methods

Method Signature Description
SetData func (g *NodeGraph) SetData(data *NodeGraphData) *NodeGraph SetData sets the graph data and triggers layout computation. The viewport centers on the first draw after this call — on FocusID if set, or on the bounding box of all nodes if SetFit(true) was called.
SetFit func (g *NodeGraph) SetFit(fit bool) *NodeGraph SetFit controls the initial viewport centering strategy. When true, the viewport centers on the bounding box of all nodes. When false (default), it centers on NodeGraphData.FocusID instead.
SetFocus func (g *NodeGraph) SetFocus(nodeID string) SetFocus sets the focused node.
SetNodeWidth func (g *NodeGraph) SetNodeWidth(width int) *NodeGraph SetNodeWidth sets the width of nodes.
SetOnSelect func (g *NodeGraph) SetOnSelect(fn func(node *GraphNode)) *NodeGraph SetOnSelect sets the callback for node selection.
SetShowEdgeLabels func (g *NodeGraph) SetShowEdgeLabels(show bool) *NodeGraph SetShowEdgeLabels enables/disables edge label display.

Types

type GraphNode struct GraphNode represents a node in the 2D node graph.
FieldTypeDescription
ID string
Label string
Status string For status-based coloring
Focused bool Whether this is the focused/primary node
Data any Associated data
type NodeGraphData struct NodeGraphData holds the data for a NodeGraph.
FieldTypeDescription
FocusID string ID of the focused node