SPB Git

Repository Public

ChartDrawingRender.swift 6.8 KB 99 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Charting/ChartDrawingRender.swift3// Description: How each drawing tool looks on the chart (trend line, horizontal line, ray, rectangle, Fibonacci retracement, text note, measure), with selection handles. Positions come from the projection of time and price, never from stored pixels.4import SwiftUI5import VQuantCore6import VQuantUI78@MainActor enum ChartDrawingRender {9    static let noteFont = Font.system(size: 11, weight: .medium)10    static let labelFont = Font.system(size: 10, weight: .medium, design: .monospaced)1112    /// Width of a note's label on screen (hit-testing uses the same measure).13    static func noteWidth(_ text: String) -> CGFloat {14        let s = (text.isEmpty ? "Note" : text) as NSString15        return min(260, s.size(withAttributes: [.font: NSFont.systemFont(ofSize: 11, weight: .medium)]).width) + 2216    }1718    static func measureText(_ d: ChartDrawing, model: ChartModel) -> String {19        guard d.points.count > 1 else { return "" }20        let m = DrawingMath.measure(d.points[0], d.points[1], times: model.times, step: model.stepSeconds)21        var parts = ["Δ " + Fmt.signed(m.deltaPrice, digits: abs(m.deltaPrice) >= 1 ? 2 : 4)]22        if let f = m.deltaFraction { parts.append(Fmt.percent(f * 100)) }23        parts.append("\(abs(m.bars)) bar\(abs(m.bars) == 1 ? "" : "s")")24        parts.append(DrawingMath.durationText(m.seconds))25        return parts.joined(separator: " · ")26    }2728    /// Draws one drawing inside the price panel (the context is already clipped to it). Returns a tag for the price29    /// axis when the drawing marks a single level (a horizontal line).30    static func draw(_ d: ChartDrawing, in ctx: inout GraphicsContext, proj: ChartProjection, plot: CGRect, selected: Bool, draft: Bool, editing: Bool, model: ChartModel) -> (String, CGFloat, Color)? {31        guard let first = d.points.first else { return nil }32        let color = ChartColors.drawing(d.color)33        let a = proj.point(first)34        let b = d.points.count > 1 ? proj.point(d.points[1]) : a35        let width: CGFloat = selected ? 2 : 1.436        let stroke = StrokeStyle(lineWidth: width, lineCap: .round, dash: draft ? [5, 4] : [])37        var tag: (String, CGFloat, Color)?3839        switch d.kind {40        case .hline:41            ctx.stroke(Path { $0.move(to: CGPoint(x: plot.minX, y: a.y)); $0.addLine(to: CGPoint(x: plot.maxX, y: a.y)) }, with: .color(color), style: stroke)42            tag = (Fmt.price(first.p), a.y, color)43        case .trend:44            ctx.stroke(Path { $0.move(to: a); $0.addLine(to: b) }, with: .color(color), style: stroke)45        case .ray:46            let e = DrawingMath.rayEnd(ScreenPoint(x: a.x, y: a.y), ScreenPoint(x: b.x, y: b.y), width: plot.maxX, height: plot.maxY)47            ctx.stroke(Path { $0.move(to: a); $0.addLine(to: CGPoint(x: e.x, y: e.y)) }, with: .color(color), style: stroke)48        case .rect:49            let r = CGRect(x: min(a.x, b.x), y: min(a.y, b.y), width: abs(b.x - a.x), height: abs(b.y - a.y))50            ctx.fill(Path(r), with: .color(color.opacity(0.10)))51            ctx.stroke(Path(r), with: .color(color), style: stroke)52        case .fib:53            let x0 = min(a.x, b.x), x1 = max(max(a.x, b.x), x0 + 40)54            ctx.fill(Path(CGRect(x: x0, y: min(a.y, b.y), width: x1 - x0, height: abs(b.y - a.y))), with: .color(color.opacity(0.05)))55            ctx.stroke(Path { $0.move(to: a); $0.addLine(to: b) }, with: .color(color.opacity(0.45)), style: StrokeStyle(lineWidth: 1, dash: [2, 3]))56            for l in DrawingMath.fibLevels(from: first.p, to: d.points.count > 1 ? d.points[1].p : first.p) {57                let yy = proj.scale.y(price: l.price)58                let major = l.ratio == 0 || l.ratio == 1 || l.ratio == 0.5 || l.ratio == 0.61859                ctx.stroke(Path { $0.move(to: CGPoint(x: x0, y: yy)); $0.addLine(to: CGPoint(x: x1, y: yy)) }, with: .color(color.opacity(major ? 1 : 0.65)), style: StrokeStyle(lineWidth: major ? width : 1, dash: draft ? [5, 4] : []))60                let label = (l.ratio * 100).formatted(.number.precision(.fractionLength(0...1))) + " %  " + Fmt.price(l.price)61                ctx.draw(Text(label).font(.system(size: 9.5, weight: .medium, design: .monospaced)).foregroundStyle(color), at: CGPoint(x: x0 + 4, y: yy - 6), anchor: .leading)62            }63        case .text:64            let text = editing ? "" : (d.text ?? "")65            ctx.fill(Path(ellipseIn: CGRect(x: a.x - 3, y: a.y - 3, width: 6, height: 6)), with: .color(color))66            if !editing {67                let t = ctx.resolve(Text(text.isEmpty ? "Note" : text).font(noteFont).foregroundStyle(VQTokens.Palette.text1))68                let sz = t.measure(in: CGSize(width: 260, height: 60))69                let r = CGRect(x: a.x + 8, y: a.y - sz.height / 2 - 3, width: sz.width + 12, height: sz.height + 6)70                ctx.fill(Path(roundedRect: r, cornerRadius: 4), with: .color(VQTokens.Palette.ink2.opacity(0.94)))71                ctx.stroke(Path(roundedRect: r, cornerRadius: 4), with: .color(color), lineWidth: selected ? 1.6 : 1)72                ctx.draw(t, in: r.insetBy(dx: 6, dy: 3))73            }74        case .measure:75            let r = CGRect(x: min(a.x, b.x), y: min(a.y, b.y), width: abs(b.x - a.x), height: abs(b.y - a.y))76            ctx.fill(Path(r), with: .color(color.opacity(0.08)))77            ctx.stroke(Path { $0.move(to: a); $0.addLine(to: b) }, with: .color(color), style: StrokeStyle(lineWidth: width, lineCap: .round, dash: [5, 3]))78            ctx.stroke(Path { $0.move(to: CGPoint(x: a.x, y: a.y)); $0.addLine(to: CGPoint(x: b.x, y: a.y)); $0.addLine(to: CGPoint(x: b.x, y: b.y)) }, with: .color(color.opacity(0.5)), style: StrokeStyle(lineWidth: 0.8, dash: [2, 3]))79            let t = ctx.resolve(Text(measureText(d, model: model)).font(labelFont).foregroundStyle(VQTokens.Palette.ink1))80            let sz = t.measure(in: CGSize(width: 420, height: 40))81            var origin = CGPoint(x: (a.x + b.x) / 2 - sz.width / 2 - 6, y: min(a.y, b.y) - sz.height - 12)82            if origin.y < plot.minY + 2 { origin.y = max(a.y, b.y) + 6 }83            origin.x = min(max(origin.x, plot.minX + 2), plot.maxX - sz.width - 14)84            let box = CGRect(origin: origin, size: CGSize(width: sz.width + 12, height: sz.height + 6))85            ctx.fill(Path(roundedRect: box, cornerRadius: 4), with: .color(color))86            ctx.draw(t, at: CGPoint(x: box.midX, y: box.midY), anchor: .center)87        }8889        if selected, !draft {90            for p in d.points.prefix(d.kind.pointsNeeded).map(proj.point) {91                let h = CGRect(x: p.x - 4.5, y: p.y - 4.5, width: 9, height: 9)92                ctx.fill(Path(ellipseIn: h), with: .color(VQTokens.Palette.ink2))93                ctx.stroke(Path(ellipseIn: h), with: .color(color), lineWidth: 1.8)94            }95        }96        return tag97    }98}99
Files main
Menu

Theme