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 ¶
- func Anchor(c *ui.Context, x, y float32, h, v ui.Align, build func()) ui.Element
- func Area(r ui.Rect, xs, lo, hi []float32, k Curve) *ui.Path
- func Extent(values ...float64) (lo, hi float64)
- func Line(r ui.Rect, xs, ys []float32, k Curve) *ui.Path
- func LineInto(path *ui.Path, r ui.Rect, xs, ys []float32, k Curve) *ui.Path
- func MinMax(dstX, dstY, xs, ys []float32, pixelWidth int) ([]float32, []float32)
- func Nearest(xs []float32, f float32) int
- func Plot(c *ui.Context, axes Axes, build func(area ui.Element)) ui.Element
- func Point(c *ui.Context, x, y, w, h float32) ui.Element
- func Polar(cx, cy, r float32, a float64) (x, y float32)
- func Rect(c *ui.Context, x, y Span) ui.Element
- func RuleX(c *ui.Context, x, width float32) ui.Element
- func RuleY(c *ui.Context, y, height float32) ui.Element
- func Sector(cx, cy, inner, outer float32, s Slice) *ui.Path
- func Select(c *ui.Context, area ui.Element, index *int, xs []float32, ...) bool
- func SliceAt(slices []Slice, x, y, inner, outer float32) (int, bool)
- func Stack(series [][]float64, how Stacking) [][]Segment
- func X(r ui.Rect, f float32) float32
- func Y(r ui.Rect, f float32) float32
- type Axes
- type Band
- type Curve
- type Linear
- type Segment
- type Side
- type Slice
- type Span
- type Stacking
- type Tick
- type Time
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Anchor ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Polar returns the point at the angle a, r from the center (cx, cy), as where a slice's label goes.
func Rect ¶
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 ¶
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 Sector ¶
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 ¶
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 ¶
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])
}
}
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 (Band) Index ¶
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 ¶
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.
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 (Linear) Nice ¶
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.
type Segment ¶
type Segment struct{ Lo, Hi float64 }
Segment is a value's stretch in a stack, from Lo up to Hi.
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.
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.
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 ¶
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)