chart

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 23 Imported by: ? modules, 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 draws charts: line, area, bar (grouped, stacked, horizontal), pie and donut, funnel, scatter and bubble, gauge, heatmap and sparklines, with axes, legends, tooltips, selection, brushing, zoom and pan, animated data changes, theme colors, right-to-left layout and a data table for screen readers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatNumber

func FormatNumber(v float64) string

FormatNumber writes v compactly: 950, 1.2k, 3.4M, 2B, with up to decimals digits after the point for small numbers.

func Sparkline

func Sparkline(values []float64, st ...styles.Styles) elements.Element

Sparkline is a small line of values without axes, for tables and stat cards: its area shaded, the last value dotted. Styles set its size (Width, Height; 120 by 32 dp by default) and Color.

func TimeX

func TimeX(t time.Time) float64

TimeX returns t as a Point X for a time axis.

Types

type Axis

type Axis struct {
	Title string
	// Format writes tick labels and tooltip values; FormatNumber, or
	// dates on a time axis, by default.
	Format func(v float64) string
	// Min and Max fix the range when Fixed is set; otherwise it's the
	// data's, rounded out to ticks (and including zero for bars).
	Min, Max float64
	Fixed    bool
	// Time makes X a time axis of Unix seconds, in Location (local
	// time when nil).
	Time     bool
	Location *time.Location
	// Hide hides the axis line and labels; NoGrid its grid lines.
	Hide, NoGrid bool
}

Axis configures an axis.

type Band

type Band struct {
	To    float64
	Color string
}

Band is a colored range of a gauge: green to 60, amber to 85, red.

type Chart

type Chart struct {
	elements.Contextual
	// contains filtered or unexported fields
}

Chart is a stateful chart. Create it once with New and lay it out every frame; set data whenever it changes.

func New

func New(kind Kind) *Chart

New returns a chart of kind.

func (*Chart) Animate

func (c *Chart) Animate(on bool) *Chart

Animate turns animating data changes on (the default) or off. With reduced motion on, they don't animate either way.

func (*Chart) Categories

func (c *Chart) Categories(names ...string) *Chart

Categories names the categories: the x axis of a category chart, pie slices, funnel stages.

func (*Chart) Description

func (c *Chart) Description(s string) *Chart

Description sets what screen readers hear about the chart, in place of the summary of its data.

func (*Chart) Height

func (c *Chart) Height(dp int) *Chart

Height sets the chart's height in dp, 280 by default; it shrinks to the height it's given.

func (*Chart) Layout

func (c *Chart) Layout(gtx layout.Context) layout.Dimensions

Layout draws the chart in the width it's given.

func (*Chart) Legend

func (c *Chart) Legend(p LegendPos) *Chart

Legend places the legend.

func (*Chart) OnBrush

func (c *Chart) OnBrush(fn func(from, to float64)) *Chart

OnBrush lets dragging across an x-y or category chart select a range, passed as x values (category indexes on a category chart).

func (*Chart) OnSelect

func (c *Chart) OnSelect(fn func(series, index int)) *Chart

OnSelect sets the handler of a click on a bar, point, slice, stage or cell: its series and index (a heatmap's row and column).

func (*Chart) ResetZoom

func (c *Chart) ResetZoom()

ResetZoom shows all the data again.

func (*Chart) Selected

func (c *Chart) Selected() (series, index int)

Selected returns the selected series and index, -1 for none.

func (*Chart) Series

func (c *Chart) Series() []Series

Series returns the chart's data.

func (*Chart) SetGauge

func (c *Chart) SetGauge(value, min, max float64, bands ...Band) *Chart

SetGauge sets a gauge's value and range, and its colored bands.

func (*Chart) SetHeatmap

func (c *Chart) SetHeatmap(rows, cols []string, values [][]float64) *Chart

SetHeatmap sets a heatmap's rows, columns and values by row.

func (*Chart) SetSeries

func (c *Chart) SetSeries(ss ...Series) *Chart

SetSeries replaces the data, animating from what was shown.

func (*Chart) Smooth

func (c *Chart) Smooth(on bool) *Chart

Smooth draws lines and areas as curves.

func (*Chart) Style

func (c *Chart) Style(st styles.Styles) *Chart

Style sets the chart's box styles.

func (*Chart) Summary

func (c *Chart) Summary() string

Summary describes the chart in words, for screen readers: its title, kind, and each series' range; or the Description set.

func (*Chart) Table

func (c *Chart) Table() elements.Element

Table returns the chart's data as a table, a text alternative to show beside it or instead of it: a row per category or x, a column per series.

func (*Chart) Title

func (c *Chart) Title(s string) *Chart

Title sets the title drawn above the chart, which also names it to screen readers.

func (*Chart) X

func (c *Chart) X(a Axis) *Chart

X, Y and Y2 configure the axes; Y2 is the right-hand one, for series with Axis 1.

func (*Chart) Y

func (c *Chart) Y(a Axis) *Chart

func (*Chart) Y2

func (c *Chart) Y2(a Axis) *Chart

func (*Chart) Zoomable

func (c *Chart) Zoomable(on bool) *Chart

Zoomable lets the wheel, or Ctrl and the wheel inside a scroll, zoom the x axis around the pointer, and dragging pan it; a double click resets.

type Kind

type Kind int

Kind is a chart's or a series' type.

const (
	// Inherit is a series' zero kind: the chart's.
	Inherit Kind = iota
	Line
	Area
	Bar
	// HorizontalBar draws bars across, categories down the side.
	HorizontalBar
	Scatter
	Pie
	Donut
	Funnel
	Gauge
	Heatmap
)

type LegendPos

type LegendPos int

LegendPos places the legend.

const (
	// LegendAuto shows one above charts of several series and pies.
	LegendAuto LegendPos = iota
	LegendNone
	LegendTop
	LegendBottom
)

type Point

type Point struct {
	X, Y float64
	// Size is a bubble's size in a scatter chart, relative to the
	// largest; 0 draws plain points.
	Size float64
	// Label names the point in tooltips.
	Label string
}

Point is a point of an x-y series: X a number or, on a time axis, Unix seconds (TimeX converts).

type Series

type Series struct {
	Name string
	// Values are one per category: bars, lines over categories, pie
	// slices and funnel stages (named by the chart's Categories).
	Values []float64
	// Points are an x-y series: lines and areas over numbers or time,
	// scatter plots.
	Points []Point
	// Color overrides the theme's series color ("theme.chart1" and on).
	Color string
	// Kind draws this series differently from the chart: a Line over
	// Bar chart's bars, for example.
	Kind Kind
	// Axis 1 measures the series on the right-hand axis (Y2).
	Axis int
	// Stack stacks the series on the others with the same Stack:
	// stacked bars and areas.
	Stack string
	// Dashed draws a line dashed, as forecasts are.
	Dashed bool
	// Hidden leaves the series out; clicking its legend entry toggles
	// it.
	Hidden bool
}

Series is a set of values a chart draws.

Jump to

Keyboard shortcuts

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