render

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package render lays out gonx graphs and writes them as self-contained SVG images. It exists so the examples in examples/ can regenerate the README gallery with zero dependencies beyond the standard library; it is NOT part of the gonx API (serialization and rendering are out of scope for the library).

Index

Constants

View Source
const (
	Bg     = "#0d1117" // canvas (GitHub-dark)
	Ink    = "#e6edf3" // light text
	Indigo = "#6366f1"
	Cyan   = "#22d3ee"
	Amber  = "#fbbf24"
	Rose   = "#fb7185"
)

Shared palette so the gallery images read as one set.

Variables

This section is empty.

Functions

func EdgeAlpha

func EdgeAlpha(a float64) string

EdgeAlpha returns the standard muted edge gray at the given opacity.

func Jitter

func Jitter(pos []Point, amount float64, r *rand.Rand)

Jitter nudges each position by up to ±amount in both axes, deterministically. Useful to break symmetry in hand-seeded layouts before calling Relax.

func Ramp

func Ramp(t float64, stops ...string) string

Ramp linearly interpolates through the given "#rrggbb" color stops at t in [0, 1]. With three stops, t = 0.5 lands exactly on the middle one.

func Relax

func Relax(g *gonx.Graph, pos []Point, iters int)

Relax refines pos in place with the Fruchterman-Reingold force model: all pairs repel with k^2/d, edges attract with d^2/k, a mild gravity pulls toward the centroid, and a linearly cooling temperature caps each step. O(iters * n^2); intended for the few-hundred-node graphs in the gallery.

func SVG

func SVG(g *gonx.Graph, pos []Point, st Style) string

SVG renders the graph at the given positions into an SVG document string. Positions are fitted to the canvas preserving aspect ratio and centered.

func WriteSVG

func WriteSVG(path string, g *gonx.Graph, pos []Point, st Style) error

WriteSVG renders the graph and writes it to path, creating parent directories.

Types

type Point

type Point struct{ X, Y float64 }

Point is a node position in layout space (arbitrary units; SVG fits it later).

func CircleLayout

func CircleLayout(n int) []Point

CircleLayout places n nodes evenly on a unit circle, node 0 at twelve o'clock, proceeding clockwise so ascending IDs read like a clock face.

func ForceLayout

func ForceLayout(g *gonx.Graph, seed uint64, iters int) []Point

ForceLayout computes a Fruchterman-Reingold layout from deterministic random initial positions. Same graph + seed + iters always yields the same picture.

func RandomLayout

func RandomLayout(n int, seed uint64) []Point

RandomLayout scatters n nodes deterministically in the unit square.

type Style

type Style struct {
	Width, Height int     // canvas size in px (default 1600x1000)
	Pad           float64 // inner margin in px (default 70)
	Background    string  // default Bg

	NodeFill   func(u int) string  // default Indigo
	NodeRadius func(u int) float64 // default 8
	NodeStroke string              // ring separating overlapping nodes; default Bg

	EdgeStroke func(u, v int) string  // default EdgeAlpha(0.30)
	EdgeWidth  func(u, v int) float64 // default 1.4

	Label     func(u int) string // empty string = no label (default: no labels)
	LabelFill func(u int) string // default Bg (dark text on bright nodes)
}

Style controls how a graph is drawn. Per-node and per-edge callbacks may be nil, in which case sensible defaults from the shared palette are used.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL