linechart

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package linechart provides a real-time line chart widget for visualizing time-series data such as CPU usage, memory consumption, or network throughput.

Construction uses functional options for immutable configuration:

chart := linechart.New(
    linechart.MaxPoints(60),
    linechart.YRange(0, 100),
    linechart.ShowGrid(true),
    linechart.ShowLabels(true),
)

Data Management

Data is organized into named series, each with its own color and rolling window of data points:

chart.AddSeries("CPU", cpuColor)
chart.PushValue("CPU", 45.2) // adds point, shifts oldest if at MaxPoints

Multiple series can be displayed simultaneously with different colors. [PushValue] is safe to call from any goroutine.

Visual Style

The visual rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) can supply its own painter to render charts in the appropriate visual style.

If no painter is set, DefaultPainter is used, which draws a minimal chart suitable for testing and prototyping.

Signal Binding

Chart data can be bound to reactive signals from the state package. When the signal value changes, the chart automatically reflects the new data:

seriesSig := state.NewSignal([]linechart.Series{...})
chart := linechart.New(
    linechart.SeriesSignal(seriesSig),
)

Retained Mode

The chart calls widget.WidgetBase.SetNeedsRedraw when data changes via Widget.PushValue or Widget.AddSeries, enabling efficient retained-mode rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataPoint

type DataPoint struct {
	Value float64
}

DataPoint represents a single data value in a series.

type DefaultPainter

type DefaultPainter struct{}

DefaultPainter provides a minimal fallback painter with no design system styling. It draws background, grid lines, Y-axis labels, and line segments for each series.

func (DefaultPainter) PaintChart

func (p DefaultPainter) PaintChart(canvas widget.Canvas, bounds geometry.Rect, cs PaintState)

PaintChart renders the chart with background, optional grid, optional labels, and line segments for each data series.

type Option

type Option func(*config)

Option configures a line chart during construction.

func BackgroundColor

func BackgroundColor(color widget.Color) Option

BackgroundColor sets the chart background fill color.

func GridColor

func GridColor(color widget.Color) Option

GridColor sets the color used for grid lines.

func MaxPoints

func MaxPoints(n int) Option

MaxPoints sets the rolling window size (number of data points displayed). Default is 60.

func PainterOpt

func PainterOpt(p Painter) Option

PainterOpt sets the painter used to render the chart. Each design system provides its own painter. If not set, DefaultPainter is used.

func SeriesData

func SeriesData(s []Series) Option

SeriesData sets the initial static series data.

func SeriesFn

func SeriesFn(fn func() []Series) Option

SeriesFn sets a dynamic function that returns the current series data. When set, this takes precedence over the static data but not over a signal set via SeriesSignal.

func SeriesReadonlySignal

func SeriesReadonlySignal(sig state.ReadonlySignal[[]Series]) Option

SeriesReadonlySignal binds the chart's series data to a read-only signal. This is useful for computed signals. When set, this takes highest precedence.

func SeriesSignal

func SeriesSignal(sig state.Signal[[]Series]) Option

SeriesSignal binds the chart's series data to a reactive signal. This is a TWO-WAY binding: the widget reads series from the signal, and when PushValue modifies series, the signal is updated.

func ShowGrid

func ShowGrid(v bool) Option

ShowGrid enables or disables grid lines. Default is false.

func ShowLabels

func ShowLabels(v bool) Option

ShowLabels enables or disables Y axis labels. Default is false.

func YRange

func YRange(yMin, yMax float64) Option

YRange sets the Y axis range [yMin, yMax]. Default is [0, 100].

type PaintState

type PaintState struct {
	Series     []Series
	MaxPoints  int
	YMin       float64
	YMax       float64
	ShowGrid   bool
	ShowLabels bool
	GridColor  widget.Color
	Background widget.Color
}

PaintState holds the read-only snapshot passed to the painter.

type Painter

type Painter interface {
	PaintChart(canvas widget.Canvas, bounds geometry.Rect, state PaintState)
}

Painter renders the chart visuals. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation to render the chart in its visual style.

If no Painter is set, DefaultPainter is used.

type Series

type Series struct {
	Label  string
	Color  widget.Color
	Points []DataPoint
}

Series represents a named collection of data points with a display color.

type Widget

type Widget struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

Widget implements a real-time line chart for visualizing time-series data.

A line chart is created with New using functional options:

chart := linechart.New(
    linechart.MaxPoints(60),
    linechart.YRange(0, 100),
    linechart.ShowGrid(true),
)

Data is pushed with Widget.AddSeries and Widget.PushValue, which are safe to call from any goroutine.

func New

func New(opts ...Option) *Widget

New creates a new line chart Widget with the given options.

The returned widget is visible and enabled by default. The default Y range is [0, 100] with a rolling window of 60 points.

func (*Widget) AddSeries

func (w *Widget) AddSeries(label string, color widget.Color)

AddSeries adds a named data series with the given color. If a series with the same label already exists, this is a no-op. Safe to call from any goroutine.

func (*Widget) Children

func (w *Widget) Children() []widget.Widget

Children returns nil because a line chart is a leaf widget.

func (*Widget) ClearSeries

func (w *Widget) ClearSeries(label string)

ClearSeries removes all data points from the named series. Safe to call from any goroutine.

func (*Widget) Draw

func (w *Widget) Draw(_ widget.Context, canvas widget.Canvas)

Draw renders the chart to the canvas.

func (*Widget) Event

func (w *Widget) Event(_ widget.Context, _ event.Event) bool

Event handles an input event and returns true if consumed. LineChart is a display-only widget and does not consume events.

func (*Widget) Layout

func (w *Widget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the chart's preferred size within the given constraints.

func (*Widget) Mount

func (w *Widget) Mount(ctx widget.Context)

Mount creates signal bindings for push-based invalidation. Implements widget.Lifecycle.

func (*Widget) Padding

func (w *Widget) Padding(v float32) *Widget

Padding sets the padding around the chart area. Returns the widget for method chaining.

func (*Widget) PushValue

func (w *Widget) PushValue(label string, value float64)

PushValue appends a data point to the named series. If the series has reached MaxPoints, the oldest point is removed (rolling window). Safe to call from any goroutine.

func (*Widget) SeriesCount

func (w *Widget) SeriesCount() int

SeriesCount returns the number of series. Safe to call from any goroutine.

func (*Widget) Unmount

func (w *Widget) Unmount()

Unmount is called when the chart is removed from the widget tree. Implements widget.Lifecycle.

Jump to

Keyboard shortcuts

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