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 {
19 var log: Bool 19 var log: Bool
20 var ticks: [Double] = [] 20 var ticks: [Double] = []
21 21
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 {
23 var data = values.filter { !log || $0 > 0 } 23 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") }
24 if zero, !log { data.append(0) } 27 if zero, !log { data.append(0) }
25 var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1 28 var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1
26 if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) } 29 if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) }
@@ -29,7 +32,9 @@ extension TablePlot {
29 if log { 32 if log {
30 if given?.low == nil { low = pow(10, log10(low).rounded(.down)) } 33 if given?.low == nil { low = pow(10, log10(low).rounded(.down)) }
31 if given?.high == nil { high = pow(10, log10(high).rounded(.up)) } 34 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]
33 } else { 38 } else {
34 let step = step ?? TablePlot.niceStep(high - low) 39 let step = step ?? TablePlot.niceStep(high - low)
35 if given?.low == nil { low = (low / step).rounded(.down) * step } 40 if given?.low == nil { low = (low / step).rounded(.down) * step }
@@ -76,13 +81,13 @@ extension TablePlot {
76 return r.low == nil && r.high == nil ? nil : r 81 return r.low == nil && r.high == nil ? nil : r
77 } 82 }
78 let ys = series.flatMap { $0.points.map(\.y) } 83 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")
80 var x: Axis 85 var x: Axis
81 if !categories.isEmpty { 86 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)
83 x.ticks = categories.indices.map(Double.init) 88 x.ticks = categories.indices.map(Double.init)
84 } else { 89 } 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)
86 } 91 }
87 92
88 let width = 640.0, height = 480.0 93 let width = 640.0, height = 480.0
Sources/OrgCore/Compute/PlotSettings.swift +22 −8
@@ -36,8 +36,9 @@ extension TablePlot {
36 let numericX = plot.options.ind <= 0 || !(plot.timeInd || plot.textInd || plot.options.with == "histograms") 36 let numericX = plot.options.ind <= 0 || !(plot.timeInd || plot.textInd || plot.options.with == "histograms")
37 return numericX ? ["title", "xlabel", "ylabel", "xrange", "yrange", "logscale", "grid", "key", "xtics", "ytics"] 37 return numericX ? ["title", "xlabel", "ylabel", "xrange", "yrange", "logscale", "grid", "key", "xtics", "ytics"]
38 : ["title", "xlabel", "ylabel", "yrange", "logscale y", "grid", "key", "ytics"] 38 : ["title", "xlabel", "ylabel", "yrange", "logscale y", "grid", "key", "ytics"]
39 case "grid": return ["title", "xlabel", "ylabel", "key"] 39 // The heat map's legend is its colour scale, which gnuplot's `set key` leaves alone.
40 case "3d" where plot.options["map"]?.truthy == true: return ["title", "xlabel", "ylabel", "key"] 40 case "grid": return ["title", "xlabel", "ylabel"]
41 case "3d" where plot.options["map"]?.truthy == true: return ["title", "xlabel", "ylabel"]
41 // The radar script sets its own key, after these. 42 // The radar script sets its own key, after these.
42 default: return ["title"] 43 default: return ["title"]
43 } 44 }
@@ -48,13 +49,24 @@ extension TablePlot {
48 public static func settings(_ plot: Plot) -> Settings { 49 public static func settings(_ plot: Plot) -> Settings {
49 var s = Settings() 50 var s = Settings()
50 let o = plot.options 51 let o = plot.options
51 let follows = followed(plot) 52 var follows = followed(plot)
52 s.title = o.title 53 func read() -> (lines: [String], sets: [String]) {
53 var lines: [String] = [], sets: [String] = [] 54 s = Settings()
54 for line in o.lines { 55 s.title = o.title
55 if !(line.hasPrefix("set ") && apply(String(line.dropFirst(4)), to: &s, follows: follows)) { lines.append("line:\"\(line)\"") } 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()
56 } 69 }
57 for set in o.sets where !apply(set, to: &s, follows: follows) { sets.append("set:\"\(set)\"") }
58 // Each comes twice, as Org reads the options twice; list them once, as written. 70 // Each comes twice, as Org reads the options twice; list them once, as written.
59 for u in lines.reversed() + sets.reversed() where !s.unfollowed.contains(u) { s.unfollowed.append(u) } 71 for u in lines.reversed() + sets.reversed() where !s.unfollowed.contains(u) { s.unfollowed.append(u) }
60 if let script = o["script"] { s.unfollowed.append("script:\"\(script)\"") } 72 if let script = o["script"] { s.unfollowed.append("script:\"\(script)\"") }
@@ -73,6 +85,8 @@ extension TablePlot {
73 guard let m = rest.firstMatch(of: /^\[\s*([^:\]]*?)\s*:\s*([^:\]]*?)\s*\]$/) else { return nil } 85 guard let m = rest.firstMatch(of: /^\[\s*([^:\]]*?)\s*:\s*([^:\]]*?)\s*\]$/) else { return nil }
74 func bound(_ t: Substring) -> Double?? { t.isEmpty || t == "*" ? .some(nil) : Double(t).map { .some($0) } } 86 func bound(_ t: Substring) -> Double?? { t.isEmpty || t == "*" ? .some(nil) : Double(t).map { .some($0) } }
75 guard let low = bound(m.1), let high = bound(m.2) else { return nil } 87 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 }
76 return Settings.Range(low: low, high: high) 90 return Settings.Range(low: low, high: high)
77 } 91 }
78 func step() -> Double? { Double(rest).flatMap { $0 > 0 ? $0 : nil } } 92 func step() -> Double? { Double(rest).flatMap { $0 > 0 ? $0 : nil } }
Tests/OrgAppTests/PlotChartTests.swift +22 −2
@@ -43,17 +43,18 @@ struct PlotChartTests {
43 static let table = "| x | a | b |\n|---+---+---|\n| 1 | 2 | 30 |\n| 2 | 4 | 10 |\n| 3 | 3 | 50 |\n" 43 static let table = "| x | a | b |\n|---+---+---|\n| 1 | 2 | 30 |\n| 2 | 4 | 10 |\n| 3 | 3 | 50 |\n"
44 44
45 @Test func followsTheSetSubset() throws { 45 @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]",
47 "grid", "key left top", "xtics 2", "ytics 0.5"] 47 "grid", "key left top", "xtics 2", "ytics 0.5"]
48 let plot = try Self.plot("#+PLOT: title:\"Given\" ind:1 " + sets.map { "set:\"\($0)\"" }.joined(separator: " ") + "\n" + Self.table) 48 let plot = try Self.plot("#+PLOT: title:\"Given\" ind:1 " + sets.map { "set:\"\($0)\"" }.joined(separator: " ") + "\n" + Self.table)
49 let s = TablePlot.settings(plot) 49 let s = TablePlot.settings(plot)
50 #expect(s.title == "Set" && s.xlabel == "Day" && s.ylabel == "Count") 50 #expect(s.title == "Set" && s.xlabel == "Day" && s.ylabel == "Count")
51 #expect(s.xrange?.low == 0 && s.xrange?.high == 10 && s.yrange?.low == nil && s.yrange?.high == 60) 51 #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)
53 #expect(s.key == .at(vertical: "top", horizontal: "left")) 53 #expect(s.key == .at(vertical: "top", horizontal: "left"))
54 #expect(s.unfollowed.isEmpty && PlotOutput.note(plot) == nil) 54 #expect(s.unfollowed.isEmpty && PlotOutput.note(plot) == nil)
55 55
56 #expect(TablePlot.settings(try Self.plot("#+PLOT: set:\"logscale\"\n" + Self.table)).logX) 56 #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)
57 #expect(TablePlot.settings(try Self.plot("#+PLOT: set:\"key off\"\n" + Self.table)).key == .off) 58 #expect(TablePlot.settings(try Self.plot("#+PLOT: set:\"key off\"\n" + Self.table)).key == .off)
58 #expect(TablePlot.settings(try Self.plot("#+PLOT: line:\"set key bottom\"\n" + Self.table)).key == .at(vertical: "bottom", horizontal: nil)) 59 #expect(TablePlot.settings(try Self.plot("#+PLOT: line:\"set key bottom\"\n" + Self.table)).key == .at(vertical: "bottom", horizontal: nil))
59 // gnuplot reads title:, then line:, then set:, so a set: wins. 60 // gnuplot reads title:, then line:, then set:, so a set: wins.
@@ -69,6 +70,19 @@ struct PlotChartTests {
69 #expect(TablePlot.settings(text).unfollowed == ["set:\"xtics 2\""]) 70 #expect(TablePlot.settings(text).unfollowed == ["set:\"xtics 2\""])
70 let grid = try Self.plot("#+PLOT: type:grid set:\"logscale\" set:\"xlabel 'C'\"\n| 1 | 2 |\n| 3 | 4 |\n") 71 let grid = try Self.plot("#+PLOT: type:grid set:\"logscale\" set:\"xlabel 'C'\"\n| 1 | 2 |\n| 3 | 4 |\n")
71 #expect(TablePlot.settings(grid).unfollowed == ["set:\"logscale\""] && TablePlot.settings(grid).xlabel == "C") 72 #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)
72 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") 86 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")
73 #expect(TablePlot.settings(radar).unfollowed == ["set:\"key off\""] && TablePlot.settings(radar).title == "R") 87 #expect(TablePlot.settings(radar).unfollowed == ["set:\"key off\""] && TablePlot.settings(radar).title == "R")
74 } 88 }
@@ -102,6 +116,12 @@ struct PlotChartTests {
102 #expect(bars.contains(">a</text>") && !bars.contains(">v</text>") && !bars.contains("stroke-dasharray")) 116 #expect(bars.contains(">a</text>") && !bars.contains(">v</text>") && !bars.contains("stroke-dasharray"))
103 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")) 117 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"))
104 #expect(log.contains(">1</text>") && log.contains(">1000</text>") && log.components(separatedBy: "<circle").count - 1 == 2) 118 #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")) }
105 let ranged = try TablePlot.svg(Self.plot("#+PLOT: ind:1 set:\"xrange [0:10]\" set:\"ytics 25\"\n" + Self.table)) 125 let ranged = try TablePlot.svg(Self.plot("#+PLOT: ind:1 set:\"xrange [0:10]\" set:\"ytics 25\"\n" + Self.table))
106 #expect(ranged.contains(">10</text>") && ranged.contains(">25</text>") && ranged.contains(">50</text>")) 126 #expect(ranged.contains(">10</text>") && ranged.contains(">25</text>") && ranged.contains(">50</text>"))
107 #expect(throws: TablePlot.Failure.self) { try TablePlot.svg(Self.plot("#+PLOT: type:3d\n| 1 | 2 |\n| 3 | 4 |\n")) } 127 #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
792twice, as Org reads the options twice. An ind column of timestamps is plotted as dates, and one of text as labels. 792twice, as Org reads the options twice. An ind column of timestamps is plotted as dates, and one of text as labels.
793 793
794A =radar= plot draws each row as an axis, clockwise from the top, and each column after 794A =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 795the first as a series named by the header. Each axis is scaled from its row's lowest and
796highest value, or from =ymin= to =ymax=. Org works out the number of rings from the values; 796highest values rounded outward (120 to 340 becomes 100 to 400), or from =ymin= to =ymax=. Org
797where Org can't (it signals an error, or never finishes, as for a row whose values are all 797works out the number of rings from the values; where Org can't (it signals an error, as for a
798the same), orgstar reports it and asks for =ticks:=. 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.
799 800
800On the Mac, when gnuplot is installed (orgstar looks on the same =PATH= as code blocks), the 801On the Mac, when gnuplot is installed (orgstar looks on the same =PATH= as code blocks), the
801table goes to gnuplot as Emacs sends it: the same data file and script, so every option and 802table 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
803in a sheet. Without gnuplot, and on iOS, orgstar draws the plot itself with Swift Charts: =2d= 804in a sheet. Without gnuplot, and on iOS, orgstar draws the plot itself with Swift Charts: =2d=
804as lines, points or bars by =with:=, =grid= as a heat map, =3d= as a surface (a wireframe in a 805as lines, points or bars by =with:=, =grid= as a heat map, =3d= as a surface (a wireframe in a
805file), =radar= as a web. =file:= then takes =.png=, =.jpg= or =.pdf=, 640 by 480 points as 806file), =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
807shows the new plot. 809shows the new plot.
808 810
809Without gnuplot these =set:= commands are followed, also as a =line:= that starts with =set=: 811Without gnuplot these =set:= commands are followed, also as a =line:= that starts with =set=:
810 812
811| Command | Effect | 813| Command | Effect |
812|------------------------------------------+------------------------------------------------------------| 814|----------------------------------------+-------------------------------------------------------------------------|
813| =title 'T'= | The title, over =title:= | 815| =title 'T'= | The title, over =title:= |
814| =xlabel 'X'=, =ylabel 'Y'= | The axes' names | 816| =xlabel 'X'=, =ylabel 'Y'= | The axes' names |
815| =xrange [0:10]=, =yrange [*:5]= | The axes' ranges; =*= or nothing leaves an end to the data | 817| =xrange [0:10]=, =yrange [*:5]= | The axes' ranges, low to high; =*= or nothing leaves an end to the data |
816| =logscale=, =logscale x=, =logscale y= | Logarithmic axes | 818| =logscale=, =logscale x=, =logscale y= | Logarithmic axes |
817| =grid= | Grid lines at the marks | 819| =grid= | Grid lines at the marks |
818| =key off=, =key top left= | Hides or places the legend (top, bottom, left, right) | 820| =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 | 821| =xtics 2=, =ytics 0.5= | A mark every so many units, on an axis that isn't logarithmic |
820 822
821=2d= plots follow all of them, except that an x axis of text or dates takes no range, steps or 823=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
823and =radar= plots only the title. Other commands, other =line:= options and =script:= are left 825and =radar= plots only the title. Other commands, other =line:= options and =script:= are left
824out, and the message after plotting names them, such as =Not followed without gnuplot: 826out, and the message after plotting names them, such as =Not followed without gnuplot:
825set:"style fill solid"=. 827set:"style fill solid"=.