Files
8bitMyGoUI/chart/sparkline.go
T
ZacharyZhang-NY 596c62e026 Add chart components 216-245; step-aware ticks, font-aware labels
Bars, lines, areas, pies, donuts, radar, histogram, scatter, bubbles,
heatmaps, gauge, bullet, funnel, waterfall, treemap, candles, box
plots, steps, rings, legend, axis, crosshair, polar area, pyramid,
spectrum bars and win-loss strips, each drawn dot by dot. Ticks follow
1-2-5 steps and print as precisely as their step needs; a label row
uses micro letters or the pixel font as a whole. Descriptions carry the
data in numbers. Bad data panics.
2026-10-12 00:58:44 -04:00

41 lines
1.1 KiB
Go

package chart
import (
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type SparklineOptions struct {
// Width is in dots; zero means 48.
Width int
Slot pixel.Slot
}
// Sparkline is a line one text line high, end marked.
func Sparkline(c *ui.Context, text string, values []float64, o SparklineOptions) *ui.Element {
k := pixel.K(c)
w := pixel.Or("Sparkline Width", o.Width, 48)
if len(values) < 2 {
panic("chart: Sparkline needs two values or more")
}
lo, hi := span(values)
if hi == lo {
hi = lo + 1
}
ink := pick(o.Slot, 0)
at := func(i int) (int, int) {
return i * (w - 2) / (len(values) - 1), pixel.LineHeight - 3 - int((values[i]-lo)/(hi-lo)*float64(pixel.LineHeight-6))
}
return ui.Box(c).Size(k.D(w), k.D(pixel.LineHeight)).Role(ui.RoleImage).Label(text).Description(nums(values)).Draw(func(pt *ui.Painter, r ui.Rect) {
g := k.Canvas(pt, r)
for i := 1; i < len(values); i++ {
x0, y0 := at(i - 1)
x1, y1 := at(i)
g.Line(x0, y0, x1, y1, ink)
}
x, y := at(len(values) - 1)
g.Rect(x, y-1, 2, 2, pixel.Accent)
})
}