Commit f308232999
Verified · cmc
Layout: unified · split
Sources/OrgApp/PlotChart.swift +2 −2
| @@ -52,8 +52,8 @@ public struct PlotChart: View { | ||
| 52 | 52 | let series = TablePlot.series(plot).map { series in |
| 53 | 53 | var series = series |
| 54 | 54 | 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 } | |
| 57 | 57 | return false |
| 58 | 58 | } |
| 59 | 59 | return series |
Sources/OrgCore/Compute/PlotSVG.swift +7 −2
| @@ -12,6 +12,9 @@ extension TablePlot { | ||
| 12 | 12 | return power * (f < 1.5 ? 1 : f < 3.5 ? 2 : f < 7.5 ? 5 : 10) |
| 13 | 13 | } |
| 14 | 14 | |
| 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 | ||
| 15 | 18 | /// An axis: its ends, widened to whole steps where they're not given, and its ticks. |
| 16 | 19 | public struct Axis { |
| 17 | 20 | public var low: Double |
| @@ -20,10 +23,11 @@ extension TablePlot { | ||
| 20 | 23 | var ticks: [Double] = [] |
| 21 | 24 | |
| 22 | 25 | 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) } | |
| 24 | 27 | // gnuplot refuses these too. |
| 25 | 28 | if log, data.isEmpty, given?.low == nil || given?.high == nil { throw Failure(description: "A log axis needs values above 0") } |
| 26 | 29 | 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") } | |
| 27 | 31 | if zero, !log { data.append(0) } |
| 28 | 32 | var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1 |
| 29 | 33 | if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) } |
| @@ -32,6 +36,7 @@ extension TablePlot { | ||
| 32 | 36 | if log { |
| 33 | 37 | if given?.low == nil { low = pow(10, log10(low).rounded(.down)) } |
| 34 | 38 | 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") } | |
| 35 | 40 | let first = log10(low).rounded(.up), last = log10(high).rounded(.down) |
| 36 | 41 | // A range with no power of ten in it is labelled at its ends. |
| 37 | 42 | ticks = first <= last ? Array(Int(first)...Int(last)).map { pow(10, Double($0)) } : [low, high] |
| @@ -132,7 +137,7 @@ extension TablePlot { | ||
| 132 | 137 | let base = py(y.log ? y.low : min(max(0, y.low), y.high)) |
| 133 | 138 | for (i, s) in series.enumerated() { |
| 134 | 139 | 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)) } | |
| 136 | 141 | guard !points.isEmpty else { continue } |
| 137 | 142 | func polyline(_ p: [(Double, Double)]) { |
| 138 | 143 | 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 { | ||
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | |
| 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 | ||
| 132 | 151 | @Test func capsRadarRingsWithoutGnuplot() throws { |
| 133 | 152 | let plot = try Self.plot("#+PLOT: type:radar ticks:2000\n| n | a | b |\n|---+---+---|\n| x | 1 | 5 |\n| y | 2 | 3 |\n") |
| 134 | 153 | #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 | ||
| 806 | 806 | in a sheet. Without gnuplot, and on iOS, orgstar draws the plot itself with Swift Charts: =2d= |
| 807 | 807 | as lines, points or bars by =with:=, =grid= as a heat map, =3d= as a surface (a wireframe in a |
| 808 | 808 | file), =radar= as a web. =file:= then takes =.png=, =.jpg= or =.pdf=, 640 by 480 points as |
| 809 | gnuplot's default, or =.svg= for a =2d= plot. A log axis needs values above 0, and its | |
| 810 | range, if given, must be above 0. If inline images are showing, a link to the file | |
| 809 | gnuplot's default, or =.svg= for a =2d= plot. A log axis places values from 1e-307 to | |
| 810 | 1e308 and leaves out others; it needs at least one, and its range, if given, must lie | |
| 811 | within them. If inline images are showing, a link to the file | |
| 811 | 812 | shows the new plot. |
| 812 | 813 | |
| 813 | 814 | Without gnuplot these =set:= commands are followed, also as a =line:= that starts with =set=: |