WIP components (as built)
This commit is contained in:
98 files changed
+2746
-1204
No files matched your search
+51
-17
@@ -32,7 +32,7 @@ type SparklineOptions struct {
|
||||
// Sparkline is an inline chart without axes.
|
||||
func Sparkline(c *ui.Context, values []float64, opts SparklineOptions) ChartResult {
|
||||
f := opts.Frame
|
||||
if f.Label == "" || f.Height < 0 || opts.Width < 0 || opts.Kind > SparklineArea {
|
||||
if f.Label == "" || f.Height < 0 || opts.Width < 0 || opts.Kind < SparklineLine || opts.Kind > SparklineArea {
|
||||
panic("mujica: Sparkline needs a Label, sizes ≥ 0 and a known Kind")
|
||||
}
|
||||
if f.Height == 0 {
|
||||
@@ -46,16 +46,23 @@ func Sparkline(c *ui.Context, values []float64, opts SparklineOptions) ChartResu
|
||||
u := Density(c).Unit()
|
||||
col := internal.Palette(k)[0]
|
||||
size := FontSize(c, theme.CaptionSize)
|
||||
lo, hi := math.Inf(1), math.Inf(-1)
|
||||
last := -1
|
||||
for i, v := range values {
|
||||
if !math.IsNaN(v) {
|
||||
lo, hi, last = min(lo, v), max(hi, v), i
|
||||
res := ChartResult{Point: -1}
|
||||
res.Element = ui.Row(c).Gap(u * 2).AlignItems(ui.Center).MinWidth(0)
|
||||
memo := ui.Local(res.Element, "mujica.sparkline.data", func() sparkMemo { return sparkMemo{} })
|
||||
if id := chartID(values); memo.id != id {
|
||||
*memo = sparkMemo{id: id, lo: math.Inf(1), hi: math.Inf(-1), last: -1}
|
||||
for i, v := range values {
|
||||
if math.IsInf(v, 0) {
|
||||
panic("mujica: Sparkline values must be finite or NaN")
|
||||
}
|
||||
if !math.IsNaN(v) {
|
||||
memo.lo, memo.hi, memo.last = min(memo.lo, v), max(memo.hi, v), i
|
||||
}
|
||||
}
|
||||
}
|
||||
res := ChartResult{Point: -1}
|
||||
res.Element = ui.Row(c).Gap(u * 2).AlignItems(ui.Center)
|
||||
lo, hi, last := memo.lo, memo.hi, memo.last
|
||||
st := ui.Local(res.Element, "mujica.sparkline", func() chartState { return chartState{focus: -1, last: -1} })
|
||||
st.focus = min(st.focus, len(values)-1)
|
||||
res.Element.Children(func() {
|
||||
if f.Status == DataLoading || f.Status == DataFailed || last < 0 {
|
||||
box := ui.Row(c).Size(w, f.Height).AlignItems(ui.Center).Gap(u)
|
||||
@@ -115,22 +122,18 @@ func Sparkline(c *ui.Context, values []float64, opts SparklineOptions) ChartResu
|
||||
active := -1
|
||||
if px, _, over := plot.PointerPosition(); over && !plot.IsDisabled() {
|
||||
active = max(0, min(n-1, int(math.Round(float64((px-3)/(w-6)*float32(max(n-1, 1)))))))
|
||||
} else if plot.Focused() {
|
||||
} else if plot.Focused() && !plot.IsDisabled() {
|
||||
active = st.focus
|
||||
}
|
||||
res.Point = active
|
||||
plot.Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
s := ys(r)
|
||||
if opts.Kind == SparklineBar {
|
||||
bw := max((r.W-6)/float32(n)-1, 1)
|
||||
for i, v := range values {
|
||||
if math.IsNaN(v) {
|
||||
continue
|
||||
}
|
||||
a, b := s.Map(0), s.Map(v)
|
||||
cx := x(r, i)
|
||||
p.Fill(ui.Rect{X: cx - bw/2, Y: min(a, b), W: bw, H: max(abs32(a-b), 1)}, col, 0)
|
||||
if memo.width != r.W || memo.indices == nil {
|
||||
memo.width = r.W
|
||||
memo.indices = internal.Columns(n, func(i int) float32 { return x(r, i) }, func(i int) float64 { return values[i] })
|
||||
}
|
||||
sparkBars(p, r, values, memo.indices, func(i int) float32 { return x(r, i) }, s, last, active, k)
|
||||
} else {
|
||||
chartRuns(values, func(i int) float32 { return x(r, i) }, func(idx []int) {
|
||||
var line ui.Path
|
||||
@@ -168,3 +171,34 @@ func Sparkline(c *ui.Context, values []float64, opts SparklineOptions) ChartResu
|
||||
st.last = res.Point
|
||||
return res
|
||||
}
|
||||
|
||||
type sparkMemo struct {
|
||||
id string
|
||||
lo, hi float64
|
||||
last int
|
||||
width float32
|
||||
indices []int
|
||||
}
|
||||
|
||||
// sparkBars highlights the last and active bars.
|
||||
func sparkBars(p *ui.Painter, r ui.Rect, values []float64, indices []int, x func(int) float32, s internal.Linear, last, active int, k theme.Tokens) {
|
||||
n := len(values)
|
||||
bw := max((r.W-6)/float32(n)-1, 1)
|
||||
zero := s.Map(0)
|
||||
internal.Rule(p, r.X, r.X+r.W, zero, k.Border)
|
||||
for _, i := range indices {
|
||||
v := values[i]
|
||||
if math.IsNaN(v) {
|
||||
continue
|
||||
}
|
||||
col := k.TextMuted
|
||||
if i == last || i == active {
|
||||
col = internal.Palette(k)[0]
|
||||
if v < 0 {
|
||||
col = k.Danger
|
||||
}
|
||||
}
|
||||
b := s.Map(v)
|
||||
p.Fill(ui.Rect{X: x(i) - bw/2, Y: min(zero, b), W: bw, H: max(abs32(zero-b), 1)}, col, 0)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user