Commit 525225e4f4
Verified · cmc
Layout: unified · split
Sources/OrgApp/PlotChart.swift +8
| @@ -360,8 +360,16 @@ public enum PlotOutput { | |||
| 360 | return unfollowed.isEmpty ? nil : "Not followed without gnuplot: " + unfollowed.joined(separator: ", ") | 360 | return unfollowed.isEmpty ? nil : "Not followed without gnuplot: " + unfollowed.joined(separator: ", ") |
| 361 | } | 361 | } |
| 362 | 362 | ||
| 363 | /// Refuses what the chart can't draw: a radar plot `TablePlot.radar` refuses, which | ||
| 364 | /// `TablePlot.plot` leaves to gnuplot when there's a `script:`. | ||
| 365 | public static func check(_ plot: TablePlot.Plot) throws { | ||
| 366 | guard plot.options.type == "radar" else { return } | ||
| 367 | do { _ = try TablePlot.radar(plot) } catch { throw Failure(description: "\(error)") } | ||
| 368 | } | ||
| 369 | |||
| 363 | /// The chart as gnuplot's default 640×480, as PNG, JPEG, PDF or SVG by the file's extension. | 370 | /// The chart as gnuplot's default 640×480, as PNG, JPEG, PDF or SVG by the file's extension. |
| 364 | public static func write(_ plot: TablePlot.Plot, to url: URL) throws { | 371 | public static func write(_ plot: TablePlot.Plot, to url: URL) throws { |
| 372 | try check(plot) | ||
| 365 | if url.pathExtension.lowercased() == "svg" { | 373 | if url.pathExtension.lowercased() == "svg" { |
| 366 | let svg: String | 374 | let svg: String |
| 367 | do { svg = try TablePlot.svg(plot) } catch { throw Failure(description: "\(error)") } | 375 | do { svg = try TablePlot.svg(plot) } catch { throw Failure(description: "\(error)") } |
Sources/OrgCore/Compute/TablePlotRadar.swift +17 −4
| @@ -32,7 +32,15 @@ extension TablePlot { | |||
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | static func printed(_ n: (value: Double, integer: Bool)) -> String { | 34 | static func printed(_ n: (value: Double, integer: Bool)) -> String { |
| 35 | n.integer ? String(Int(n.value)) : Elisp.floatText(n.value) | 35 | n.integer && abs(n.value) < 0x1p63 ? String(Int(n.value)) : Elisp.floatText(n.value) |
| 36 | } | ||
| 37 | |||
| 38 | /// An integer field as Emacs prints what `string-to-number` reads from it, bignums included. | ||
| 39 | static func integerText(_ field: String) -> String { | ||
| 40 | let trimmed = field.drop { $0 == " " || $0 == "\t" } | ||
| 41 | guard let m = trimmed.firstMatch(of: /^([-+]?)0*([0-9]+)/) else { return "0" } | ||
| 42 | let digits = String(m.2) | ||
| 43 | return (m.1 == "-" && digits != "0" ? "-" : "") + digits | ||
| 36 | } | 44 | } |
| 37 | 45 | ||
| 38 | /// `org--plot/values-stats`. | 46 | /// `org--plot/values-stats`. |
| @@ -51,7 +59,10 @@ extension TablePlot { | |||
| 51 | var decompositions: [[(Int, Double)]] = [] | 59 | var decompositions: [[(Int, Double)]] = [] |
| 52 | for row in rows { | 60 | for row in rows { |
| 53 | let s = stats(row.dropFirst().map(lispNumber), hardMin: hardMin, hardMax: hardMax) | 61 | let s = stats(row.dropFirst().map(lispNumber), hardMin: hardMin, hardMax: hardMax) |
| 54 | var value = Int((s.factor * (s.niceMax.value - s.niceMin.value)).rounded(.toNearestOrEven)) | 62 | let raw = (s.factor * (s.niceMax.value - s.niceMin.value)).rounded(.toNearestOrEven) |
| 63 | // A ymin above a row's values or a ymax below them: `ceiling` of NaN signals overflow-error. | ||
| 64 | guard raw.isFinite, raw >= 0, raw < 0x1p63 else { return nil } | ||
| 65 | var value = Int(raw) | ||
| 55 | if value % 10 == 0 { value /= 10 } | 66 | if value % 10 == 0 { value /= 10 } |
| 56 | // `org--plot/prime-factors` loops forever on 0. | 67 | // `org--plot/prime-factors` loops forever on 0. |
| 57 | guard value > 0 else { return nil } | 68 | guard value > 0 else { return nil } |
| @@ -116,6 +127,8 @@ extension TablePlot { | |||
| 116 | let ymin = try number("ymin"), ymax = try number("ymax") | 127 | let ymin = try number("ymin"), ymax = try number("ymax") |
| 117 | let ticks: Int | 128 | let ticks: Int |
| 118 | if let given = try number("ticks") { | 129 | if let given = try number("ticks") { |
| 130 | // gnuplot can't draw fewer than none, and the web has a ring for each. | ||
| 131 | guard given >= 0, given <= 1000 else { throw Failure(description: "ticks: must be from 0 to 1000") } | ||
| 119 | ticks = Int(given) | 132 | ticks = Int(given) |
| 120 | } else if let sensible = sensibleTicks(rows, hardMin: ymin, hardMax: ymax) { | 133 | } else if let sensible = sensibleTicks(rows, hardMin: ymin, hardMax: ymax) { |
| 121 | ticks = sensible | 134 | ticks = sensible |
| @@ -127,8 +140,8 @@ extension TablePlot { | |||
| 127 | let s = stats(nums) | 140 | let s = stats(nums) |
| 128 | return Radar.Axis(label: row[0], fields: Array(row.dropFirst()), values: nums.map(\.value), | 141 | return Radar.Axis(label: row[0], fields: Array(row.dropFirst()), values: nums.map(\.value), |
| 129 | low: ymin ?? s.niceMin.value, high: ymax ?? s.niceMax.value, | 142 | low: ymin ?? s.niceMin.value, high: ymax ?? s.niceMax.value, |
| 130 | lowText: plot.options["ymin"]?.description ?? printed(s.niceMin), | 143 | lowText: plot.options["ymin"]?.description ?? (s.niceMin.integer ? integerText(row[1]) : printed(s.niceMin)), |
| 131 | highText: plot.options["ymax"]?.description ?? printed(s.niceMax)) | 144 | highText: plot.options["ymax"]?.description ?? (s.niceMax.integer ? integerText(row[1]) : printed(s.niceMax))) |
| 132 | } | 145 | } |
| 133 | return Radar(axes: axes, series: Array((plot.options.labels ?? []).dropFirst()), ticks: ticks, sharedTicks: ymin != nil && ymax != nil) | 146 | return Radar(axes: axes, series: Array((plot.options.labels ?? []).dropFirst()), ticks: ticks, sharedTicks: ymin != nil && ymax != nil) |
| 134 | } | 147 | } |
Sources/Orgstar/ContentView.swift +1
| @@ -975,6 +975,7 @@ struct ContentView: View { | |||
| 975 | commands.show("\(error)") | 975 | commands.show("\(error)") |
| 976 | } | 976 | } |
| 977 | } else { | 977 | } else { |
| 978 | do { try PlotOutput.check(plot) } catch { return commands.show("\(error)") } | ||
| 978 | panel = .plot(UUID(), plot) | 979 | panel = .plot(UUID(), plot) |
| 979 | if let note = PlotOutput.note(plot) { commands.show(note) } | 980 | if let note = PlotOutput.note(plot) { commands.show(note) } |
| 980 | } | 981 | } |
Sources/OrgstarMobile/EditorScreen.swift +1
| @@ -617,6 +617,7 @@ extension EditorScreen { | |||
| 617 | message = "Plot written to \(url.lastPathComponent)" + (PlotOutput.note(plot).map { ". " + $0 } ?? "") | 617 | message = "Plot written to \(url.lastPathComponent)" + (PlotOutput.note(plot).map { ". " + $0 } ?? "") |
| 618 | if editor.showsImages { editor.restyleAll() } | 618 | if editor.showsImages { editor.restyleAll() } |
| 619 | } else { | 619 | } else { |
| 620 | try PlotOutput.check(plot) | ||
| 620 | sheet = .plot(plot) | 621 | sheet = .plot(plot) |
| 621 | if let note = PlotOutput.note(plot) { message = note } | 622 | if let note = PlotOutput.note(plot) { message = note } |
| 622 | } | 623 | } |
Tests/OrgAppTests/PlotChartTests.swift +6
| @@ -83,6 +83,12 @@ struct PlotChartTests { | |||
| 83 | #expect(size > 1000, "\(name) is \(size) bytes") | 83 | #expect(size > 1000, "\(name) is \(size) bytes") |
| 84 | } | 84 | } |
| 85 | #expect(throws: PlotOutput.Failure.self) { try PlotOutput.write(plot, to: folder.appendingPathComponent("radar.svg")) } | 85 | #expect(throws: PlotOutput.Failure.self) { try PlotOutput.write(plot, to: folder.appendingPathComponent("radar.svg")) } |
| 86 | // With script:, TablePlot.plot leaves the radar to gnuplot; drawn here, it's refused. | ||
| 87 | let scripted = try Self.plot("#+PLOT: type:radar script:\"r.gp\"\n| n | a | b |\n|---+---+---|\n| x | 1 | 1 |\n| y | 2 | 3 |\n") | ||
| 88 | #expect(throws: PlotOutput.Failure.self) { try PlotOutput.check(scripted) } | ||
| 89 | #expect(throws: PlotOutput.Failure.self) { try PlotOutput.write(scripted, to: folder.appendingPathComponent("scripted.png")) } | ||
| 90 | #expect(!FileManager.default.fileExists(atPath: folder.appendingPathComponent("scripted.png").path)) | ||
| 91 | try PlotOutput.check(plot) | ||
| 86 | } | 92 | } |
| 87 | 93 | ||
| 88 | @Test func writesSVG() throws { | 94 | @Test func writesSVG() throws { |
Tests/OrgCoreTests/TablePlotTests.swift +16
| @@ -180,6 +180,14 @@ struct TablePlotTests { | |||
| 180 | let ours = TablePlot.sensibleTicks(t, hardMin: nil, hardMax: nil).map(String.init) ?? "error" | 180 | let ours = TablePlot.sensibleTicks(t, hardMin: nil, hardMax: nil).map(String.init) ?? "error" |
| 181 | #expect(ours == want, "\(t)") | 181 | #expect(ours == want, "\(t)") |
| 182 | } | 182 | } |
| 183 | let rows = [["a", "1", "5"], ["b", "3", "4"]] | ||
| 184 | let bounds: [(Double?, Double?, String)] = [(100, nil, "100 nil"), (nil, 0, "nil 0"), (0, 10, "0 10"), (-5, nil, "-5 nil")] | ||
| 185 | let hard = try EmacsOracle.evaluate("", "(progn (require 'org-plot) (list " + bounds.map { | ||
| 186 | "(condition-case nil (format \"%S\" (org--plot/sensible-tick-num '((\"a\" \"1\" \"5\") (\"b\" \"3\" \"4\")) \($0.2))) (error \"error\"))" | ||
| 187 | }.joined(separator: " ") + "))") | ||
| 188 | for (b, want) in zip(bounds, hard) { | ||
| 189 | #expect((TablePlot.sensibleTicks(rows, hardMin: b.0, hardMax: b.1).map(String.init) ?? "error") == want, "\(b.2)") | ||
| 190 | } | ||
| 183 | } | 191 | } |
| 184 | 192 | ||
| 185 | @Test func seriesAndMatrices() throws { | 193 | @Test func seriesAndMatrices() throws { |
| @@ -201,6 +209,14 @@ struct TablePlotTests { | |||
| 201 | // Emacs signals an error where the ranges leave nothing to pick ticks from. | 209 | // Emacs signals an error where the ranges leave nothing to pick ticks from. |
| 202 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymin:0 ymax:10\n| a | 1 | 9 |\n| b | 2 | 3 |\n", at: 0) } | 210 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymin:0 ymax:10\n| a | 1 | 9 |\n| b | 2 | 3 |\n", at: 0) } |
| 203 | _ = try TablePlot.plot("#+PLOT: type:radar ticks:3\n| a | 1 | 1 |\n| b | 2 | 3 |\n", at: 0) | 211 | _ = try TablePlot.plot("#+PLOT: type:radar ticks:3\n| a | 1 | 1 |\n| b | 2 | 3 |\n", at: 0) |
| 212 | // A ymin above a row's values, or a ymax below them: Emacs signals overflow-error. | ||
| 213 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymin:100\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | ||
| 214 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ymax:0\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | ||
| 215 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:-1\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | ||
| 216 | #expect(throws: TablePlot.Failure.self) { try TablePlot.plot("#+PLOT: type:radar ticks:99999999999999999999\n| x | 1 | 5 |\n| y | 2 | 3 |\n", at: 0) } | ||
| 217 | // Emacs prints a bignum as it is. | ||
| 218 | let big = try TablePlot.radarSetup(TablePlot.plot("#+PLOT: type:radar ticks:3\n| x | 100000000000000000000 | 100000000000000000000 |\n| y | -007 | -007 |\n", at: 0)) | ||
| 219 | #expect(big.contains("\"x\" 100000000000000000000 100000000000000000000 3\n\"y\" -7 -7 3\n"), "\(big)") | ||
| 204 | let radar = try TablePlot.radar(TablePlot.plot(Self.cases[9], at: 40)) | 220 | let radar = try TablePlot.radar(TablePlot.plot(Self.cases[9], at: 40)) |
| 205 | #expect(radar.series == ["a", "b"] && radar.axes.map(\.label) == ["x", "y", "z"] && radar.sharedTicks) | 221 | #expect(radar.series == ["a", "b"] && radar.axes.map(\.label) == ["x", "y", "z"] && radar.sharedTicks) |
| 206 | #expect(radar.axes[2].values == [8, 3] && radar.axes[0].low == 0 && radar.axes[0].high == 8) | 222 | #expect(radar.axes[2].values == [8, 3] && radar.axes[0].low == 0 && radar.axes[0].high == 8) |