Commit f308232999

f308232999573b4528bcd3fdc6fc5b5d8f1126a3

parent: 8ea52e8b2c

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 12:18 UTC

Leave out log-axis values outside 1e-307 to 1e308 instead of trapping

Layout: unified · split

Sources/OrgApp/PlotChart.swift +2 −2
@@ -52,8 +52,8 @@ public struct PlotChart: View {
5252 let series = TablePlot.series(plot).map { series in
5353 var series = series
5454 series.points.removeAll { point in
55 if s.logY, point.y <= 0 { return true }
56 if s.logX, case .number(let n) = point.x, n <= 0 { return true }
55 if s.logY, !TablePlot.logRange.contains(point.y) { return true }
56 if s.logX, case .number(let n) = point.x, !TablePlot.logRange.contains(n) { return true }
5757 return false
5858 }
5959 return series
Sources/OrgCore/Compute/PlotSVG.swift +7 −2
@@ -12,6 +12,9 @@ extension TablePlot {
1212 return power * (f < 1.5 ? 1 : f < 3.5 ? 2 : f < 7.5 ? 5 : 10)
1313 }
1414
15 /// What a log axis places; gnuplot skips undefined points, and Swift Charts traps past these.
16 public static let logRange = 1e-307...1e308
17
1518 /// An axis: its ends, widened to whole steps where they're not given, and its ticks.
1619 public struct Axis {
1720 public var low: Double
@@ -20,10 +23,11 @@ extension TablePlot {
2023 var ticks: [Double] = []
2124
2225 public init(values: [Double], given: Settings.Range?, log: Bool, step: Double?, zero: Bool) throws {
23 var data = values.filter { !log || $0 > 0 }
26 var data = values.filter { !log || TablePlot.logRange.contains($0) }
2427 // gnuplot refuses these too.
2528 if log, data.isEmpty, given?.low == nil || given?.high == nil { throw Failure(description: "A log axis needs values above 0") }
2629 if log, [given?.low, given?.high].contains(where: { ($0 ?? 1) <= 0 }) { throw Failure(description: "A log axis's range must be above 0") }
30 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") }
2731 if zero, !log { data.append(0) }
2832 var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1
2933 if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) }
@@ -32,6 +36,7 @@ extension TablePlot {
3236 if log {
3337 if given?.low == nil { low = pow(10, log10(low).rounded(.down)) }
3438 if given?.high == nil { high = pow(10, log10(high).rounded(.up)) }
39 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") }
3540 let first = log10(low).rounded(.up), last = log10(high).rounded(.down)
3641 // A range with no power of ten in it is labelled at its ends.
3742 ticks = first <= last ? Array(Int(first)...Int(last)).map { pow(10, Double($0)) } : [low, high]
@@ -132,7 +137,7 @@ extension TablePlot {
132137 let base = py(y.log ? y.low : min(max(0, y.low), y.high))
133138 for (i, s) in series.enumerated() {
134139 let color = colors[i % colors.count]
135 let points = s.points.filter { (!y.log || $0.y > 0) && (!x.log || xValue($0.x) > 0) }.map { (px(xValue($0.x)), py($0.y)) }
140 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)) }
136141 guard !points.isEmpty else { continue }
137142 func polyline(_ p: [(Double, Double)]) {
138143 add("<polyline fill=\"none\" stroke=\"\(color)\" stroke-width=\"1.5\" points=\"" + p.map { "\(number($0.0)),\(number($0.1))" }.joined(separator: " ") + "\"/>")
Tests/OrgAppTests/PlotChartTests.swift +19
@@ -129,6 +129,25 @@ struct PlotChartTests {
129129 }
130130 }
131131
132 @Test func skipsLogPointsPastADouble() throws {
133 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-log-\(UUID().uuidString)")
134 defer { try? FileManager.default.removeItem(at: folder) }
135 for axis in ["x", "y"] {
136 for field in ["1e400", "3e-324", "1e-320", "1.5e308", "1.0e+INF"] {
137 let row = axis == "x" ? "| \(field) | 5 |" : "| 2 | \(field) |"
138 let plot = try Self.plot("#+PLOT: ind:1 set:\"logscale \(axis)\"\n| x | y |\n|---+---|\n| 1 | 10 |\n\(row)\n| 3 | 20 |\n")
139 try PlotOutput.check(plot)
140 let svg = try TablePlot.svg(plot)
141 #expect(!svg.contains("inf") && !svg.contains("nan"), "\(axis) \(field)")
142 try PlotOutput.write(plot, to: folder.appendingPathComponent("l.png"))
143 }
144 }
145 for range in ["[1:1e400]", "[1e-320:10]"] {
146 let plot = try Self.plot("#+PLOT: ind:1 set:\"logscale y\" set:\"yrange \(range)\"\n| 1 | 10 |\n| 2 | 20 |\n")
147 #expect(throws: PlotOutput.Failure.self, "\(range)") { try PlotOutput.check(plot) }
148 }
149 }
150
132151 @Test func capsRadarRingsWithoutGnuplot() throws {
133152 let plot = try Self.plot("#+PLOT: type:radar ticks:2000\n| n | a | b |\n|---+---+---|\n| x | 1 | 5 |\n| y | 2 | 3 |\n")
134153 #expect(try TablePlot.radarSetup(plot).hasSuffix(" 2000\nEOD\n"))
docs/manual/guide/10-tables.org +3 −2
@@ -806,8 +806,9 @@ gnuplot's own terminals and styles apply. A plot without =file:= is drawn to an
806806in a sheet. Without gnuplot, and on iOS, orgstar draws the plot itself with Swift Charts: =2d=
807807as lines, points or bars by =with:=, =grid= as a heat map, =3d= as a surface (a wireframe in a
808808file), =radar= as a web. =file:= then takes =.png=, =.jpg= or =.pdf=, 640 by 480 points as
809gnuplot's default, or =.svg= for a =2d= plot. A log axis needs values above 0, and its
810range, if given, must be above 0. If inline images are showing, a link to the file
809gnuplot's default, or =.svg= for a =2d= plot. A log axis places values from 1e-307 to
8101e308 and leaves out others; it needs at least one, and its range, if given, must lie
811within them. If inline images are showing, a link to the file
811812shows the new plot.
812813
813814Without gnuplot these =set:= commands are followed, also as a =line:= that starts with =set=: