Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Vrai-Prix iOS9// File : VraiPrix/Design/Plumb/GaugeScale.swift10// Description : échelle de la jauge de valeur — toujours centrée sur la11// valeur estimée (la valeur est la verticale), graduations12// rondes, position du prix. Pur et testable.13// Module : VraiPrix14// Repository : https://git.spboucher.ai/vrai-prix-ios-v315// Project Website : https://www.vrai-prix.com16// Created : 2026-11-2617// Last Modified : 2026-11-2818//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import CoreGraphics92import Foundation9394struct GaugeScale: Sendable, Equatable {95 var value: Double96 var low: Double97 var high: Double98 var price: Double?99 /// Dessin de la jauge. `classic` : API 2 (fourchette interrompue sous le plomb, graduations chiffrées).100 /// `interval` : API v3 (bande continue, bornes chiffrées sous l'axe). `approximate` : ordre de grandeur (bande tiretée).101 enum Style: Sendable, Equatable { case classic, interval, approximate }102 var style: Style = .classic103104 /// Demi-étendue : 1,12 × max(valeur − bas, haut − valeur, |prix − valeur|) ; un prix très éloigné ne105 /// tasse pas la fourchette (il est alors posé au bord, voir `priceOffScale`).106 var halfSpan: Double {107 let r = max(value - low, high - value, value * 0.02)108 var m = r109 if let p = price { m = max(m, min(abs(p - value), r * 2.4)) }110 return 1.12 * m111 }112113 var priceOffScale: Bool {114 guard let p = price else { return false }115 return abs(p - value) > halfSpan / 1.12 + 1116 }117118 /// Abscisse d'un montant sur une jauge de largeur `width` (bornée à la jauge).119 func x(_ amount: Double, width: CGFloat) -> CGFloat {120 let u = (amount - value) / halfSpan121 return width / 2 + CGFloat(min(1, max(-1, u))) * width / 2122 }123124 /// Abscisse du prix : au bord (à 96 %) s'il est hors échelle.125 func priceX(width: CGFloat) -> CGFloat? {126 guard let p = price else { return nil }127 if priceOffScale { return width / 2 + (p > value ? 1 : -1) * width / 2 * 0.96 }128 return x(p, width: width)129 }130131 struct Tick: Sendable, Equatable {132 var amount: Double133 var major: Bool134 }135136 /// Pas mineur : la plus petite valeur ronde donnant au plus `maxMinor` graduations ; majeur = 5 pas.137 func steps(maxMinor: Int = 24) -> (minor: Double, major: Double) {138 let span = 2 * halfSpan139 var base = 1000.0140 while true {141 for m in [1.0, 2.0, 5.0] {142 let step = base * m143 if span / step <= Double(maxMinor) { return (step, step * 5) }144 }145 base *= 10146 }147 }148149 func ticks(maxMinor: Int = 24) -> [Tick] {150 let s = steps(maxMinor: maxMinor)151 let lo = value - halfSpan, hi = value + halfSpan152 var out: [Tick] = []153 var k = (lo / s.minor).rounded(.up)154 while k * s.minor <= hi {155 let a = k * s.minor156 if a > 0 { out.append(Tick(amount: a, major: Int(k) % 5 == 0)) }157 k += 1158 }159 return out160 }161162 /// Étiquette d'une graduation : « 450 k$ », « 1,25 M$ ».163 static func label(_ amount: Double) -> String {164 if amount >= 1_000_000 {165 var s = String(format: "%.2f", amount / 1_000_000)166 while s.hasSuffix("0") { s.removeLast() }167 if s.hasSuffix(".") { s.removeLast() }168 return s.replacingOccurrences(of: ".", with: ",") + Fmt.nbsp + "M$"169 }170 return String(format: "%.0f", amount / 1000) + Fmt.nbsp + "k$"171 }172}173