import Foundation /// A 2d plot as an SVG file, 640×480 as gnuplot's default: lines, points or bars by `with:`, /// with the `set` commands `settings` follows. extension TablePlot { /// gnuplot's default line colours. public static let colors = ["#9400d3", "#009e73", "#56b4e9", "#e69f00", "#f0e442", "#0072b2", "#e51e10", "#000000"] /// 1, 2 or 5 times a power of ten, near a fifth of `span`. static func niceStep(_ span: Double) -> Double { let raw = span / 5, power = pow(10, (log10(raw)).rounded(.down)), f = raw / power return power * (f < 1.5 ? 1 : f < 3.5 ? 2 : f < 7.5 ? 5 : 10) } /// What a log axis places; gnuplot skips undefined points, and Swift Charts traps past these. public static let logRange = 1e-307...1e308 /// An axis: its ends, widened to whole steps where they're not given, and its ticks. public struct Axis { public var low: Double public var high: Double var log: Bool var ticks: [Double] = [] public init(values: [Double], given: Settings.Range?, log: Bool, step: Double?, zero: Bool) throws { var data = values.filter { !log || TablePlot.logRange.contains($0) } // gnuplot refuses these too. if log, data.isEmpty, given?.low == nil || given?.high == nil { throw Failure(description: "A log axis needs values above 0") } if log, [given?.low, given?.high].contains(where: { ($0 ?? 1) <= 0 }) { throw Failure(description: "A log axis's range must be above 0") } if log, [given?.low, given?.high].contains(where: { !TablePlot.logRange.contains($0 ?? 1) }) { throw Failure(description: "A log axis's range must be within 1e-307 to 1e308") } if zero, !log { data.append(0) } var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1 if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) } if low > high { swap(&low, &high) } self.log = log if log { if given?.low == nil { low = pow(10, log10(low).rounded(.down)) } if given?.high == nil { high = pow(10, log10(high).rounded(.up)) } guard TablePlot.logRange.contains(low), TablePlot.logRange.contains(high) else { throw Failure(description: "A log axis's range must be within 1e-307 to 1e308") } let first = log10(low).rounded(.up), last = log10(high).rounded(.down) // A range with no power of ten in it is labelled at its ends. ticks = first <= last ? Array(Int(first)...Int(last)).map { pow(10, Double($0)) } : [low, high] } else { let step = step ?? TablePlot.niceStep(high - low) if given?.low == nil { low = (low / step).rounded(.down) * step } if given?.high == nil { high = (high / step).rounded(.up) * step } var t = (low / step).rounded(.up) * step while t <= high + step * 1e-9, ticks.count < 200 { ticks.append(abs(t) < step * 1e-9 ? 0 : t); t += step } } self.low = low self.high = high } func fraction(_ v: Double) -> Double { log ? (log10(v) - log10(low)) / (log10(high) - log10(low)) : (v - low) / (high - low) } } static func escaped(_ s: String) -> String { s.replacingOccurrences(of: "&", with: "&").replacingOccurrences(of: "<", with: "<") .replacingOccurrences(of: ">", with: ">").replacingOccurrences(of: "\"", with: """) } static func number(_ v: Double) -> String { String(format: "%.2f", v) } public static func svg(_ plot: Plot, calendar: Calendar = .current) throws -> String { guard plot.options.type == "2d" else { throw Failure(description: "Without gnuplot, only 2d plots are written as .svg; use .png, .jpg or .pdf") } let s = settings(plot), with = plot.options.with let series = series(plot, calendar: calendar) let bars = ["boxes", "histograms", "hist"].contains(with) var categories: [String] = [] for p in series.flatMap(\.points) { if case .label(let l) = p.x, !categories.contains(l) { categories.append(l) } } let dates = series.contains { $0.points.contains { if case .date = $0.x { true } else { false } } } func xValue(_ x: Series.X) -> Double { switch x { case .number(let n): n case .date(let d): d.timeIntervalSince1970 case .label(let l): Double(categories.firstIndex(of: l)!) } } func bound(_ key: String) -> Double? { if case .number(let n)? = plot.options[key] { n } else { nil } } func given(_ set: Settings.Range?, _ low: String, _ high: String) -> Settings.Range? { let r = Settings.Range(low: set?.low ?? bound(low), high: set?.high ?? bound(high)) return r.low == nil && r.high == nil ? nil : r } let ys = series.flatMap { $0.points.map(\.y) } let y = try Axis(values: ys, given: given(s.yrange, "ymin", "ymax"), log: s.logY, step: s.ytics, zero: bars || with == "impulses") var x: Axis if !categories.isEmpty { x = try Axis(values: [], given: .init(low: -0.5, high: Double(categories.count) - 0.5), log: false, step: 1, zero: false) x.ticks = categories.indices.map(Double.init) } else { x = try Axis(values: series.flatMap { $0.points.map { xValue($0.x) } }, given: given(s.xrange, "xmin", "xmax"), log: s.logX, step: s.xtics, zero: false) } let width = 640.0, height = 480.0 let left = 60.0 + (s.ylabel == nil ? 0 : 20), right = 620.0 let top = 20.0 + (s.title == nil ? 0 : 20), bottom = height - 40 - (s.xlabel == nil ? 0 : 20) func px(_ v: Double) -> Double { left + x.fraction(v) * (right - left) } func py(_ v: Double) -> Double { bottom - y.fraction(v) * (bottom - top) } func xLabel(_ t: Double) -> String { if !categories.isEmpty { return categories[Int(t)] } if dates { let c = calendar.dateComponents([.year, .month, .day], from: Date(timeIntervalSince1970: t)) return String(format: "%04d-%02d-%02d", c.year ?? 0, c.month ?? 0, c.day ?? 0) } return String(format: "%g", t) } var out = """ """ func add(_ line: String) { out += line + "\n" } for t in x.ticks where t >= x.low && t <= x.high { if s.grid { add("") } add("") add("\(escaped(xLabel(t)))") } for t in y.ticks where t >= y.low && t <= y.high { if s.grid { add("") } add("") add("\(escaped(String(format: "%g", t)))") } add("") if let title = s.title { add("\(escaped(title))") } if let label = s.xlabel { add("\(escaped(label))") } if let label = s.ylabel { add("\(escaped(label))") } add("") let xs = series.flatMap { $0.points.map { xValue($0.x) } }.sorted() let spacing = categories.isEmpty ? zip(xs, xs.dropFirst()).map { $1 - $0 }.filter { $0 > 0 }.min().map { abs(px(xs[0] + $0) - px(xs[0])) } ?? 40 : (px(1) - px(0)) let base = py(y.log ? y.low : min(max(0, y.low), y.high)) for (i, s) in series.enumerated() { let color = colors[i % colors.count] let points = s.points.filter { (!y.log || logRange.contains($0.y)) && (!x.log || logRange.contains(xValue($0.x))) }.map { (px(xValue($0.x)), py($0.y)) } guard !points.isEmpty else { continue } func polyline(_ p: [(Double, Double)]) { add("") } switch with { case "points", "dots", "linespoints": if with == "linespoints" { polyline(points) } for (px, py) in points { add("") } case "impulses": for (px, py) in points { add("") } case "boxes", "histograms", "hist": let group = spacing * 0.8, bar = group / Double(series.count) for (px, py) in points { let x0 = px - group / 2 + bar * Double(i) add("") } case "steps", "fsteps": var stepped = [points[0]] for p in points.dropFirst() { stepped.append(with == "steps" ? (p.0, stepped.last!.1) : (stepped.last!.0, p.1)) stepped.append(p) } polyline(stepped) default: polyline(points) } } add("") var showKey = true, vertical = "top", horizontal = "right" switch s.key { case .off: showKey = false case .at(let v, let h): vertical = v ?? vertical; horizontal = h ?? horizontal case nil: break } if showKey { for (i, s) in series.enumerated() { let color = colors[i % colors.count] let row = vertical == "top" ? top + 16 + Double(i) * 16 : bottom - 10 - Double(series.count - 1 - i) * 16 let sample = horizontal == "right" ? right - 50 : left + 10 let text = horizontal == "right" ? "" : "" add(text + escaped(s.label) + "") add("") } } return out + "\n" } }