| @@ -64,10 +64,13 @@ public struct PlotChart: View { |
| 64 | let ticks = o["ticks"].flatMap { if case .number(let n) = $0 { Int(n) } else { nil } } |
64 | let ticks = o["ticks"].flatMap { if case .number(let n) = $0 { Int(n) } else { nil } } |
| 65 | let numericX = series.allSatisfy { $0.points.allSatisfy { if case .number = $0.x { true } else { false } } } |
65 | let numericX = series.allSatisfy { $0.points.allSatisfy { if case .number = $0.x { true } else { false } } } |
| 66 | let bars = ["boxes", "histograms", "hist", "impulses"].contains(o.with) |
66 | let bars = ["boxes", "histograms", "hist", "impulses"].contains(o.with) |
| |
67 | let yGiven = (low: s.yrange?.low ?? PlotChart.number(o["ymin"]), high: s.yrange?.high ?? PlotChart.number(o["ymax"])) |
| |
68 | // A log axis has no 0, so bars stand on its foot, the range the SVG gives it. |
| |
69 | let logBars = bars && s.logY ? try? TablePlot.Axis(values: ys, given: yGiven.low == nil && yGiven.high == nil ? nil : .init(low: yGiven.low, high: yGiven.high), log: true, step: nil, zero: false) : nil |
| 67 | let chart = Chart { |
70 | let chart = Chart { |
| 68 | ForEach(Array(series.enumerated()), id: \.offset) { _, s in |
71 | ForEach(Array(series.enumerated()), id: \.offset) { _, s in |
| 69 | ForEach(Array(s.points.enumerated()), id: \.offset) { _, point in |
72 | ForEach(Array(s.points.enumerated()), id: \.offset) { _, point in |
| 70 | mark(point.x, point.y, series: s.label) |
73 | mark(point.x, point.y, base: logBars?.low, series: s.label) |
| 71 | } |
74 | } |
| 72 | } |
75 | } |
| 73 | } |
76 | } |
| @@ -91,7 +94,9 @@ public struct PlotChart: View { |
| 91 | // As gnuplot autoscales: to the data, from zero for bars. |
94 | // As gnuplot autoscales: to the data, from zero for bars. |
| 92 | let yType: ScaleType? = s.logY ? .log : nil, xType: ScaleType? = s.logX ? .log : nil |
95 | let yType: ScaleType? = s.logY ? .log : nil, xType: ScaleType? = s.logX ? .log : nil |
| 93 | let y = Group { |
96 | let y = Group { |
| 94 | if let domain = PlotChart.domain(s.yrange?.low ?? PlotChart.number(o["ymin"]), s.yrange?.high ?? PlotChart.number(o["ymax"]), values: ys) { |
97 | if let logBars { |
| |
98 | chart.chartYScale(domain: logBars.low...logBars.high, type: yType) |
| |
99 | } else if let domain = PlotChart.domain(yGiven.low, yGiven.high, values: ys) { |
| 95 | chart.chartYScale(domain: domain, type: yType) |
100 | chart.chartYScale(domain: domain, type: yType) |
| 96 | } else { |
101 | } else { |
| 97 | chart.chartYScale(domain: .automatic(includesZero: bars && !s.logY), type: yType) |
102 | chart.chartYScale(domain: .automatic(includesZero: bars && !s.logY), type: yType) |
| @@ -107,25 +112,33 @@ public struct PlotChart: View { |
| 107 | } |
112 | } |
| 108 | |
113 | |
| 109 | @ChartContentBuilder |
114 | @ChartContentBuilder |
| 110 | func mark(_ x: TablePlot.Series.X, _ y: Double, series: String) -> some ChartContent { |
115 | func mark(_ x: TablePlot.Series.X, _ y: Double, base: Double?, series: String) -> some ChartContent { |
| 111 | switch x { |
116 | switch x { |
| 112 | case .number(let n): marks(.value("x", n), y, series) |
117 | case .number(let n): marks(.value("x", n), y, base, series) |
| 113 | case .date(let d): marks(.value("x", d), y, series) |
118 | case .date(let d): marks(.value("x", d), y, base, series) |
| 114 | case .label(let l): marks(.value("x", l), y, series) |
119 | case .label(let l): marks(.value("x", l), y, base, series) |
| 115 | } |
120 | } |
| 116 | } |
121 | } |
| 117 | |
122 | |
| 118 | @ChartContentBuilder |
123 | @ChartContentBuilder |
| 119 | func marks<X: Plottable>(_ x: PlottableValue<X>, _ y: Double, _ series: String) -> some ChartContent { |
124 | func marks<X: Plottable>(_ x: PlottableValue<X>, _ y: Double, _ base: Double?, _ series: String) -> some ChartContent { |
| 120 | switch plot.options.with { |
125 | switch plot.options.with { |
| 121 | case "points", "dots": |
126 | case "points", "dots": |
| 122 | PointMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)) |
127 | PointMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)) |
| 123 | case "linespoints": |
128 | case "linespoints": |
| 124 | LineMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)).symbol(by: .value("Series", series)) |
129 | LineMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)).symbol(by: .value("Series", series)) |
| 125 | case "impulses": |
130 | case "impulses": |
| 126 | BarMark(x: x, y: .value("y", y), width: 2).foregroundStyle(by: .value("Series", series)) |
131 | if let base { |
| |
132 | BarMark(x: x, yStart: .value("y", base), yEnd: .value("y", y), width: 2).foregroundStyle(by: .value("Series", series)) |
| |
133 | } else { |
| |
134 | BarMark(x: x, y: .value("y", y), width: 2).foregroundStyle(by: .value("Series", series)) |
| |
135 | } |
| 127 | case "boxes", "histograms", "hist": |
136 | case "boxes", "histograms", "hist": |
| 128 | BarMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)).position(by: .value("Series", series)) |
137 | if let base { |
| |
138 | BarMark(x: x, yStart: .value("y", base), yEnd: .value("y", y)).foregroundStyle(by: .value("Series", series)).position(by: .value("Series", series)) |
| |
139 | } else { |
| |
140 | BarMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)).position(by: .value("Series", series)) |
| |
141 | } |
| 129 | case "steps": |
142 | case "steps": |
| 130 | LineMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)).interpolationMethod(.stepEnd) |
143 | LineMark(x: x, y: .value("y", y)).foregroundStyle(by: .value("Series", series)).interpolationMethod(.stepEnd) |
| 131 | case "fsteps": |
144 | case "fsteps": |
| @@ -361,10 +374,21 @@ public enum PlotOutput { |
| 361 | } |
374 | } |
| 362 | |
375 | |
| 363 | /// Refuses what the chart can't draw: a radar plot `TablePlot.radar` refuses, which |
376 | /// Refuses what the chart can't draw: a radar plot `TablePlot.radar` refuses, which |
| 364 | /// `TablePlot.plot` leaves to gnuplot when there's a `script:`. |
377 | /// `TablePlot.plot` leaves to gnuplot when there's a `script:`, or one with more than 1000 |
| |
378 | /// rings, and a log axis the SVG refuses, which Swift Charts traps on. |
| 365 | public static func check(_ plot: TablePlot.Plot) throws { |
379 | public static func check(_ plot: TablePlot.Plot) throws { |
| 366 | guard plot.options.type == "radar" else { return } |
380 | switch plot.options.type { |
| 367 | do { _ = try TablePlot.radar(plot) } catch { throw Failure(description: "\(error)") } |
381 | case "radar": |
| |
382 | let radar: TablePlot.Radar |
| |
383 | do { radar = try TablePlot.radar(plot) } catch { throw Failure(description: "\(error)") } |
| |
384 | guard radar.ticks <= 1000 else { throw Failure(description: "Without gnuplot, ticks: takes 0 to 1000") } |
| |
385 | case "2d": |
| |
386 | let s = TablePlot.settings(plot) |
| |
387 | guard s.logX || s.logY else { return } |
| |
388 | do { _ = try TablePlot.svg(plot) } catch { throw Failure(description: "\(error)") } |
| |
389 | default: |
| |
390 | return |
| |
391 | } |
| 368 | } |
392 | } |
| 369 | |
393 | |
| 370 | /// The chart as gnuplot's default 640×480, as PNG, JPEG, PDF or SVG by the file's extension. |
394 | /// The chart as gnuplot's default 640×480, as PNG, JPEG, PDF or SVG by the file's extension. |