import Foundation import Charts import CockpitKern import SwiftUI /// Ergebnis und Stil unter einem Reiter. struct StatistikAnsicht: View { enum Teil: String, CaseIterable { case ergebnis = "Ergebnis", stil = "Stil" } @AppStorage("statistik.teil") private var teil: Teil = .ergebnis var body: some View { VStack(spacing: 0) { Picker("", selection: $teil) { ForEach(Teil.allCases, id: \.self) { Text($0.rawValue) } } .pickerStyle(.segmented).labelsHidden().padding(.horizontal, 12).padding(.top, 10) switch teil { case .ergebnis: ErgebnisAnsicht() case .stil: StilAnsicht() } } } } /// bb/100 ueber alle Stakes, gewichtet nach Haenden. enum BB100 { static func gesamt(_ z: [StakeZeile]) -> Double? { let n = z.reduce(0) { $0 + ($1.haende ?? 0) } guard n > 0 else { return nil } return z.reduce(0.0) { $0 + ($1.bb100 ?? 0) * Double($1.haende ?? 0) } / Double(n) } } struct StilAnsicht: View { enum Teil: String, CaseIterable { case anteile = "Anteile", verlauf = "Verlauf", groessen = "Größen", denkzeit = "Denkzeit" } @Environment(Cockpit.self) private var cockpit @AppStorage("stil.teil") private var teil: Teil = .anteile private static let aktionen: [(String, KeyPath, Color)] = [ ("Fold", \.fold, .gray), ("Check", \.check, .teal), ("Call", \.call, .blue), ("Raise", \.raise, .orange), ("All-in", \.allin, .red), ] var body: some View { let s = cockpit.ansicht.stil ScrollView { VStack(alignment: .leading, spacing: 12) { Picker("", selection: $teil) { ForEach(Teil.allCases, id: \.self) { Text($0.rawValue) } } .pickerStyle(.segmented).labelsHidden() switch teil { case .anteile: anteile(s?.anteile) case .verlauf: verlauf(s?.verlauf ?? []) case .groessen: groessen(s?.groessen ?? []) case .denkzeit: denkzeit(s) } } .padding(12) } .overlay { if s == nil || (s?.anteile?.n ?? 0) == 0 { Leer(titel: "Noch kein Stil", text: s?.leer ?? "Erst nach den ersten Entscheidungen.").background(.regularMaterial) } } } private func anteile(_ a: Anteile?) -> some View { VStack(alignment: .leading, spacing: 8) { ForEach(Self.aktionen, id: \.0) { name, pfad, farbe in let v = a?[keyPath: pfad] ?? 0 HStack { Text(name).frame(width: 52, alignment: .leading).font(.callout) Balken(anteil: v, farbe: farbe) Text(Format.prozent(v)).font(.caption.monospacedDigit()).frame(width: 40, alignment: .trailing) } } if let n = a?.n { Text("aus \(n) Entscheidungen").font(.caption).foregroundStyle(.secondary) } } } private func verlauf(_ v: [Anteile]) -> some View { VStack(alignment: .leading, spacing: 6) { verlaufDiagramm(v).frame(height: 240) Fussnote("Blöcke zu 20 Entscheidungen") } } private func verlaufDiagramm(_ v: [Anteile]) -> some View { Chart { ForEach(Array(v.enumerated()), id: \.offset) { i, b in ForEach(Self.aktionen, id: \.0) { name, pfad, _ in AreaMark(x: .value("Block", i + 1), y: .value("Anteil", b[keyPath: pfad]), stacking: .normalized) .foregroundStyle(by: .value("Aktion", name)) } } } .chartForegroundStyleScale(domain: Self.aktionen.map(\.0), range: Self.aktionen.map(\.2)) .chartLegend(position: .bottom) } private func groessen(_ g: [Stil.Groesse]) -> some View { let p = g.compactMap { x -> (Double, Double)? in guard let a = x.topfAnteil, let b = x.betragBB else { return nil } return (a, b) } return Group { if p.isEmpty { Text("Noch keine Einsätze mit Topfgröße.").font(.caption).foregroundStyle(.secondary) } else { VStack(alignment: .leading, spacing: 6) { Chart(Array(p.enumerated()), id: \.offset) { _, q in PointMark(x: .value("Topfanteil", q.0), y: .value("bb", q.1)).symbolSize(28).foregroundStyle(.blue.opacity(0.7)) } .chartXScale(domain: min(0, p.map(\.0).min() ?? 0)...max(p.map(\.0).max() ?? 1, 1e-9)) .chartYScale(domain: 0...max(p.map(\.1).max() ?? 1, 1e-9)) .chartXAxis { AxisMarks { v in AxisGridLine().foregroundStyle(.white.opacity(0.06)) AxisValueLabel { if let d = v.as(Double.self) { Text(Format.prozent(d)).font(.caption2) } } } } .chartYAxis { AxisMarks { v in AxisGridLine().foregroundStyle(.white.opacity(0.06)) AxisValueLabel { if let d = v.as(Double.self) { Text(Format.zahl(d, stellen: 0) + " bb").font(.caption2) } } } } .frame(height: 240) Fussnote("x: Anteil am Topf · y: Einsatz in bb") } } } } /// Haeufigkeit je voller Sekunde, auch die Sekunden ohne Entscheidung dazwischen (als Null). private static func sekunden(_ d: [Double]) -> [(Int, Int)] { let je = Dictionary(grouping: d.filter(\.isFinite).map { Int($0.rounded(.down)) }, by: { $0 }).mapValues(\.count) guard let von = je.keys.min(), let bis = je.keys.max() else { return [] } return (von...bis).map { ($0, je[$0] ?? 0) } } private func denkzeit(_ s: Stil?) -> some View { VStack(alignment: .leading, spacing: 10) { HStack(spacing: 8) { Kennzahl(titel: "Median", wert: (s?.denkzeit?.median).map { Format.zahl($0, stellen: 1) + " s" } ?? "–") Kennzahl(titel: "Streuung", wert: (s?.denkzeit?.streuung).map { Format.zahl($0, stellen: 1) + " s" } ?? "–") Kennzahl(titel: "VK", wert: Format.zahl(s?.denkzeit?.vk)) } Chart(Self.sekunden(s?.denkzeiten ?? []), id: \.0) { sekunde, anzahl in BarMark(x: .value("Sekunden", sekunde), y: .value("Anzahl", anzahl)) } .frame(height: 200) Fussnote("Gedenkzeit, volle Sekunden") Text("Ein VK deutlich über 0 heißt: nicht maschinell gleichmäßig.").font(.caption).foregroundStyle(.secondary) } } } struct Balken: View { var anteil: Double var farbe: Color var body: some View { GeometryReader { g in ZStack(alignment: .leading) { Capsule().fill(.quaternary) Capsule().fill(farbe).frame(width: g.size.width * min(1, max(0, anteil))) } } .frame(height: 8) } }