Files
openchamber-android-native/third_party/mygo/ui/chart/stack.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

81 lines
2.0 KiB
Go

package chart
import "math"
// Segment is a value's stretch in a stack, from Lo up to Hi.
type Segment struct{ Lo, Hi float64 }
// Stacking is how Stack piles series up, as Swift Charts' stacking
// methods.
type Stacking int
const (
// StackStandard piles values up from 0, and negative values down from it.
StackStandard Stacking = iota
// StackNormalized piles them up as StackStandard does, in shares of the sum of
// their sizes: the stacks of positive values go from 0 to 1.
StackNormalized
// StackCenter piles them up around 0, as a streamgraph.
StackCenter
)
// Stack piles series up, as stacked bars and areas show them: series[s][i]
// is the i-th value of the s-th series, and the result's [s][i] is where
// it goes in the stack of the i-th values, the s-th from the bottom. NaN
// values take no room.
//
// stacked := chart.Stack([][]float64{apples, pears}, chart.StackStandard)
// for s, series := range stacked {
// for i, seg := range series {
// chart.Rect(c, x.Band(i), y.Span(seg.Lo, seg.Hi)).Background(colors[s])
// }
// }
func Stack(series [][]float64, how Stacking) [][]Segment {
n := 0
for _, s := range series {
n = max(n, len(s))
}
out := make([][]Segment, len(series))
for s := range series {
out[s] = make([]Segment, n)
}
value := func(s, i int) float64 {
if i >= len(series[s]) || math.IsNaN(series[s][i]) {
return 0
}
return series[s][i]
}
for i := range n {
up, down, total := 0.0, 0.0, 0.0
for s := range series {
v := value(s, i)
total += math.Abs(v)
switch {
case how == StackCenter:
out[s][i] = Segment{up, up + v}
up += v
case v < 0:
out[s][i] = Segment{down + v, down}
down += v
default:
out[s][i] = Segment{up, up + v}
up += v
}
}
shift, scale := 0.0, 1.0
switch how {
case StackCenter:
shift = -up / 2
case StackNormalized:
if total > 0 {
scale = 1 / total
}
}
for s := range series {
seg := &out[s][i]
seg.Lo, seg.Hi = (seg.Lo+shift)*scale, (seg.Hi+shift)*scale
}
}
return out
}