Files
Lyrebird/Sources/LyrebirdUI/LyrebirdScatterView.swift
T
2026-06-01 18:27:40 -07:00

120 lines
3.8 KiB
Swift

// LyrebirdScatterView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
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<Double>?
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<Double>? = 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..<count {
let px: CGFloat = CGFloat(i) / CGFloat(count - 1) * size.width
let py: CGFloat = _yFor(samples[i], yMin: yMin, yMax: yMax, height: size.height)
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))
}
}
// MARK: - Helpers
private func _yBounds() -> (Double, Double) {
if let r: ClosedRange<Double> = 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))
}
}