Files
8bitMyGoUI/chart/horizontalbar.go
T
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:31:08 -04:00

45 lines
1.4 KiB
Go

package chart
import (
"fmt"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type HorizontalBarOptions struct {
// Width is in dots, zero means 160; rows are lines.
Width int
// Slot fills the bars; zero means Accent.
Slot pixel.Slot
}
// HorizontalBar lays labeled bars across, longest to the edge.
func HorizontalBar(c *ui.Context, text string, labels []string, values []float64, o HorizontalBarOptions) ui.Element {
k := pixel.K(c)
if len(labels) != len(values) || len(values) == 0 {
panic(fmt.Sprintf("chart: HorizontalBar has %d labels for %d values", len(labels), len(values)))
}
w := pixel.Or("HorizontalBar Width", o.Width, 160)
lo, hi := span(values)
if lo < 0 {
panic(fmt.Sprintf("chart: HorizontalBar value %v is negative", lo))
}
gutter, room := 0, 0
for i, l := range labels {
gutter, room = max(gutter, pixel.Measure(l)), max(room, tinyW(num(values[i])))
}
ink := pick(o.Slot, 0)
return ui.Box(c).Size(k.D(w), k.D(len(values)*pixel.LineHeight)).Role(ui.RoleImage).Label(text).Description(read(labels, values)).Draw(func(pt *ui.Painter, r ui.Rect) {
g := k.Canvas(pt, r)
span := w - gutter - room - 8
for i, v := range values {
y, bw := i*pixel.LineHeight, int(part(v, hi)*float64(span))
g.Text(gutter-pixel.Measure(labels[i]), y, labels[i], pixel.Faint, 1)
g.Rect(gutter+4, y+4, max(1, bw), pixel.LineHeight-8, ink)
tiny(g, gutter+6+bw, y+6, num(v), pixel.Ink)
}
})
}