SPB Git

Repository Public

main.swift 7.0 KB 153 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetCharts/Sources/chart-gallery/main.swift10// Module           : chart-gallery11// Repository       : https://git.spboucher.ai/orix-sheet12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import SheetCharts8990// chart-gallery <output-directory>91// Writes one PNG (2x) + SVG + PDF per chart kind (light theme), a dark PNG per kind, and three multi-panel92// figures. All data is synthetic and every chart says so ("Sample data").9394let args = CommandLine.arguments95guard args.count >= 2 else {96    FileHandle.standardError.write(Data("usage: chart-gallery <output-directory>\n".utf8))97    exit(2)98}99let out = URL(fileURLWithPath: args[1], isDirectory: true)100try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)101let source = LiteralDataSource()102let size = CGSize(width: 640, height: 400)103var failures = 0104105@MainActor func write(_ data: Data?, _ name: String) {106    guard let data else { print("FAILED \(name)"); failures += 1; return }107    do { try data.write(to: out.appendingPathComponent(name)) } catch { print("FAILED \(name): \(error)"); failures += 1 }108}109110for kind in ChartKind.allCases {111    let spec = ChartSamples.sample(kind)112    let t0 = Date()113    write(ChartRenderer.pngData(spec, source: source, size: size, scale: 2), "\(kind.rawValue).png")114    write(Data(ChartRenderer.svgString(spec, source: source, size: size).utf8), "\(kind.rawValue).svg")115    write(ChartRenderer.pdfData(spec, source: source, size: size), "\(kind.rawValue).pdf")116    write(ChartRenderer.pngData(ChartSamples.sample(kind, theme: .dark), source: source, size: size, scale: 2), "\(kind.rawValue)-dark.png")117    print(String(format: "%-12@ %6.1f ms", kind.rawValue as NSString, Date().timeIntervalSince(t0) * 1000))118}119for (name, spec) in ChartSamples.extras() {120    write(ChartRenderer.pngData(spec, source: source, size: size, scale: 2), "\(name).png")121    write(Data(ChartRenderer.svgString(spec, source: source, size: size).utf8), "\(name).svg")122    write(ChartRenderer.pdfData(spec, source: source, size: size), "\(name).pdf")123}124for (name, figure) in ChartSamples.figures() {125    write(FigureRenderer.pngData(figure, source: source, scale: max(2, figure.exportPreset.scale / 2)), "\(name).png")126    write(Data(FigureRenderer.svgString(figure, source: source).utf8), "\(name).svg")127    write(FigureRenderer.pdfData(figure, source: source), "\(name).pdf")128    print("\(name)  \(Int(figure.width)) x \(Int(figure.height)) pt")129}130131// performance: 100,000-point series (resolve + layout + draw), best of 3132func time(_ body: () -> Void) -> Double {133    var best = Double.infinity134    for _ in 0..<3 { let t = Date(); body(); best = min(best, Date().timeIntervalSince(t) * 1000) }135    return best136}137for kind in [ChartKind.line, .scatter] {138    let spec = ChartSamples.largeSeries(kind, count: 100_000)139    var noDecimation = spec140    noDecimation.options.decimation = false141    let png = time { _ = ChartRenderer.pngData(spec, source: source, size: size, scale: 2) }142    let pdf = time { _ = ChartRenderer.pdfData(spec, source: source, size: size) }143    let svg = time { _ = ChartRenderer.svgString(spec, source: source, size: size) }144    print(String(format: "100k %@: png %.0f ms, pdf %.0f ms, svg %.0f ms", kind.rawValue as NSString, png, pdf, svg))145    if kind == .line {146        let raw = time { _ = ChartRenderer.pngData(noDecimation, source: source, size: size, scale: 2) }147        print(String(format: "100k line without decimation: png %.0f ms", raw))148    }149    write(ChartRenderer.pngData(spec, source: source, size: size, scale: 2), "perf-\(kind.rawValue)-100k.png")150}151print(failures == 0 ? "Gallery written to \(out.path)" : "\(failures) file(s) failed")152exit(failures == 0 ? 0 : 1)153
Files main
Menu

Theme