| @@ -12,6 +12,9 @@ extension TablePlot { |
| 12 | return power * (f < 1.5 ? 1 : f < 3.5 ? 2 : f < 7.5 ? 5 : 10) |
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 | /// An axis: its ends, widened to whole steps where they're not given, and its ticks. |
18 | /// An axis: its ends, widened to whole steps where they're not given, and its ticks. |
| 16 | public struct Axis { |
19 | public struct Axis { |
| 17 | public var low: Double |
20 | public var low: Double |
| @@ -20,10 +23,11 @@ extension TablePlot { |
| 20 | var ticks: [Double] = [] |
23 | var ticks: [Double] = [] |
| 21 | |
24 | |
| 22 | public init(values: [Double], given: Settings.Range?, log: Bool, step: Double?, zero: Bool) throws { |
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 | // gnuplot refuses these too. |
27 | // 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") } |
28 | 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") } |
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 | if zero, !log { data.append(0) } |
31 | if zero, !log { data.append(0) } |
| 28 | var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1 |
32 | var low = given?.low ?? data.min() ?? 0, high = given?.high ?? data.max() ?? 1 |
| 29 | if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) } |
33 | if low == high { (low, high) = log ? (low / 10, high * 10) : (low - 1, high + 1) } |
| @@ -32,6 +36,7 @@ extension TablePlot { |
| 32 | if log { |
36 | if log { |
| 33 | if given?.low == nil { low = pow(10, log10(low).rounded(.down)) } |
37 | if given?.low == nil { low = pow(10, log10(low).rounded(.down)) } |
| 34 | if given?.high == nil { high = pow(10, log10(high).rounded(.up)) } |
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 | let first = log10(low).rounded(.up), last = log10(high).rounded(.down) |
40 | 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. |
41 | // 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] |
42 | ticks = first <= last ? Array(Int(first)...Int(last)).map { pow(10, Double($0)) } : [low, high] |
| @@ -132,7 +137,7 @@ extension TablePlot { |
| 132 | let base = py(y.log ? y.low : min(max(0, y.low), y.high)) |
137 | let base = py(y.log ? y.low : min(max(0, y.low), y.high)) |
| 133 | for (i, s) in series.enumerated() { |
138 | for (i, s) in series.enumerated() { |
| 134 | let color = colors[i % colors.count] |
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 | guard !points.isEmpty else { continue } |
141 | guard !points.isEmpty else { continue } |
| 137 | func polyline(_ p: [(Double, Double)]) { |
142 | func polyline(_ p: [(Double, Double)]) { |
| 138 | add("<polyline fill=\"none\" stroke=\"\(color)\" stroke-width=\"1.5\" points=\"" + p.map { "\(number($0.0)),\(number($0.1))" }.joined(separator: " ") + "\"/>") |
143 | add("<polyline fill=\"none\" stroke=\"\(color)\" stroke-width=\"1.5\" points=\"" + p.map { "\(number($0.0)),\(number($0.1))" }.joined(separator: " ") + "\"/>") |