chart

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package chart builds charts in native UI without a look of their own, as headless libraries do on the web: scales that map data onto a plot and pick round ticks, a plot with axes around it, marks that are elements you style as any other, paths of lines, areas and pie slices for Draw functions, stacking, and a selection that the pointer and the keyboard move.

x := chart.NewBand(months...).Padding(0.3, 0.15)
y := chart.NewLinear(0, 120).Nice(4)
chart.Plot(c, chart.Axes{Left: y.Ticks(4), Bottom: x.Ticks()}, func(area ui.Element) {
	for i, v := range sales {
		chart.Rect(c, x.Band(i), y.Span(0, v)).Background(t.Accent).Radius(4)
	}
}).Height(200).Gap(6)

Positions along a plot are fractions of it, from 0 to 1: along x from its left edge, and along y from its bottom up, the way values grow. Marks place themselves in percentages of the plot, so they need no layout to be built, follow the plot as it resizes, and move smoothly with a Transition as the data changes; Draw functions get the plot's box, and X and Y turn fractions into points in it.

See docs/ui/chart.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Anchor

func Anchor(c *ui.Context, x, y float32, h, v ui.Align, build func()) ui.Element

Anchor builds what build adds at the point at x along the plot and y up it, of any size: as a label over a bar, or a tooltip by the selection. h and v place it: Center centers it on the point, Start puts its left (h) or top (v) edge there, and End its right or bottom edge, so that End, End shows it above and to the left of the point.

chart.Anchor(c, x.At(i), y.At(v), ui.Center, ui.End, func() {
	ui.Text(c, label).NoWrap().Margin(0, 0, 4, 0)
})

func Area

func Area(r ui.Rect, xs, lo, hi []float32, k Curve) *ui.Path

Area returns the path of the area between two lines through the points at xs, lo below and hi above, joined as curve says, for areas and bands and the layers of a stack. Nil lo puts the bottom line at the bottom of the plot. A NaN value breaks the area there.

area.Draw(func(p *ui.Painter, r ui.Rect) {
	p.FillPath(chart.Area(r, xs, nil, ys, chart.CurveMonotone), fill)
})

func Extent

func Extent(values ...float64) (lo, hi float64)

Extent returns the smallest and the largest of values, the domain of a scale showing them, and 0, 1 for none. Include 0 for bars and areas:

lo, hi := chart.Extent(sales...)
y := chart.NewLinear(min(lo, 0), hi).Nice(5)

func Line

func Line(r ui.Rect, xs, ys []float32, k Curve) *ui.Path

Line returns the path of a line through the points at xs and ys, fractions along and up the plot whose box is r, joined as curve says. A NaN value breaks the line there. Stroke it in a Draw function of the plot:

area.Draw(func(p *ui.Painter, r ui.Rect) {
	p.StrokePath(chart.Line(r, xs, ys, chart.CurveMonotone), 2, t.Accent)
})

func LineInto

func LineInto(path *ui.Path, r ui.Rect, xs, ys []float32, k Curve) *ui.Path

LineInto replaces path with Line's geometry, reusing its storage. It also clears any Trim. Keep one path per series when drawing live waveforms.

func MinMax

func MinMax(dstX, dstY, xs, ys []float32, pixelWidth int) ([]float32, []float32)

MinMax reduces a line of many points to the lowest and highest of each pixel column of the plot, in their order, and the ends of each run of defined points, so that a line through them looks the same and narrow spikes still show. xs and ys are fractions, as for Line; xs must not decrease, or the columns come out wrong, and a NaN or infinite value breaks the line. Points left or right of the plot go, but for the nearest one on each side. pixelWidth is the plot's width in pixels, ceil(r.W * p.Scale()).

MinMax appends to dstX[:0] and dstY[:0] and returns them, so that a view keeping them reduces every frame without allocating; they must not share memory with xs or ys. Keep the points themselves for selections and readings.

func Nearest

func Nearest(xs []float32, f float32) int

Nearest returns the index of the value of xs, in ascending order, that is nearest f, or -1 for none: the point of a line under the pointer.

func Plot

func Plot(c *ui.Context, axes Axes, build func(area ui.Element)) ui.Element

Plot builds a plot with the axes around it: a grid whose middle cell is the plot's area, which build gets to style, as with a background or a Draw function painting grid lines, and to add marks to. Give the plot a height, and Gap to keep the labels away from the area:

chart.Plot(c, chart.Axes{Left: y.Ticks(5)}, func(area ui.Element) {
	area.Draw(func(p *ui.Painter, r ui.Rect) {
		for _, tick := range y.Ticks(5) {
			p.Line(r.X, chart.Y(r, tick.At), r.X+r.W, chart.Y(r, tick.At), 1, t.Border)
		}
	})
}).Height(160).Gap(8)

Name the plot for assistive technology with Label, and describe what it shows with Description.

func Point

func Point(c *ui.Context, x, y, w, h float32) ui.Element

Point places a mark of w by h DIPs centered on the point at x along the plot and y up it, as a dot of a scatter plot or the point a selection is on.

chart.Point(c, x.At(v.Weight), y.At(v.Height), 8, 8).Radius(4).Background(t.Accent)

func Polar

func Polar(cx, cy, r float32, a float64) (x, y float32)

Polar returns the point at the angle a, r from the center (cx, cy), as where a slice's label goes.

func Rect

func Rect(c *ui.Context, x, y Span) ui.Element

Rect places a mark over the stretch x along the plot and y up it, as a bar, a band of a range or a cell of a heat map: an element in the plot's area, which you style as any other, and name for assistive technology. Build it in the function of Plot.

chart.Rect(c, x.Band(i), y.Span(0, v)).Background(t.Accent).Radius(4).Tooltip(label)

func RuleX

func RuleX(c *ui.Context, x, width float32) ui.Element

RuleX places a vertical line of width DIPs across the plot at x along it, as a threshold or the selection of a line chart; RuleY a horizontal one at y up it. Color them with Background.

func RuleY

func RuleY(c *ui.Context, y, height float32) ui.Element

RuleY places a horizontal line of height DIPs across the plot at y up it.

func Sector

func Sector(cx, cy, inner, outer float32, s Slice) *ui.Path

Sector returns the path of a slice between the radii inner and outer around (cx, cy): a wedge of a pie with inner 0, of a donut otherwise. The slice's pad keeps a gap of the same width along it, as d3's arcs do.

ring.Draw(func(p *ui.Painter, r ui.Rect) {
	cx, cy := r.X+r.W/2, r.Y+r.H/2
	for i, s := range slices {
		p.FillPath(chart.Sector(cx, cy, 40, 80, s), colors[i])
	}
})

func Select

func Select(c *ui.Context, area ui.Element, index *int, xs []float32, describe func(i int) string) bool

Select makes the plot's area choose one of the points at xs, fractions along it in ascending order: *index is the one nearest the pointer while it is over the area, and -1, none, once it leaves. The area takes the keyboard focus, as one stop of Tab, where the left and right arrows, Home and End move the choice, and Escape clears it. Changed reports a new choice.

describe tells assistive technology about a point, "March: 42 sales", which it reads as the area's value, and as the arrows come to the point. Show the choice with marks, as a rule, a point and a label by it:

if chart.Select(c, area, &app.selected, xs, describe) { ... }
if i := app.selected; i >= 0 {
	chart.RuleX(c, xs[i], 1).Background(t.Border)
	chart.Point(c, xs[i], ys[i], 8, 8).Radius(4).Background(t.Accent)
}

func SliceAt

func SliceAt(slices []Slice, x, y, inner, outer float32) (int, bool)

SliceAt returns the slice at (x, y), relative to the center of a pie whose slices go from inner to outer, and whether there is one there: the slice under the pointer.

func Stack

func Stack(series [][]float64, how Stacking) [][]Segment

Stack piles series up, as stacked bars and areas show them: series[s][i] is the i-th value of the s-th series, and the result's [s][i] is where it goes in the stack of the i-th values, the s-th from the bottom. NaN values take no room.

stacked := chart.Stack([][]float64{apples, pears}, chart.StackStandard)
for s, series := range stacked {
	for i, seg := range series {
		chart.Rect(c, x.Band(i), y.Span(seg.Lo, seg.Hi)).Background(colors[s])
	}
}

func X

func X(r ui.Rect, f float32) float32

X returns the x of f, a fraction along a plot's width, in r, the plot's box a Draw function gets.

func Y

func Y(r ui.Rect, f float32) float32

Y returns the y of f, a fraction up a plot's height, in r: 0 is its bottom and 1 its top.

Types

type Axes

type Axes struct {
	Left, Right, Top, Bottom []Tick
	// Label builds the label of a tick on a side, centered on the tick;
	// nil shows its Label as text. Labels sit in boxes of no width or
	// height, so keep their text on one line with NoWrap.
	Label func(side Side, t Tick)
}

Axes are the ticks along the sides of a plot, whose labels go beside it: a scale's Ticks, or ticks of your own.

type Band

type Band struct {
	Labels []string
	// Inner is the room between bands, and Outer the room before the
	// first and after the last, in fractions of a band's step.
	Inner, Outer float32
	// Align places the bands in the room the outer padding leaves: 0 at
	// the start, 0.5 (as NewBand) in the middle, 1 at the end.
	Align float32
}

Band divides the plot into a band for each of its labels, as d3's scaleBand and Swift Charts' scales of categories: the bars of a bar chart, one for each month.

x := chart.NewBand(months...).Padding(0.2, 0.1)
bar := x.Band(3) // April's

func NewBand

func NewBand(labels ...string) Band

NewBand returns a band scale of labels, without padding.

func (Band) At

func (b Band) At(i int) float32

At returns the middle of the i-th band.

func (Band) Band

func (b Band) Band(i int) Span

Band returns the stretch of the i-th band.

func (Band) Index

func (b Band) Index(f float32) (i int, ok bool)

Index returns the band at f along the plot, the one whose step holds it, so that the room between bars belongs to a bar; ok is false outside of every band's step.

func (Band) Padding

func (b Band) Padding(inner, outer float32) Band

Padding sets the room between bands, and before and after them, in fractions of a band's step: 0.2 leaves a fifth of each step between bars.

func (Band) Ticks

func (b Band) Ticks() []Tick

Ticks returns a tick in the middle of each band, labeled with its label.

func (Band) Width

func (b Band) Width() float32

Width returns the width of a band, in a fraction of the plot.

type Curve

type Curve int

Curve is how a line goes from a point to the next, as Swift Charts' interpolation methods and d3's curves.

const (
	// CurveLinear joins the points with straight lines.
	CurveLinear Curve = iota
	// CurveMonotone joins them with a smooth curve that keeps between the
	// values of each two points, never going past them, as d3's
	// curveMonotoneX (Steffen's method).
	CurveMonotone
	// CurveStepAfter keeps a point's value until the next point, as a value
	// that holds until it changes.
	CurveStepAfter
	// CurveStepBefore takes on a point's value from the point before it.
	CurveStepBefore
	// CurveStep changes the value halfway between points.
	CurveStep
)

type Linear

type Linear struct {
	Lo, Hi float64
	// Clamp keeps values outside the domain at its ends.
	Clamp bool
	// Format labels the ticks; nil labels them with as many decimals as
	// their step needs.
	Format func(v float64) string
}

Linear maps numbers from Lo to Hi onto the plot, Lo at its start, as d3's scaleLinear and Swift Charts' automatic scales of numbers. Values outside the domain go outside the plot unless Clamp says otherwise.

y := chart.NewLinear(0, 87).Nice(10) // 0 to 90, ticks every 10
bar := y.Span(0, 42)

func NewLinear

func NewLinear(lo, hi float64) Linear

NewLinear returns a linear scale from lo to hi.

func (Linear) At

func (s Linear) At(v float64) float32

At returns where v is along the plot.

func (Linear) Invert

func (s Linear) Invert(f float32) float64

Invert returns the value at f along the plot, as where the pointer is.

func (Linear) Nice

func (s Linear) Nice(count int) Linear

Nice extends the domain to round values, those of about count ticks, as d3's nice: 0 to 87 becomes 0 to 90 for 10 ticks, and 0 to 100 for 5.

func (Linear) Span

func (s Linear) Span(a, b float64) Span

Span returns the stretch from a to b along the plot, as a bar's from 0 to its value.

func (Linear) Ticks

func (s Linear) Ticks(count int) []Tick

Ticks returns about count round values within the domain, 1, 2 or 5 times a power of ten apart, as d3's ticks and Swift Charts' automatic axis marks: 0, 20, 40, 60, 80 and 100 for 0 to 100 and 5.

type Segment

type Segment struct{ Lo, Hi float64 }

Segment is a value's stretch in a stack, from Lo up to Hi.

type Side

type Side int

Side is a side of a plot, where an axis goes.

const (
	Left Side = iota
	Right
	Top
	Bottom
)

type Slice

type Slice struct {
	Start, End, Pad float64
}

Slice is a value's part of a pie, from the angle Start to End, in radians clockwise from twelve o'clock, as d3's pie and Swift Charts' sector marks. Pad is the gap Sector leaves between it and the next.

func Pie

func Pie(values []float64, pad float64) []Slice

Pie divides a circle among values, in their order from twelve o'clock, each slice as large as its share of their sum. Pad is the angle between slices, in radians; values below 0, and NaN, take no room.

slices := chart.Pie(shares, 0.02)

func (Slice) Mid

func (s Slice) Mid() float64

Mid returns the angle halfway through the slice, where its label goes.

type Span

type Span struct{ Lo, Hi float32 }

Span is a stretch of a chart's plot along one axis, in fractions of it: from the left edge (0) to the right (1) along x, and from the bottom (0) to the top (1) along y, the way values grow. Scales return spans for the marks: a bar's band along x, its stretch from 0 to its value along y.

func (Span) Mid

func (s Span) Mid() float32

Mid returns the middle of the span.

type Stacking

type Stacking int

Stacking is how Stack piles series up, as Swift Charts' stacking methods.

const (
	// StackStandard piles values up from 0, and negative values down from it.
	StackStandard Stacking = iota
	// StackNormalized piles them up as StackStandard does, in shares of the sum of
	// their sizes: the stacks of positive values go from 0 to 1.
	StackNormalized
	// StackCenter piles them up around 0, as a streamgraph.
	StackCenter
)

type Tick

type Tick struct {
	At    float32
	Value float64
	Label string
}

Tick is a value a scale marks along an axis: where it is, in a fraction of the plot, and its label.

type Time

type Time struct {
	From, To time.Time
	// Format labels the ticks; nil labels each with what changed since
	// a round time of the coarser unit, as d3's multi-scale format:
	// "2026", "Mar", "Mar 5", "14:00", "14:05:30".
	Format func(t time.Time) string
}

Time maps times from From to To onto the plot, as d3's scaleTime and Swift Charts' scales of dates. Its ticks fall on round times of the times' location, a second, a quarter of an hour, a day, the first of a month, chosen for the span: midnights for a week, months for a year.

x := chart.NewTime(start, end)
ticks := x.Ticks(6)

func NewTime

func NewTime(from, to time.Time) Time

NewTime returns a time scale from from to to.

func (Time) At

func (s Time) At(t time.Time) float32

At returns where t is along the plot.

func (Time) Invert

func (s Time) Invert(f float32) time.Time

Invert returns the time at f along the plot.

func (Time) Span

func (s Time) Span(a, b time.Time) Span

Span returns the stretch from a to b along the plot.

func (Time) Ticks

func (s Time) Ticks(count int) []Tick

Ticks returns about count round times within the domain.

Jump to

Keyboard shortcuts

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