Skip to Content
Onevra

Relationship graph

A force-directed graph of nodes and edges, panned, zoomed and dragged.

Loading the live example…

graph.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components/graph"
)

type notesGraph struct {
	state  graph.State // layout and camera, kept across frames
	opened string
}

var notesData = graph.Data{
	Nodes: []graph.Node{
		{ID: "index", Label: "Index", Openable: true, Size: 12},
		{ID: "ideas", Label: "Ideas", Group: "notes", Openable: true},
		{ID: "journal", Label: "Journal", Group: "notes", Openable: true},
		{ID: "reading", Label: "Reading list", Group: "notes"},
		{ID: "go", Label: "Go", Group: "topics"},
		{ID: "design", Label: "Design", Group: "topics"},
		{ID: "ui", Label: "Interfaces", Group: "topics"},
		{ID: "sketch", Label: "sketch.png", Group: "files", Size: 6},
		{ID: "talk", Label: "talk.pdf", Group: "files", Size: 6},
	},
	Edges: []graph.Edge{
		{Source: "index", Target: "ideas"}, {Source: "index", Target: "journal"},
		{Source: "index", Target: "reading"}, {Source: "ideas", Target: "go"},
		{Source: "ideas", Target: "design"}, {Source: "design", Target: "ui"},
		{Source: "go", Target: "ui"}, {Source: "ideas", Target: "sketch"},
		{Source: "reading", Target: "talk"}, {Source: "journal", Target: "design"},
	},
}

func (n *notesGraph) View(c *ui.Context) {
	// Drag nodes, wheel or pinch to zoom, and double-click an openable node.
	graph.View(c, &n.state, notesData, graph.Options{
		Label: "Notes",
		Open:  func(id string) { n.opened = id },
	})
}
Source

graph.View lays out nodes with forces and keeps the layout and camera in a graph.State. Nodes need stable IDs; groups seed their placement and color. Drag nodes, use the wheel or a pinch to zoom, and double-click an openable node to open it. Hovering a node highlights its neighbors.

API

View

func View(c *ui.Context, state *State, data Data, opts Options) ui.Element

View builds the graph. Drag empty space or two fingers on a touchpad to pan, drag nodes to move them, and use the wheel or a pinch to zoom around the pointer; double-clicking empty space zooms in, or out with Shift.

pkg.go.dev

Data

Data is borrowed for the frame and never modified by the view.

Prop

Type

pkg.go.dev

Node

Node is one vertex. ID must be nonempty, unique and stable across frames; later duplicates are ignored. Group places related nodes near each other initially and colors them by group; an empty Group uses the palette's neutral node color, or its isolated color for a node without edges. Weight sizes the node (zero uses its edge count) and Size, when positive, sets its diameter instead. Nonzero Color and LabelColor override the palette. Openable nodes take double-clicks, reported to Options.Open.

Prop

Type

pkg.go.dev

Edge

Edge joins two node IDs. Edges to unknown nodes and self-loops are ignored.

Prop

Type

pkg.go.dev

Options

Options supplies presentation and application-owned content.

Label is the accessible description (a node and edge count by default). Height sets a fixed height; zero fills the parent. Nil Palette uses DefaultPalette and nil GroupColor uses DefaultGroupColor; both are re-evaluated only when the data, theme or palette change. HoverDepth is how many edges away from the hovered node stay emphasized (default 1).

Open is called on the UI thread, during input handling, when an Openable node is double-clicked; a frame follows. Empty replaces the view when there are no nodes (EmptyText, default "—", otherwise). Overlay builds content in the top trailing corner above the graph, such as view settings.

Prop

Type

pkg.go.dev

State

type State struct {
	// contains filtered or unexported fields
}

State retains layout, camera and hover between frames. Keep one per graph; 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 graph.

func (s *State) Hovered() string

Hovered returns the ID of the node under the pointer, or "".

func (s *State) Settled() bool

Settled reports whether the layout has come to rest.

func (s *State) Zoom() float32

Zoom returns the camera's scale.

pkg.go.dev

DefaultPalette

func DefaultPalette(dark bool) Palette

DefaultPalette returns the built-in light or dark palette.

pkg.go.dev

DefaultGroupColor

func DefaultGroupColor(group string, index, count int, dark bool) (fill, label ui.Color)

DefaultGroupColor spaces group hues evenly around the color wheel, so any two groups stay distinguishable, and varies saturation and lightness by name.

pkg.go.dev
Edit this page on GitHub

On this page