Skip to Content
Onevra

Charts

Bar, line, area, stacked bar, donut, bubble and split bar charts over data you own.

Loading the live example…

chart_bar.go
import (
	"fmt"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components/chart"
)

type weeklyHours struct {
	state chart.State // Index -1 starts with nothing selected
}

func (w *weeklyHours) View(c *ui.Context) {
	data := chart.Data{
		Labels: []string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
		Series: []chart.Series{
			{ID: "this", Label: "This week", Values: []float64{38, 52, 46, 68, 61, 82}},
			{ID: "last", Label: "Last week", Values: []float64{26, 34, 28, 48, 43, 57}},
		},
	}
	ui.Column(c).Width(460).MaxWidthPercent(100).Children(func() {
		chart.Bar(c, &w.state, data, chart.Options{
			Label:       "Focus hours",
			Description: "Use the arrow keys to explore",
			PlotHeight:  200,
			FormatValue: func(v float64) string { return fmt.Sprintf("%.0f h", v) },
		})
	})
}
Source

The chart package draws categorical charts. The application keeps the data and a chart.State per chart, gives series and slices stable IDs, and supplies labels and formatting. Hover, or focus the chart and use the arrow keys, to select a category; its details show in a tooltip. Start with chart.State{Index: -1} for no selection.

Data changes animate: values move to their new places, and Reduced Motion skips the transition. These charts suit bounded datasets; aggregate or sample large data before passing it in.

Line

Lines are monotone and break at missing values.

Loading the live example…

chart_line.go
import (
	"fmt"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
	"github.com/oneo-me/onevra/source/components/chart"
)

type temperatures struct {
	state chart.State
	round int
}

func (t *temperatures) View(c *ui.Context) {
	// Updating the data animates the lines to their new values.
	values := [][]float64{{12, 14, 13, 17, 19, 18, 21}, {15, 13, 16, 14, 18, 22, 20}}
	data := chart.Data{
		Labels: []string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"},
		Series: []chart.Series{
			{ID: "lisbon", Label: "Lisbon", Values: values[t.round%2]},
			{ID: "oslo", Label: "Oslo", Values: []float64{4, 6, 5, 7, 3, 5, 8}},
		},
	}
	ui.Column(c).Width(460).MaxWidthPercent(100).Gap(8).Children(func() {
		chart.Line(c, &t.state, data, chart.Options{
			Label:       "Temperature",
			PlotHeight:  200,
			FormatValue: func(v float64) string { return fmt.Sprintf("%.0f °C", v) },
		})
		if components.Button(c, "Next week", components.ButtonSecondary).Clicked() {
			t.round++
		}
	})
}
Source

Area

Areas overlap from zero; use stacked bars for additive totals.

Loading the live example…

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

type traffic struct {
	state chart.State
}

func (t *traffic) View(c *ui.Context) {
	data := chart.Data{
		Labels: []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun"},
		Series: []chart.Series{
			{ID: "visits", Label: "Visits", Values: []float64{120, 180, 150, 240, 210, 300}},
			{ID: "signups", Label: "Sign-ups", Values: []float64{30, 45, 38, 70, 64, 92}},
		},
	}
	ui.Column(c).Width(460).MaxWidthPercent(100).Children(func() {
		chart.Area(c, &t.state, data, chart.Options{Label: "Traffic", PlotHeight: 200})
	})
}
Source

Stacked bar

The legend hides and shows series.

Loading the live example…

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

type revenue struct {
	state chart.State
}

func (r *revenue) View(c *ui.Context) {
	data := chart.Data{
		Labels: []string{"Q1", "Q2", "Q3", "Q4"},
		Series: []chart.Series{
			{ID: "apps", Label: "Apps", Values: []float64{42, 48, 51, 63}},
			{ID: "services", Label: "Services", Values: []float64{28, 31, 37, 40}},
			{ID: "hardware", Label: "Hardware", Values: []float64{18, 15, 22, 30}},
		},
	}
	ui.Column(c).Width(460).MaxWidthPercent(100).Children(func() {
		// The legend hides and shows series; totals stack.
		chart.StackedBar(c, &r.state, data, chart.Options{Label: "Revenue", PlotHeight: 200})
	})
}
Source

Donut

Slices are proportional; the legend selects them.

Loading the live example…

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

type timeSpent struct {
	state chart.State
}

func (t *timeSpent) View(c *ui.Context) {
	slices := []chart.Slice{
		{ID: "design", Label: "Design", Value: 38},
		{ID: "build", Label: "Development", Value: 52},
		{ID: "research", Label: "Research", Value: 21},
		{ID: "planning", Label: "Planning", Value: 14},
	}
	ui.Column(c).Width(420).MaxWidthPercent(100).Children(func() {
		chart.Donut(c, &t.state, slices, chart.Options{Label: "Time spent", PlotHeight: 220})
	})
}
Source

Bubble

A bubble's area follows its value. Added bubbles grow in and the pack springs to its new layout.

Loading the live example…

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

var languageShares = []chart.Slice{
	{ID: "go", Label: "Go", Value: 48}, {ID: "ts", Label: "TypeScript", Value: 31},
	{ID: "rust", Label: "Rust", Value: 18}, {ID: "swift", Label: "Swift", Value: 12},
	{ID: "py", Label: "Python", Value: 26}, {ID: "kt", Label: "Kotlin", Value: 9},
	{ID: "c", Label: "C", Value: 7},
}

type languages struct {
	state chart.State
	count int
}

func (l *languages) View(c *ui.Context) {
	ui.Column(c).Width(440).MaxWidthPercent(100).Gap(8).Children(func() {
		// A bubble's area, not its radius, follows its value.
		chart.Bubble(c, &l.state, languageShares[:l.count], chart.Options{Label: "Languages", PlotHeight: 220})
		ui.Row(c).Gap(8).Children(func() {
			if components.Button(c, "Add", components.ButtonSecondary).Disabled(l.count == len(languageShares)).Clicked() {
				l.count++
			}
			if components.Button(c, "Remove", components.ButtonGhost).Disabled(l.count == 1).Clicked() {
				l.count--
			}
		})
	})
}
Source

Split bar

One bar divided into shares, for part-to-whole splits of a few categories, with a readout of the total or the highlighted slice.

Loading the live example…

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

type trafficSources struct {
	state chart.State
}

func (t *trafficSources) View(c *ui.Context) {
	slices := []chart.Slice{
		{ID: "organic", Label: "Organic", Value: 41260},
		{ID: "direct", Label: "Direct", Value: 24610},
		{ID: "paid", Label: "Paid", Value: 15830},
		{ID: "social", Label: "Social", Value: 8420},
		{ID: "referral", Label: "Referral", Value: 5310},
	}
	ui.Column(c).Width(480).MaxWidthPercent(100).Children(func() {
		chart.SplitBar(c, &t.state, slices, chart.Options{
			Label:       "Traffic sources",
			Description: "Sessions this month",
		})
	})
}
Source

API

Bar

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

Bar draws grouped bars with a zero baseline, including negative values.

pkg.go.dev

Line

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

Line draws monotone lines, breaking paths at missing values.

pkg.go.dev

Area

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

Area draws overlapping, translucent areas from zero to each series. Use StackedBar for additive totals; areas are not stacked.

pkg.go.dev

StackedBar

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

StackedBar stacks positive and negative values separately around zero.

pkg.go.dev

Donut

func Donut(c *ui.Context, state *State, data []Slice, opts Options) ui.Element

Donut draws proportional slices. Hover, arrow keys, Home/End and legend buttons select a slice; Escape clears it. State.Hidden is unused. Invalid and non-positive slices are omitted while State.Index refers to input order.

pkg.go.dev

Bubble

func Bubble(c *ui.Context, state *State, data []Slice, opts Options) ui.Element

Bubble packs one circle per slice, its area proportional to the value, so larger values take larger bubbles. The pack keeps to the smallest extent the plot's proportions allow, centered, so the bubbles are as large as possible. Bubbles large enough show their label and formatted value. Bubbles show packed at once, then spring to new sizes and places and pop in as they appear. Hover, arrow keys, Home/End and legend buttons select a bubble; Escape clears it. State.Hidden is unused. Invalid and non-positive slices are omitted while State.Index refers to input order.

pkg.go.dev

SplitBar

func SplitBar(c *ui.Context, state *State, data []Slice, opts Options) ui.Element

SplitBar draws one bar divided into the shares of the visible slices, for part-to-whole splits of about two to eight categories. Options.Label titles the bar and names it; Description is a line under the title, and a readout beside them shows the total or the highlighted slice's value and share. FormatValue writes values, unit included (nil writes grouped numbers); shares are percentages with one decimal. PlotHeight is the bar's thickness (default 32 DIPs); labels that do not fit inside their segment move to a lane under it on leader lines. Detail replaces the readout.

State.Index is the highlighted slice in input order, set by hover, arrow keys, Home/End and legend hover; Escape clears it. State.Hidden records slices the legend, Enter or Space switched off, keyed by Slice.ID; the last visible slice cannot be switched off. Invalid and non-positive slices are omitted. Segment edges follow a spring, which AnimationDuration sets (zero uses MyGo's smooth spring, negative disables motion), and figures roll as they change. The total's name and refusal message come from the onevra catalog.

pkg.go.dev

Data

Data is borrowed for the frame and never modified by a chart.

Prop

Type

pkg.go.dev

Series

Series holds values aligned with Data.Labels. Missing and non-finite values are gaps; excess values are ignored. ID must be nonempty, unique and stable. A zero Color chooses a muted theme-aware color by series order.

Prop

Type

pkg.go.dev

Slice

Slice is a donut or bubble category. IDs have the same requirements as Series.ID. Only finite positive values take space in the chart and legend.

Prop

Type

pkg.go.dev

Options

Options supplies presentation and application-owned content. PlotHeight defaults to 220 DIPs; legends add their own height below the plot. FormatValue formats axes, tooltips and accessible values (nil uses numbers). Description describes the plot to assistive technology. Detail replaces the content of the selection tooltip, which follows the pointer over the plot or sits beside a selection the keyboard made; it is called only while the tooltip shows. Empty replaces the empty plot, including when all series are hidden; legends remain available. With no Empty slot, EmptyText is shown (default "—").

Prop

Type

pkg.go.dev

State

State retains interaction between frames. Index is a category (or original slice) index, with -1 meaning no selection; its zero value selects the first. Hidden records series hidden by Cartesian legends, keyed by Series.ID. Keep separate state per chart. Nil state uses element-local storage instead.

Prop

Type

pkg.go.dev
Edit this page on GitHub

On this page