Files
8bitMyGoUI/chart/progressring.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

35 lines
979 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package chart
import (
"fmt"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ProgressRingOptions struct {
// Size is the ring's width in dots; zero means 40.
Size int
Slot pixel.Slot
}
// ProgressRing fills a ring clockwise from twelve, in pixel arcs.
func ProgressRing(c *ui.Context, text string, value float64, o ProgressRingOptions) *ui.Element {
k := pixel.K(c)
if !(value >= 0 && value <= 1) {
panic(fmt.Sprintf("chart: ProgressRing value %v outside 0–1", value))
}
n := pixel.Or("ProgressRing Size", o.Size, 40)
pct := strconv.Itoa(int(value*100)) + "%"
ink := pick(o.Slot, 0)
return ui.Box(c).Size(k.D(n), k.D(n)).Role(ui.RoleProgress).Label(text).Description(pct).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
c, rad := n/2, n/2-1
sector(g, c, c, rad-4, rad, 0, 1, pixel.Well)
sector(g, c, c, rad-4, rad, 0, value, ink)
tiny(g, c-tinyW(pct)/2+1, c-2, pct, pixel.Ink)
})
}