Commit 525225e4f4

525225e4f44b53a1ea7559b03462473575dfa523

parent: c27e8a552f

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 09:42 UTC

Refuse radar plots Org can't tick, and ticks: outside 0 to 1000

Print bignum fields as written. Check radar plots with script: before drawing them without gnuplot.

Layout: unified · split

Sources/OrgApp/PlotChart.swift +8
@@ -360,8 +360,16 @@ public enum PlotOutput {
360360 return unfollowed.isEmpty ? nil : "Not followed without gnuplot: " + unfollowed.joined(separator: ", ")
361361 }
362362
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
363370 /// The chart as gnuplot's default 640×480, as PNG, JPEG, PDF or SVG by the file's extension.
364371 public static func write(_ plot: TablePlot.Plot, to url: URL) throws {
372 try check(plot)
365373 if url.pathExtension.lowercased() == "svg" {
366374 let svg: String
367375 do { svg = try TablePlot.svg(plot) } catch { throw Failure(description: "\(error)") }
Sources/OrgCore/Compute/TablePlotRadar.swift +17 −4
@@ -32,7 +32,15 @@ extension TablePlot {
3232 }
3333
3434 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
3644 }
3745
3846 /// `org--plot/values-stats`.
@@ -51,7 +59,10 @@ extension TablePlot {
5159 var decompositions: [[(Int, Double)]] = []
5260 for row in rows {
5361 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)
5566 if value % 10 == 0 { value /= 10 }
5667 // `org--plot/prime-factors` loops forever on 0.
5768 guard value > 0 else { return nil }
@@ -116,6 +127,8 @@ extension TablePlot {
116127 let ymin = try number("ymin"), ymax = try number("ymax")
117128 let ticks: Int
118129 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") }
119132 ticks = Int(given)
120133 } else if let sensible = sensibleTicks(rows, hardMin: ymin, hardMax: ymax) {
121134 ticks = sensible
@@ -127,8 +140,8 @@ extension TablePlot {
127140 let s = stats(nums)
128141 return Radar.Axis(label: row[0], fields: Array(row.dropFirst()), values: nums.map(\.value),
129142 low: ymin ?? s.niceMin.value, high: ymax ?? s.niceMax.value,
130 lowText: plot.options["ymin"]?.description ?? printed(s.niceMin),
131 highText: plot.options["ymax"]?.description ?? printed(s.niceMax))
143 lowText: plot.options["ymin"]?.description ?? (s.niceMin.integer ? integerText(row[1]) : printed(s.niceMin)),
144 highText: plot.options["ymax"]?.description ?? (s.niceMax.integer ? integerText(row[1]) : printed(s.niceMax)))
132145 }
133146 return Radar(axes: axes, series: Array((plot.options.labels ?? []).dropFirst()), ticks: ticks, sharedTicks: ymin != nil && ymax != nil)
134147 }
Sources/Orgstar/ContentView.swift +1
@@ -975,6 +975,7 @@ struct ContentView: View {
975975 commands.show("\(error)")
976976 }
977977 } else {
978 do { try PlotOutput.check(plot) } catch { return commands.show("\(error)") }
978979 panel = .plot(UUID(), plot)
979980 if let note = PlotOutput.note(plot) { commands.show(note) }
980981 }
Sources/OrgstarMobile/EditorScreen.swift +1
@@ -617,6 +617,7 @@ extension EditorScreen {
617617 message = "Plot written to \(url.lastPathComponent)" + (PlotOutput.note(plot).map { ". " + $0 } ?? "")
618618 if editor.showsImages { editor.restyleAll() }
619619 } else {
620 try PlotOutput.check(plot)
620621 sheet = .plot(plot)
621622 if let note = PlotOutput.note(plot) { message = note }
622623 }
Tests/OrgAppTests/PlotChartTests.swift +6
@@ -83,6 +83,12 @@ struct PlotChartTests {
8383 #expect(size > 1000, "\(name) is \(size) bytes")
8484 }
8585 #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)
8692 }
8793
8894 @Test func writesSVG() throws {
Tests/OrgCoreTests/TablePlotTests.swift +16
@@ -180,6 +180,14 @@ struct TablePlotTests {
180180 let ours = TablePlot.sensibleTicks(t, hardMin: nil, hardMax: nil).map(String.init) ?? "error"
181181 #expect(ours == want, "\(t)")
182182 }
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 }
183191 }
184192
185193 @Test func seriesAndMatrices() throws {
@@ -201,6 +209,14 @@ struct TablePlotTests {
201209 // Emacs signals an error where the ranges leave nothing to pick ticks from.
202210 #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) }
203211 _ = 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)")
204220 let radar = try TablePlot.radar(TablePlot.plot(Self.cases[9], at: 40))
205221 #expect(radar.series == ["a", "b"] && radar.axes.map(\.label) == ["x", "y", "z"] && radar.sharedTicks)
206222 #expect(radar.axes[2].values == [8, 3] && radar.axes[0].low == 0 && radar.axes[0].high == 8)