Commit 8331782f4d

8331782f4d9a2bf056d70d6da2c7a6c5292c109b

parent: 525225e4f4

Verified · cmc

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

Report log-axis tics, reversed ranges and heat map keys as not followed

Refuse SVG log axes with no positive data or a range reaching 0; label a log range with no power of ten at its ends.

Layout: unified · split

Sources/OrgCore/Compute/PlotSVG.swift +10 −5
@@ -19,8 +19,11 @@ extension TablePlot {
1919 var log: Bool
2020 var ticks: [Double] = []
2121
22 init(values: [Double], given: Settings.Range?, log: Bool, step: Double?, zero: Bool) {
22 init(values: [Double], given: Settings.Range?, log: Bool, step: Double?, zero: Bool) throws {
2323 var data = values.filter { !log || $0 > 0 }
24 // gnuplot refuses these too.
25 if log, data.isEmpty, given?.low == nil || given?.high == nil { throw Failure(description: "A log axis needs values above 0") }
26 if log, [given?.low, given?.high].contains(where: { ($0 ?? 1) <= 0 }) { throw Failure(description: "A log axis's range must be above 0") }
2427 if zero, !log { data.append(0) }
2528 var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1
2629 if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) }
@@ -29,7 +32,9 @@ extension TablePlot {
2932 if log {
3033 if given?.low == nil { low = pow(10, log10(low).rounded(.down)) }
3134 if given?.high == nil { high = pow(10, log10(high).rounded(.up)) }
32 ticks = Array(Int(log10(low).rounded(.up))...Int(log10(high).rounded(.down))).map { pow(10, Double($0)) }
35 let first = log10(low).rounded(.up), last = log10(high).rounded(.down)
36 // A range with no power of ten in it is labelled at its ends.
37 ticks = first <= last ? Array(Int(first)...Int(last)).map { pow(10, Double($0)) } : [low, high]
3338 } else {
3439 let step = step ?? TablePlot.niceStep(high - low)
3540 if given?.low == nil { low = (low / step).rounded(.down) * step }
@@ -76,13 +81,13 @@ extension TablePlot {
7681 return r.low == nil && r.high == nil ? nil : r
7782 }
7883 let ys = series.flatMap { $0.points.map(\.y) }
79 let y = Axis(values: ys, given: given(s.yrange, "ymin", "ymax"), log: s.logY, step: s.ytics, zero: bars || with == "impulses")
84 let y = try Axis(values: ys, given: given(s.yrange, "ymin", "ymax"), log: s.logY, step: s.ytics, zero: bars || with == "impulses")
8085 var x: Axis
8186 if !categories.isEmpty {
82 x = Axis(values: [], given: .init(low: -0.5, high: Double(categories.count) - 0.5), log: false, step: 1, zero: false)
87 x = try Axis(values: [], given: .init(low: -0.5, high: Double(categories.count) - 0.5), log: false, step: 1, zero: false)
8388 x.ticks = categories.indices.map(Double.init)
8489 } else {
85 x = Axis(values: series.flatMap { $0.points.map { xValue($0.x) } }, given: given(s.xrange, "xmin", "xmax"), log: s.logX, step: s.xtics, zero: false)
90 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)
8691 }
8792
8893 let width = 640.0, height = 480.0
Sources/OrgCore/Compute/PlotSettings.swift +22 −8
@@ -36,8 +36,9 @@ extension TablePlot {
3636 let numericX = plot.options.ind <= 0 || !(plot.timeInd || plot.textInd || plot.options.with == "histograms")
3737 return numericX ? ["title", "xlabel", "ylabel", "xrange", "yrange", "logscale", "grid", "key", "xtics", "ytics"]
3838 : ["title", "xlabel", "ylabel", "yrange", "logscale y", "grid", "key", "ytics"]
39 case "grid": return ["title", "xlabel", "ylabel", "key"]
40 case "3d" where plot.options["map"]?.truthy == true: return ["title", "xlabel", "ylabel", "key"]
39 // The heat map's legend is its colour scale, which gnuplot's `set key` leaves alone.
40 case "grid": return ["title", "xlabel", "ylabel"]
41 case "3d" where plot.options["map"]?.truthy == true: return ["title", "xlabel", "ylabel"]
4142 // The radar script sets its own key, after these.
4243 default: return ["title"]
4344 }
@@ -48,13 +49,24 @@ extension TablePlot {
4849 public static func settings(_ plot: Plot) -> Settings {
4950 var s = Settings()
5051 let o = plot.options
51 let follows = followed(plot)
52 s.title = o.title
53 var lines: [String] = [], sets: [String] = []
54 for line in o.lines {
55 if !(line.hasPrefix("set ") && apply(String(line.dropFirst(4)), to: &s, follows: follows)) { lines.append("line:\"\(line)\"") }
52 var follows = followed(plot)
53 func read() -> (lines: [String], sets: [String]) {
54 s = Settings()
55 s.title = o.title
56 var lines: [String] = [], sets: [String] = []
57 for line in o.lines {
58 if !(line.hasPrefix("set ") && apply(String(line.dropFirst(4)), to: &s, follows: follows)) { lines.append("line:\"\(line)\"") }
59 }
60 for set in o.sets where !apply(set, to: &s, follows: follows) { sets.append("set:\"\(set)\"") }
61 return (lines, sets)
62 }
63 var (lines, sets) = read()
64 // On a log axis gnuplot steps by a factor, not an increment.
65 if s.logX || s.logY {
66 if s.logX { follows.remove("xtics") }
67 if s.logY { follows.remove("ytics") }
68 (lines, sets) = read()
5669 }
57 for set in o.sets where !apply(set, to: &s, follows: follows) { sets.append("set:\"\(set)\"") }
5870 // Each comes twice, as Org reads the options twice; list them once, as written.
5971 for u in lines.reversed() + sets.reversed() where !s.unfollowed.contains(u) { s.unfollowed.append(u) }
6072 if let script = o["script"] { s.unfollowed.append("script:\"\(script)\"") }
@@ -73,6 +85,8 @@ extension TablePlot {
7385 guard let m = rest.firstMatch(of: /^\[\s*([^:\]]*?)\s*:\s*([^:\]]*?)\s*\]$/) else { return nil }
7486 func bound(_ t: Substring) -> Double?? { t.isEmpty || t == "*" ? .some(nil) : Double(t).map { .some($0) } }
7587 guard let low = bound(m.1), let high = bound(m.2) else { return nil }
88 // gnuplot reverses the axis for these, which the charts here don't.
89 if let low, let high, low > high { return nil }
7690 return Settings.Range(low: low, high: high)
7791 }
7892 func step() -> Double? { Double(rest).flatMap { $0 > 0 ? $0 : nil } }
Tests/OrgAppTests/PlotChartTests.swift +22 −2
@@ -43,17 +43,18 @@ struct PlotChartTests {
4343 static let table = "| x | a | b |\n|---+---+---|\n| 1 | 2 | 30 |\n| 2 | 4 | 10 |\n| 3 | 3 | 50 |\n"
4444
4545 @Test func followsTheSetSubset() throws {
46 let sets = ["title 'Set'", "xlabel 'Day'", "ylabel 'Count'", "xrange [0:10]", "yrange [*:60]", "logscale y",
46 let sets = ["title 'Set'", "xlabel 'Day'", "ylabel 'Count'", "xrange [0:10]", "yrange [*:60]",
4747 "grid", "key left top", "xtics 2", "ytics 0.5"]
4848 let plot = try Self.plot("#+PLOT: title:\"Given\" ind:1 " + sets.map { "set:\"\($0)\"" }.joined(separator: " ") + "\n" + Self.table)
4949 let s = TablePlot.settings(plot)
5050 #expect(s.title == "Set" && s.xlabel == "Day" && s.ylabel == "Count")
5151 #expect(s.xrange?.low == 0 && s.xrange?.high == 10 && s.yrange?.low == nil && s.yrange?.high == 60)
52 #expect(s.logY && !s.logX && s.grid && s.xtics == 2 && s.ytics == 0.5)
52 #expect(!s.logY && !s.logX && s.grid && s.xtics == 2 && s.ytics == 0.5)
5353 #expect(s.key == .at(vertical: "top", horizontal: "left"))
5454 #expect(s.unfollowed.isEmpty && PlotOutput.note(plot) == nil)
5555
5656 #expect(TablePlot.settings(try Self.plot("#+PLOT: set:\"logscale\"\n" + Self.table)).logX)
57 #expect(TablePlot.settings(try Self.plot("#+PLOT: set:\"logscale y\"\n" + Self.table)).logY)
5758 #expect(TablePlot.settings(try Self.plot("#+PLOT: set:\"key off\"\n" + Self.table)).key == .off)
5859 #expect(TablePlot.settings(try Self.plot("#+PLOT: line:\"set key bottom\"\n" + Self.table)).key == .at(vertical: "bottom", horizontal: nil))
5960 // gnuplot reads title:, then line:, then set:, so a set: wins.
@@ -69,6 +70,19 @@ struct PlotChartTests {
6970 #expect(TablePlot.settings(text).unfollowed == ["set:\"xtics 2\""])
7071 let grid = try Self.plot("#+PLOT: type:grid set:\"logscale\" set:\"xlabel 'C'\"\n| 1 | 2 |\n| 3 | 4 |\n")
7172 #expect(TablePlot.settings(grid).unfollowed == ["set:\"logscale\""] && TablePlot.settings(grid).xlabel == "C")
73 // gnuplot's key leaves a heat map's colour scale alone.
74 let heat = try Self.plot("#+PLOT: type:grid set:\"key off\"\n| 1 | 2 |\n| 3 | 4 |\n")
75 #expect(TablePlot.settings(heat).unfollowed == ["set:\"key off\""] && TablePlot.settings(heat).key == nil)
76 let map = try Self.plot("#+PLOT: type:3d map:t set:\"key left\"\n| 1 | 2 |\n| 3 | 4 |\n")
77 #expect(TablePlot.settings(map).unfollowed == ["set:\"key left\""])
78 // On a log axis gnuplot's step is a factor, whichever comes first.
79 let logTics = try Self.plot("#+PLOT: ind:1 set:\"ytics 2\" set:\"logscale y\" set:\"xtics 1\"\n" + Self.table)
80 #expect(TablePlot.settings(logTics).unfollowed == ["set:\"ytics 2\""] && TablePlot.settings(logTics).ytics == nil && TablePlot.settings(logTics).xtics == 1)
81 let logX = try Self.plot("#+PLOT: ind:1 line:\"set xtics 2\" set:\"logscale x\"\n" + Self.table)
82 #expect(TablePlot.settings(logX).unfollowed == ["line:\"set xtics 2\""])
83 // gnuplot reverses an axis whose range runs high to low.
84 let reversed = try Self.plot("#+PLOT: ind:1 set:\"xrange [10:0]\" set:\"yrange [5:0]\" set:\"yrange [0:5]\"\n" + Self.table)
85 #expect(TablePlot.settings(reversed).unfollowed == ["set:\"xrange [10:0]\"", "set:\"yrange [5:0]\""] && TablePlot.settings(reversed).xrange == nil)
7286 let radar = try Self.plot("#+PLOT: type:radar ticks:2 set:\"key off\" set:\"title 'R'\"\n| n | a |\n|---+---|\n| x | 1 |\n| y | 2 |\n| z | 3 |\n")
7387 #expect(TablePlot.settings(radar).unfollowed == ["set:\"key off\""] && TablePlot.settings(radar).title == "R")
7488 }
@@ -102,6 +116,12 @@ struct PlotChartTests {
102116 #expect(bars.contains(">a</text>") && !bars.contains(">v</text>") && !bars.contains("stroke-dasharray"))
103117 let log = try TablePlot.svg(Self.plot("#+PLOT: ind:1 with:points set:\"logscale y\"\n| x | v |\n|---+---|\n| 1 | 5 |\n| 2 | 500 |\n| 3 | 0 |\n"))
104118 #expect(log.contains(">1</text>") && log.contains(">1000</text>") && log.components(separatedBy: "<circle").count - 1 == 2)
119 // A log range with no power of ten in it is labelled at its ends.
120 let narrow = try TablePlot.svg(Self.plot("#+PLOT: ind:1 set:\"logscale y\" set:\"yrange [2:5]\"\n| 1 | 3 |\n| 2 | 4 |\n"))
121 #expect(narrow.contains(">2</text>") && narrow.contains(">5</text>"))
122 #expect(throws: TablePlot.Failure.self) { try TablePlot.svg(Self.plot("#+PLOT: ind:1 set:\"logscale y\"\n| 1 | 0 |\n| 2 | -4 |\n")) }
123 #expect(throws: TablePlot.Failure.self) { try TablePlot.svg(Self.plot("#+PLOT: ind:1 set:\"logscale y\" set:\"yrange [0:100]\"\n| 1 | 3 |\n| 2 | 4 |\n")) }
124 #expect(throws: TablePlot.Failure.self) { try TablePlot.svg(Self.plot("#+PLOT: ind:1 set:\"logscale x\" set:\"xrange [*:10]\"\n| 0 | 3 |\n| -1 | 4 |\n")) }
105125 let ranged = try TablePlot.svg(Self.plot("#+PLOT: ind:1 set:\"xrange [0:10]\" set:\"ytics 25\"\n" + Self.table))
106126 #expect(ranged.contains(">10</text>") && ranged.contains(">25</text>") && ranged.contains(">50</text>"))
107127 #expect(throws: TablePlot.Failure.self) { try TablePlot.svg(Self.plot("#+PLOT: type:3d\n| 1 | 2 |\n| 3 | 4 |\n")) }
docs/manual/guide/10-tables.org +17 −15
@@ -792,10 +792,11 @@ options before the last =line:= are dropped, and each =set:= and =line:= that's
792792twice, as Org reads the options twice. An ind column of timestamps is plotted as dates, and one of text as labels.
793793
794794A =radar= plot draws each row as an axis, clockwise from the top, and each column after
795the first as a series named by the header. Each axis is scaled from its row's lowest to
796highest value, or from =ymin= to =ymax=. Org works out the number of rings from the values;
797where Org can't (it signals an error, or never finishes, as for a row whose values are all
798the same), orgstar reports it and asks for =ticks:=.
795the first as a series named by the header. Each axis is scaled from its row's lowest and
796highest values rounded outward (120 to 340 becomes 100 to 400), or from =ymin= to =ymax=. Org
797works out the number of rings from the values; where Org can't (it signals an error, as for a
798=ymin= above a row's values, or never finishes, as for a row whose values are all the same),
799orgstar reports it and asks for =ticks:=. =ticks:= takes 0 to 1000.
799800
800801On the Mac, when gnuplot is installed (orgstar looks on the same =PATH= as code blocks), the
801802table goes to gnuplot as Emacs sends it: the same data file and script, so every option and
@@ -803,23 +804,24 @@ gnuplot's own terminals and styles apply. A plot without =file:= is drawn to an
803804in a sheet. Without gnuplot, and on iOS, orgstar draws the plot itself with Swift Charts: =2d=
804805as lines, points or bars by =with:=, =grid= as a heat map, =3d= as a surface (a wireframe in a
805806file), =radar= as a web. =file:= then takes =.png=, =.jpg= or =.pdf=, 640 by 480 points as
806gnuplot's default, or =.svg= for a =2d= plot. If inline images are showing, a link to the file
807gnuplot's default, or =.svg= for a =2d= plot. A log axis needs values above 0, and its
808range, if given, must be above 0. If inline images are showing, a link to the file
807809shows the new plot.
808810
809811Without gnuplot these =set:= commands are followed, also as a =line:= that starts with =set=:
810812
811| Command | Effect |
812|------------------------------------------+------------------------------------------------------------|
813| =title 'T'= | The title, over =title:= |
814| =xlabel 'X'=, =ylabel 'Y'= | The axes' names |
815| =xrange [0:10]=, =yrange [*:5]= | The axes' ranges; =*= or nothing leaves an end to the data |
816| =logscale=, =logscale x=, =logscale y= | Logarithmic axes |
817| =grid= | Grid lines at the marks |
818| =key off=, =key top left= | Hides or places the legend (top, bottom, left, right) |
819| =xtics 2=, =ytics 0.5= | A mark every so many units |
813| Command | Effect |
814|----------------------------------------+-------------------------------------------------------------------------|
815| =title 'T'= | The title, over =title:= |
816| =xlabel 'X'=, =ylabel 'Y'= | The axes' names |
817| =xrange [0:10]=, =yrange [*:5]= | The axes' ranges, low to high; =*= or nothing leaves an end to the data |
818| =logscale=, =logscale x=, =logscale y= | Logarithmic axes |
819| =grid= | Grid lines at the marks |
820| =key off=, =key top left= | Hides or places the legend (top, bottom, left, right) |
821| =xtics 2=, =ytics 0.5= | A mark every so many units, on an axis that isn't logarithmic |
820822
821823=2d= plots follow all of them, except that an x axis of text or dates takes no range, steps or
822log scale. =grid= and =3d map:t= plots follow the title, axis names and key; other =3d= plots
824log scale. =grid= and =3d map:t= plots follow the title and axis names; other =3d= plots
823825and =radar= plots only the title. Other commands, other =line:= options and =script:= are left
824826out, and the message after plotting names them, such as =Not followed without gnuplot:
825827set:"style fill solid"=.