Files
pokerfreak/mac/Sources/Cockpit/StatistikKurven.swift
T
mmintelandClaude Opus 5.5 383f125769 Mac cockpit: catch up with WinUI (server-built live views, stakes, limits, sammler, statistik)
WinUI is ahead; the Swift app now draws what sicht/ precomputes instead of
computing its own views:
- Spielen page: live tables from sitzen.live (raster, kacheln, bereit, search,
  close question), "Gerade eben" from entscheidungen.feed, money from geld.anzeige
- Stakes priority with tables per stake (spielplan.stufen/stufen_tische);
  micro_only is gone
- Loss limits (deckel_einstellen), sammler in Zuschauer mode (main button
  becomes Sammeln starten/stoppen), "Nur offene Tische spielen", queue
- Statistik: full auswertung (key figures, EV/SD curve, bb/100 by ...,
  history, best times, play/opponent picture, tournaments)
- Small items: Spickzettel, streamer toggle, sendung_lesen, daily limits editor

Intentional deviations: "highest first" compares instead of 1e15 + bb (WinUI
cannot separate NL2 and NL5 there), a single table shows its tile, narrow
segment pickers fall back to menus. Plan: plans/2026-11-30_swift-winui-abgleich.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 09:55:51 +01:00

475 lines
21 KiB
Swift

import Charts
import CockpitKern
import SwiftUI
// Die Diagramme der Statistik-Seite (winui/Seiten/Diagramme.cs) mit Swift Charts: Kurven, Spannen, Baender,
// Saeulen, Anteilsbalken, Streu. Beim Zeigen ein Fadenkreuz bzw. ein Wertfenster.
/// Mehrere Linien auf gemeinsamer Achse, beim Zeigen ein Fadenkreuz mit allen Werten an der Stelle.
struct KurvenDiagramm: View {
var reihen: [KurvenReihe]
var xText: (Double) -> String
var yText: (Double) -> String
var xKopf: ((Double) -> String?)?
var yAchse: (Double) -> String = AuswertungText.kurzzahl
var hoehe: CGFloat = 280
@State private var auswahl: Double?
private var mit: [KurvenReihe] { reihen.filter { !$0.punkte.isEmpty } }
var body: some View {
if mit.isEmpty {
Text("Noch keine Punkte.").font(.caption).foregroundStyle(.secondary)
} else {
VStack(alignment: .leading, spacing: 6) {
diagramm.frame(height: hoehe)
legende
}
}
}
private var xBereich: ClosedRange<Double> {
let x0 = mit.map { $0.punkte[0].x }.min() ?? 0
var x1 = mit.map { $0.punkte[$0.punkte.count - 1].x }.max() ?? 1
if x1 - x0 < 1e-9 { x1 = x0 + 1 }
return x0...x1
}
/// Von der kleineren Null oder dem Tiefstwert bis zum Hoechstwert, mit etwas Luft.
private var yBereich: ClosedRange<Double> {
var lo = min(0, mit.flatMap(\.punkte).map(\.y).min() ?? 0)
var hi = max(0, mit.flatMap(\.punkte).map(\.y).max() ?? 0)
let spiel = (hi - lo) * 0.06
if lo < 0 { lo -= spiel }
hi += spiel
if hi - lo < 1e-9 { hi = lo + 1 }
return lo...hi
}
private func naechster(_ r: KurvenReihe, _ x: Double) -> KurvenReihe.Punkt? {
r.punkte.min { abs($0.x - x) < abs($1.x - x) }
}
private var diagramm: some View {
Chart {
ForEach(mit) { r in
if r.flaeche {
ForEach(Array(r.punkte.enumerated()), id: \.offset) { _, p in
AreaMark(x: .value("x", p.x), yStart: .value("Null", 0), yEnd: .value("y", p.y),
series: .value("Fläche", r.name))
.foregroundStyle(StatistikFarbe.ton(r.ton).opacity(0.10))
}
}
ForEach(Array(r.punkte.enumerated()), id: \.offset) { _, p in
LineMark(x: .value("x", p.x), y: .value(r.name, p.y), series: .value("Reihe", r.name))
.foregroundStyle(StatistikFarbe.ton(r.ton).opacity(r.gestrichelt ? 0.9 : 1))
.lineStyle(StrokeStyle(lineWidth: r.dicke, lineCap: .round, lineJoin: .round, dash: r.gestrichelt ? [3, 2] : []))
}
}
if let a = auswahl, a >= xBereich.lowerBound, a <= xBereich.upperBound {
RuleMark(x: .value("Auswahl", a))
.foregroundStyle(.white.opacity(0.35))
.annotation(position: .top, spacing: 2, overflowResolution: .init(x: .fit(to: .chart), y: .fit(to: .chart))) {
Wertfenster(zeilen: fensterZeilen(a))
}
ForEach(mit) { r in
if let p = naechster(r, a) {
PointMark(x: .value("x", p.x), y: .value(r.name, p.y))
.foregroundStyle(StatistikFarbe.ton(r.ton)).symbolSize(40)
}
}
}
}
.chartXSelection(value: $auswahl)
.chartXScale(domain: xBereich)
.chartYScale(domain: yBereich)
.chartXAxis {
AxisMarks(values: .automatic(desiredCount: 4)) { v in
AxisGridLine().foregroundStyle(.white.opacity(0.06))
AxisValueLabel { if let d = v.as(Double.self) { Text(xText(d)).font(.caption2) } }
}
}
.chartYAxis {
AxisMarks { v in
AxisGridLine().foregroundStyle(.white.opacity(0.06))
AxisValueLabel { if let d = v.as(Double.self) { Text(yAchse(d)).font(.caption2) } }
}
}
}
private func fensterZeilen(_ a: Double) -> [Wertfenster.Zeile] {
var z = [Wertfenster.Zeile(text: xKopf?(a) ?? xText(a), ton: nil)]
for r in mit {
if let p = naechster(r, a) { z.append(.init(text: "\(r.name) \(yText(p.y))", ton: r.ton)) }
}
return z
}
private var legende: some View {
HStack(spacing: 14) {
ForEach(mit) { r in
HStack(spacing: 5) {
Capsule().fill(StatistikFarbe.ton(r.ton)).frame(width: 16, height: r.gestrichelt ? 2 : 3)
.opacity(r.gestrichelt ? 0.7 : 1)
Text(r.name).font(.caption2).foregroundStyle(.secondary)
}
}
}
}
}
/// Eine Zeile eines Spannen-Diagramms (bb/100 mit 95 %).
struct SpannenZeile: Identifiable {
var name: String
var unter: String
var wert: Double?
var unten: Double?
var oben: Double?
var rechts: String
var haende = 1000.0
var id: String { name }
}
/// bb/100 mit 95-%-Spanne je Zeile auf gemeinsamer Achse; Null als Strich. Unter 30 Haenden bestimmt eine
/// Zeile die Achse nicht (sie wird blass).
struct SpannenDiagramm: View {
static let mindestHaende = 30.0
var zeilen: [SpannenZeile]
var einheit = "bb/100"
private var tragend: [SpannenZeile] { zeilen.filter { $0.haende >= Self.mindestHaende } }
private var achse: (lo: Double, hi: Double, rand: Double) {
let basis = tragend.isEmpty ? zeilen : tragend
let werte = basis.flatMap { [$0.unten, $0.oben, $0.wert].compactMap { $0 } } + [0.0]
var lo = werte.min() ?? -1, hi = werte.max() ?? 1
if hi - lo < 1e-9 { lo -= 1; hi += 1 }
let rand = (hi - lo) * 0.04
return (lo - rand, hi + rand, rand)
}
var body: some View {
if zeilen.isEmpty {
Text("Noch keine Hände in dieser Aufteilung.").font(.caption).foregroundStyle(.secondary)
} else {
let a = achse
VStack(alignment: .leading, spacing: 7) {
ForEach(zeilen) { z in zeile(z, a.lo, a.hi) }
achsenBeschriftung(a.lo, a.hi, a.rand)
}
}
}
private func zeile(_ z: SpannenZeile, _ lo: Double, _ hi: Double) -> some View {
let blass = z.haende < Self.mindestHaende && !tragend.isEmpty
let farbe = StatistikFarbe.ton(StatistikFarbe.ergebnis(z.wert))
return VStack(alignment: .leading, spacing: 3) {
HStack(spacing: 6) {
Text(z.name).font(.caption.weight(.semibold))
Text(z.unter).font(.caption2).foregroundStyle(.tertiary).lineLimit(1)
Spacer(minLength: 4)
Text(z.rechts).font(.caption.weight(.semibold).monospacedDigit()).foregroundStyle(farbe)
}
.opacity(blass ? 0.6 : 1)
GeometryReader { g in
let x = { (v: Double) in min(g.size.width, max(0, g.size.width * (v - lo) / (hi - lo))) }
ZStack(alignment: .leading) {
Capsule().fill(.quaternary).frame(height: 4)
Rectangle().fill(.secondary.opacity(0.6)).frame(width: 1, height: 12).offset(x: x(0))
if let u = z.unten, let o = z.oben {
Capsule().fill(farbe.opacity(0.35)).frame(width: max(2, x(o) - x(u)), height: 8).offset(x: x(u))
}
if let w = z.wert {
Circle().fill(farbe).frame(width: 9, height: 9).offset(x: min(g.size.width - 9, max(0, x(w) - 4.5)))
}
}
.frame(maxHeight: .infinity)
.opacity(blass ? 0.45 : 1)
}
.frame(height: 12)
.help(blass ? "unter 30 Händen: noch kein Urteil"
: z.unten.flatMap { u in z.oben.map { o in "\(Format.vorzeichen(z.wert, stellen: 1)) \(einheit), 95 %: \(Format.vorzeichen(u, stellen: 1)) bis \(Format.vorzeichen(o, stellen: 1))" } }
?? "zu wenig Hände für eine Spanne")
}
}
private func achsenBeschriftung(_ lo: Double, _ hi: Double, _ rand: Double) -> some View {
GeometryReader { g in
ForEach([lo + rand, 0.0, hi - rand], id: \.self) { v in
Text(AuswertungText.kurzzahl(v)).font(.caption2).foregroundStyle(.tertiary)
.position(x: min(g.size.width - 24, max(24, g.size.width * (v - lo) / (hi - lo))), y: 7)
}
}
.frame(height: 14)
}
}
/// Eine Zelle eines Band-Diagramms: Wert mit 95-%-Band, Stichprobe, Fenstertext; hervor = hinterlegt.
struct BandZelle: Identifiable {
var name: String
var wert: Double?
var unten: Double?
var oben: Double?
var haende: Double
var fenster: [String]
var hervor = false
var id: String { name }
}
/// Senkrechte Baender nebeneinander (je Stunde, Tag, ...): Balken = 95 %, Punkt = Wert. Zellen unter
/// `minHaende` sind blass und bestimmen die Achse nicht, damit duenne Zellen nicht glaenzen.
struct BaenderDiagramm: View {
var zellen: [BandZelle]
var minHaende: Double
var hoehe: CGFloat = 220
@State private var auswahl: Double?
private var bereich: ClosedRange<Double> {
var t = zellen.filter { $0.haende >= minHaende }
if t.isEmpty { t = zellen.filter { $0.haende >= SpannenDiagramm.mindestHaende } }
if t.isEmpty { t = zellen }
let werte = t.flatMap { [$0.unten, $0.oben, $0.wert].compactMap { $0 } } + [0.0]
var lo = werte.min() ?? -1, hi = werte.max() ?? 1
if hi - lo < 1e-9 { lo -= 1; hi += 1 }
let rand = (hi - lo) * 0.06
return (lo - rand)...(hi + rand)
}
private func blass(_ z: BandZelle) -> Bool { z.haende < minHaende }
var body: some View {
if zellen.isEmpty {
Text("Noch keine Werte.").font(.caption).foregroundStyle(.secondary)
} else {
let b = bereich
let jede = max(1, Int((Double(zellen.count) / 12).rounded(.up)))
Chart {
RuleMark(y: .value("Null", 0)).foregroundStyle(.white.opacity(0.25)).lineStyle(StrokeStyle(lineWidth: 1))
ForEach(Array(zellen.enumerated()), id: \.offset) { i, z in
let ton = StatistikFarbe.ton(StatistikFarbe.ergebnis(z.wert))
let op = blass(z) ? 0.3 : 1.0
if z.hervor {
RectangleMark(x: .value("i", Double(i)), yStart: .value("lo", b.lowerBound), yEnd: .value("hi", b.upperBound), width: .ratio(0.9))
.foregroundStyle(ton.opacity(0.08))
}
if let u = z.unten, let o = z.oben {
RuleMark(x: .value("i", Double(i)), yStart: .value("unten", min(b.upperBound, max(b.lowerBound, u))),
yEnd: .value("oben", min(b.upperBound, max(b.lowerBound, o))))
.lineStyle(StrokeStyle(lineWidth: 9, lineCap: .round))
.foregroundStyle(ton.opacity(0.32 * op))
}
if let w = z.wert {
PointMark(x: .value("i", Double(i)), y: .value("Wert", min(b.upperBound, max(b.lowerBound, w))))
.symbolSize(50).foregroundStyle(ton.opacity(op))
}
}
if let a = auswahl, case let i = Int(a.rounded()), zellen.indices.contains(i) {
RuleMark(x: .value("Auswahl", Double(i)))
.foregroundStyle(.white.opacity(0.06))
.lineStyle(StrokeStyle(lineWidth: 18))
.annotation(position: .top, spacing: 2, overflowResolution: .init(x: .fit(to: .chart), y: .fit(to: .chart))) {
Wertfenster(zeilen: fenster(zellen[i]))
}
}
}
.chartXSelection(value: $auswahl)
.chartXScale(domain: -0.5...(Double(zellen.count) - 0.5))
.chartYScale(domain: b)
.chartXAxis {
AxisMarks(values: Array(stride(from: 0, to: zellen.count, by: jede)).map(Double.init)) { v in
AxisValueLabel { if let d = v.as(Double.self), zellen.indices.contains(Int(d)) { Text(zellen[Int(d)].name).font(.caption2) } }
}
}
.chartYAxis {
AxisMarks { v in
AxisGridLine().foregroundStyle(.white.opacity(0.06))
AxisValueLabel { if let d = v.as(Double.self) { Text(AuswertungText.kurzzahl(d)).font(.caption2) } }
}
}
.frame(height: hoehe)
}
}
private func fenster(_ z: BandZelle) -> [Wertfenster.Zeile] {
let ton = StatistikFarbe.ergebnis(z.wert)
var r = z.fenster.enumerated().map { Wertfenster.Zeile(text: $1, ton: $0 == 1 ? ton : nil) }
if blass(z) { r.append(.init(text: "unter \(Format.zahl(minHaende, stellen: 0)) Händen: kein Urteil", ton: nil)) }
return r
}
}
/// Eine Saeule mit Vorzeichen (gruen ueber, rot unter null) und Wertfenster beim Zeigen.
struct Saeule: Identifiable {
var name: String
var wert: Double
var fenster: [String]
var ton: String?
var id = UUID()
var farbe: Color { StatistikFarbe.ton(ton ?? (wert >= 0 ? "gruen" : "rot")) }
}
struct SaeulenDiagramm: View {
var saeulen: [Saeule]
var hoehe: CGFloat = 200
@State private var auswahl: Double?
var body: some View {
if saeulen.isEmpty {
Text("Noch keine Werte.").font(.caption).foregroundStyle(.secondary)
} else {
let jede = max(1, Int((Double(saeulen.count) / 12).rounded(.up)))
Chart {
ForEach(Array(saeulen.enumerated()), id: \.offset) { i, s in
BarMark(x: .value("i", Double(i)), y: .value("Wert", s.wert), width: .ratio(0.8))
.foregroundStyle(s.farbe.opacity(0.75)).cornerRadius(2)
}
if let a = auswahl, case let i = Int(a.rounded()), saeulen.indices.contains(i) {
RuleMark(x: .value("Auswahl", Double(i)))
.foregroundStyle(.white.opacity(0.06))
.lineStyle(StrokeStyle(lineWidth: 14))
.annotation(position: .top, spacing: 2, overflowResolution: .init(x: .fit(to: .chart), y: .fit(to: .chart))) {
let s = saeulen[i]
Wertfenster(zeilen: s.fenster.enumerated().map { .init(text: $1, ton: $0 == 0 ? nil : (s.ton ?? (s.wert >= 0 ? "gruen" : "rot"))) })
}
}
}
.chartXSelection(value: $auswahl)
.chartXScale(domain: -0.5...(Double(saeulen.count) - 0.5))
.chartXAxis {
AxisMarks(values: Array(stride(from: 0, to: saeulen.count, by: jede)).map(Double.init)) { v in
AxisValueLabel { if let d = v.as(Double.self), saeulen.indices.contains(Int(d)) { Text(saeulen[Int(d)].name).font(.caption2) } }
}
}
.chartYAxis {
AxisMarks { v in
AxisGridLine().foregroundStyle(.white.opacity(0.06))
AxisValueLabel { if let d = v.as(Double.self) { Text(AuswertungText.kurzzahl(d)).font(.caption2) } }
}
}
.frame(height: hoehe)
}
}
}
/// Gestapelter Anteilsbalken mit Legende (Gegnertypen).
struct AnteilsBalken: View {
struct Teil: Identifiable {
var name: String
var anteil: Double
var ton: String
var zusatz: String
var id: String { name }
}
var teile: [Teil]
var body: some View {
let summe = max(1e-9, teile.reduce(0) { $0 + $1.anteil })
VStack(alignment: .leading, spacing: 8) {
GeometryReader { g in
HStack(spacing: 0) {
ForEach(teile.filter { $0.anteil > 0 }) { t in
Rectangle().fill(StatistikFarbe.ton(t.ton).opacity(0.85))
.frame(width: g.size.width * t.anteil / summe)
.help("\(t.name): \(Format.prozent(t.anteil / summe)) · \(t.zusatz)")
}
}
.clipShape(Capsule())
}
.frame(height: 14)
ViewThatFits(in: .horizontal) {
legende
VStack(alignment: .leading, spacing: 3) { legende }
}
}
}
private var legende: some View {
let summe = max(1e-9, teile.reduce(0) { $0 + $1.anteil })
return HStack(spacing: 12) {
ForEach(teile) { t in
HStack(spacing: 5) {
Circle().fill(StatistikFarbe.ton(t.ton)).frame(width: 8, height: 8)
Text("\(t.name) \(Format.prozent(t.anteil / summe))").font(.caption2).foregroundStyle(.secondary)
}
}
}
}
}
/// Streudiagramm 0..max auf beiden Achsen (VPIP gegen PFR), dazu markierte Punkte.
struct StreuDiagramm: View {
struct Marke: Identifiable {
var vpip: Double
var pfr: Double
var ton: String
var id: String { ton }
}
var punkte: [Gegnerbild.Streupunkt]
var marken: [Marke]
var xName = "VPIP"
var yName = "PFR"
var maximum = 0.8
var hoehe: CGFloat = 240
@State private var treffer: Int?
var body: some View {
Chart {
LineMark(x: .value(xName, 0), y: .value(yName, 0), series: .value("Diagonale", "d"))
.foregroundStyle(.white.opacity(0.1)).lineStyle(StrokeStyle(lineWidth: 1, dash: [3, 3]))
LineMark(x: .value(xName, maximum), y: .value(yName, maximum), series: .value("Diagonale", "d"))
.foregroundStyle(.white.opacity(0.1)).lineStyle(StrokeStyle(lineWidth: 1, dash: [3, 3]))
ForEach(Array(punkte.enumerated()), id: \.offset) { _, p in
let d = 4 + 6 * p.groesse.clamped(0, 1).squareRoot()
PointMark(x: .value(xName, p.vpip), y: .value(yName, p.pfr))
.symbolSize(d * d).foregroundStyle(StatistikFarbe.ton(p.ton).opacity(0.55))
}
ForEach(marken) { m in
PointMark(x: .value(xName, m.vpip), y: .value(yName, m.pfr))
.symbol(.diamond).symbolSize(110).foregroundStyle(StatistikFarbe.ton(m.ton))
}
if let i = treffer, punkte.indices.contains(i) {
let p = punkte[i]
PointMark(x: .value(xName, p.vpip), y: .value(yName, p.pfr))
.symbolSize(200).foregroundStyle(.clear)
.annotation(position: .top, spacing: 4, overflowResolution: .init(x: .fit(to: .chart), y: .fit(to: .chart))) {
Wertfenster(zeilen: [.init(text: p.text, ton: nil),
.init(text: "\(xName) \(Format.prozent(p.vpip)) · \(yName) \(Format.prozent(p.pfr))", ton: p.ton)])
}
}
}
.chartXScale(domain: 0...maximum)
.chartYScale(domain: 0...maximum)
.chartXAxis { AxisMarks(values: .automatic(desiredCount: 5)) { v in
AxisGridLine().foregroundStyle(.white.opacity(0.06))
AxisValueLabel { if let d = v.as(Double.self) { Text(Format.prozent(d)).font(.caption2) } }
} }
.chartYAxis { AxisMarks(values: .automatic(desiredCount: 5)) { v in
AxisGridLine().foregroundStyle(.white.opacity(0.06))
AxisValueLabel { if let d = v.as(Double.self) { Text(Format.prozent(d)).font(.caption2) } }
} }
.chartXAxisLabel(xName, alignment: .trailing)
.chartYAxisLabel(yName, position: .top)
.chartOverlay { proxy in
GeometryReader { g in
Color.clear.contentShape(Rectangle())
.onContinuousHover { phase in
guard case .active(let p) = phase, let rahmen = proxy.plotFrame else { treffer = nil; return }
let o = g[rahmen].origin
guard let x: Double = proxy.value(atX: p.x - o.x), let y: Double = proxy.value(atY: p.y - o.y) else { treffer = nil; return }
let grenze = maximum * 0.04
treffer = punkte.indices
.map { ($0, hypot(punkte[$0].vpip - x, punkte[$0].pfr - y)) }
.filter { $0.1 < grenze }.min { $0.1 < $1.1 }?.0
}
}
}
.frame(height: hoehe)
}
}
private extension Double {
func clamped(_ lo: Double, _ hi: Double) -> Double { Swift.min(hi, Swift.max(lo, self)) }
}