Files
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

65 lines
2.0 KiB
Go
Raw Permalink 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"
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Candle is a period's open, high, low and close.
type Candle struct {
Label string
Open, High, Low, Close float64
}
type CandlestickOptions struct {
// Width and Height are dots; zeros mean 160 by 80.
Width, Height int
}
// Candlestick draws wicks and bodies: up Good, down Bad.
func Candlestick(c *ui.Context, text string, candles []Candle, o CandlestickOptions) ui.Element {
k := pixel.K(c)
if len(candles) == 0 {
panic("chart: Candlestick has no candles")
}
w, h := size("Candlestick", o.Width, o.Height, 160, 80)
labels, lows, highs := make([]string, len(candles)), make([]float64, len(candles)), make([]float64, len(candles))
for i, cd := range candles {
span([]float64{cd.Open, cd.High, cd.Low, cd.Close})
if cd.Low > min(cd.Open, cd.Close) || cd.High < max(cd.Open, cd.Close) {
panic(fmt.Sprintf("chart: candle %q has open %v and close %v outside %v–%v", cd.Label, cd.Open, cd.Close, cd.Low, cd.High))
}
labels[i], lows[i], highs[i] = cd.Label, cd.Low, cd.High
}
lo, _ := span(lows)
_, hi := span(highs)
p := fit(lo, hi, w, h, labels)
return ui.Box(c).Size(k.D(w), k.D(h)).Role(ui.RoleImage).Label(text).Description(ohlc(candles)).Draw(func(pt *ui.Painter, r ui.Rect) {
g := k.Canvas(pt, r)
p.grid(g, labels)
bw := max(1, p.w/len(candles)/2)
for i, cd := range candles {
x, ink := p.slot(i, len(candles)), pixel.Good
if cd.Close < cd.Open {
ink = pixel.Bad
}
g.VLine(x, p.at(cd.High), p.at(cd.Low)-p.at(cd.High)+1, pixel.Edge)
top, bottom := p.at(max(cd.Open, cd.Close)), p.at(min(cd.Open, cd.Close))
g.Rect(x-bw/2, top, max(1, bw), max(1, bottom-top), ink)
}
})
}
// ohlc lists each candle's open, high, low and close.
func ohlc(candles []Candle) string {
parts := make([]string, len(candles))
for i, cd := range candles {
parts[i] = cd.Label + " " + nums([]float64{cd.Open, cd.High, cd.Low, cd.Close})
}
return strings.Join(parts, "; ")
}