// LyrebirdScatterView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if canImport(SwiftUI) import SwiftUI /// A Canvas-based scatter plot for an array of `Double` values. /// /// Each sample is plotted as a filled circle. X position reflects sample index; /// Y position reflects value relative to the displayed range. /// /// ```swift /// let data = DemandTemporalTendency(distribution: .gauss, /// parX: .value(0.5), parY: .value(0.0), seed: 1) /// .samples(count: 200) /// LyrebirdScatterView(data) /// .padding() /// ``` public struct LyrebirdScatterView: View { private let samples: [Double] private let yRange: ClosedRange? private let pointColor: Color private let showZeroLine: Bool private let width: CGFloat private let height: CGFloat private static let backgroundColor: Color = Color(white: 0.08) private static let cornerRadius: CGFloat = 4 private static let scatterDotRadius: CGFloat = 2.5 private static let zeroLineOpacity: Double = 0.25 private static let guideLineWidth: CGFloat = 0.5 public init( _ samples: [Double], yRange: ClosedRange? = nil, pointColor: Color = .cyan, showZeroLine: Bool = true, width: CGFloat = 300, height: CGFloat = 120 ) { self.samples = samples self.yRange = yRange self.pointColor = pointColor self.showZeroLine = showZeroLine self.width = width self.height = height } public var body: some View { Canvas { ctx, size in _draw(ctx: ctx, size: size) } .frame(width: width, height: height) .background(LyrebirdScatterView.backgroundColor) .clipShape(RoundedRectangle(cornerRadius: LyrebirdScatterView.cornerRadius)) } // MARK: - Drawing private func _draw( ctx: GraphicsContext, size: CGSize ) { guard !samples.isEmpty else { return } let (yMin, yMax) = _yBounds() if showZeroLine && yMin <= 0 && yMax >= 0 { let zy: CGFloat = _yFor(0, yMin: yMin, yMax: yMax, height: size.height) var guide: Path = Path() guide.move(to: CGPoint(x: 0, y: zy)) guide.addLine(to: CGPoint(x: size.width, y: zy)) ctx.stroke(guide, with: .color(.white.opacity(LyrebirdScatterView.zeroLineOpacity)), lineWidth: LyrebirdScatterView.guideLineWidth) } let count: Int = samples.count let r: CGFloat = LyrebirdScatterView.scatterDotRadius if count == 1 { let px: CGFloat = size.width / 2 let py: CGFloat = size.height / 2 var dot: Path = Path() dot.addEllipse(in: CGRect(x: px - r, y: py - r, width: 2 * r, height: 2 * r)) ctx.fill(dot, with: .color(pointColor)) return } for i: Int in 0.. (Double, Double) { if let r: ClosedRange = yRange { return (r.lowerBound, r.upperBound) } return _envelopeYBounds(levels: samples) } private func _yFor( _ v: Double, yMin: Double, yMax: Double, height: CGFloat ) -> CGFloat { guard yMax > yMin else { return height / 2 } let t: Double = (v - yMin) / (yMax - yMin) return height * (1.0 - CGFloat(t)) } } #else // Linux/GTK4 counterpart to `LyrebirdUI.LyrebirdScatterView`: a Canvas scatter // plot of an array of `Double`s, one filled dot per sample. The SwiftUI // `GraphicsContext`/`Path` drawing is rewritten to imperative `DrawingContext` // (Cairo) calls: a background fill, an optional zero guide line, and one // `arc`+`fill` per dot. Y-bounds reuse the shared `_linuxEnvelopeYBounds`. // // No axis text is drawn (`DrawingContext` has no text primitive) — the Apple // side draws none either, so there is no feature loss here. // // SwiftOpenUI + BackendGTK4 are always present in this target (see Package.swift). import SwiftOpenUI import BackendGTK4 import Foundation // CGSize / CGFloat /// A Canvas scatter plot for the Linux/GTK4 UI. Mirrors /// `LyrebirdUI.LyrebirdScatterView`; same public API/init as the Apple side. public struct LyrebirdScatterView: View { private let samples: [Double] private let yRange: ClosedRange? private let pointColor: Color private let showZeroLine: Bool private let width: CGFloat private let height: CGFloat // Background is a fixed dark panel; drawn inside the renderer since the // Cairo backend has no `clipShape`. private static let backgroundRGB: (r: Double, g: Double, b: Double) = (0.08, 0.08, 0.08) private static let scatterDotRadius: Double = 2.5 private static let zeroLineOpacity: Double = 0.25 private static let guideLineWidth: Double = 0.5 public init( _ samples: [Double], yRange: ClosedRange? = nil, pointColor: Color = .cyan, showZeroLine: Bool = true, width: CGFloat = 300, height: CGFloat = 120 ) { self.samples = samples self.yRange = yRange self.pointColor = pointColor self.showZeroLine = showZeroLine self.width = width self.height = height } public var body: some View { Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in _draw(ctx: ctx, size: size) }) .frame(width: Double(width), height: Double(height)) } // MARK: - Drawing private func _draw( ctx: DrawingContext, size: CGSize ) { let w: Double = Double(size.width) let h: Double = Double(size.height) // Background panel. let bg: (r: Double, g: Double, b: Double) = LyrebirdScatterView.backgroundRGB ctx.setColor(r: bg.r, g: bg.g, b: bg.b) ctx.rectangle(x: 0, y: 0, width: w, height: h) ctx.fill() guard !samples.isEmpty else { return } let (yMin, yMax): (Double, Double) = _yBounds() if showZeroLine && yMin <= 0 && yMax >= 0 { let zy: Double = _yFor(0, yMin: yMin, yMax: yMax, height: h) ctx.setColor(r: 1.0, g: 1.0, b: 1.0, a: LyrebirdScatterView.zeroLineOpacity) ctx.setLineWidth(LyrebirdScatterView.guideLineWidth) ctx.moveTo(x: 0, y: zy) ctx.lineTo(x: w, y: zy) ctx.stroke() } let count: Int = samples.count let r: Double = LyrebirdScatterView.scatterDotRadius let fullCircle: Double = 2.0 * Double.pi ctx.setColor(r: pointColor.red, g: pointColor.green, b: pointColor.blue, a: pointColor.alpha) if count == 1 { let px: Double = w / 2 let py: Double = h / 2 ctx.arc(centerX: px, centerY: py, radius: r, startAngle: 0, endAngle: fullCircle) ctx.fill() return } for i: Int in 0.. (Double, Double) { if let r: ClosedRange = yRange { return (r.lowerBound, r.upperBound) } return _linuxEnvelopeYBounds(levels: samples) } private func _yFor( _ v: Double, yMin: Double, yMax: Double, height: Double ) -> Double { guard yMax > yMin else { return height / 2 } let t: Double = (v - yMin) / (yMax - yMin) return height * (1.0 - t) } } #endif