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

65 lines
2.0 KiB
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"
"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, "; ")
}