Commit 777d40b51d
777d40b51d0f1e570ff818423e7c022669f3c5d1
parent: f308232999
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-21 12:18 UTC
Keep a lone set key direction's other side at gnuplot's right top in charts
Layout: unified · split
Sources/OrgApp/PlotChart.swift
+15 −15
| @@ -216,22 +216,22 @@ public struct PlotChart: View { |
| 216 | extension View { |
216 | extension View { |
| 217 | /// The legend as `set key` places it, else shown or not. |
217 | /// The legend as `set key` places it, else shown or not. |
| 218 | func key(_ key: TablePlot.Settings.Key?, shown: Bool) -> some View { |
218 | func key(_ key: TablePlot.Settings.Key?, shown: Bool) -> some View { |
| 219 | var position = AnnotationPosition.automatic |
| |
| 220 | if case .at(let vertical, let horizontal) = key { |
| |
| 221 | switch (vertical, horizontal) { |
| |
| 222 | case ("top", "left"): position = .topLeading |
| |
| 223 | case ("top", "right"): position = .topTrailing |
| |
| 224 | case ("bottom", "left"): position = .bottomLeading |
| |
| 225 | case ("bottom", "right"): position = .bottomTrailing |
| |
| 226 | case ("top", _): position = .top |
| |
| 227 | case ("bottom", _): position = .bottom |
| |
| 228 | case (_, "left"): position = .leading |
| |
| 229 | case (_, "right"): position = .trailing |
| |
| 230 | default: break |
| |
| 231 | } |
| |
| 232 | } |
| |
| 233 | let visible = key.map { $0 != .off } ?? shown |
219 | let visible = key.map { $0 != .off } ?? shown |
| 234 | return chartLegend(visible ? .visible : .hidden).chartLegend(position: position) |
220 | return chartLegend(visible ? .visible : .hidden).chartLegend(position: PlotChart.keyPosition(key)) |
| |
221 | } |
| |
222 | } |
| |
223 | |
| |
224 | extension PlotChart { |
| |
225 | /// Where `set key` puts the legend; gnuplot keeps a direction not given at its default, right top. |
| |
226 | static func keyPosition(_ key: TablePlot.Settings.Key?) -> AnnotationPosition { |
| |
227 | guard case .at(let vertical, let horizontal) = key else { return .automatic } |
| |
228 | switch (vertical ?? "top", horizontal ?? "right") { |
| |
229 | case ("top", "left"): return .topLeading |
| |
230 | case ("top", "right"): return .topTrailing |
| |
231 | case ("bottom", "left"): return .bottomLeading |
| |
232 | case ("bottom", "right"): return .bottomTrailing |
| |
233 | default: return .automatic |
| |
234 | } |
| 235 | } |
235 | } |
| 236 | } |
236 | } |
| 237 | |
237 | |
Tests/OrgAppTests/PlotChartTests.swift
+12
| @@ -1,3 +1,4 @@ |
| |
1 | import Charts |
| 1 | import Foundation |
2 | import Foundation |
| 2 | import OrgCore |
3 | import OrgCore |
| 3 | @testable import OrgApp |
4 | @testable import OrgApp |
| @@ -148,6 +149,17 @@ struct PlotChartTests { |
| 148 | } |
149 | } |
| 149 | } |
150 | } |
| 150 | |
151 | |
| |
152 | @Test func placesALoneKeyDirectionAsGnuplot() throws { |
| |
153 | func position(_ set: String) throws -> String { |
| |
154 | String(describing: PlotChart.keyPosition(TablePlot.settings(try Self.plot("#+PLOT: set:\"\(set)\"\n" + Self.table)).key)) |
| |
155 | } |
| |
156 | #expect(try position("key top") == String(describing: AnnotationPosition.topTrailing)) |
| |
157 | #expect(try position("key left") == String(describing: AnnotationPosition.topLeading)) |
| |
158 | #expect(try position("key bottom") == String(describing: AnnotationPosition.bottomTrailing)) |
| |
159 | #expect(try position("key right") == String(describing: AnnotationPosition.topTrailing)) |
| |
160 | #expect(try position("key bottom left") == String(describing: AnnotationPosition.bottomLeading)) |
| |
161 | } |
| |
162 | |
| 151 | @Test func capsRadarRingsWithoutGnuplot() throws { |
163 | @Test func capsRadarRingsWithoutGnuplot() throws { |
| 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") |
164 | let plot = try Self.plot("#+PLOT: type:radar ticks:2000\n| n | a | b |\n|---+---+---|\n| x | 1 | 5 |\n| y | 2 | 3 |\n") |
| 153 | #expect(try TablePlot.radarSetup(plot).hasSuffix(" 2000\nEOD\n")) |
165 | #expect(try TablePlot.radarSetup(plot).hasSuffix(" 2000\nEOD\n")) |
docs/manual/guide/10-tables.org
+1 −1
| @@ -820,7 +820,7 @@ Without gnuplot these =set:= commands are followed, also as a =line:= that start |
| 820 | | =xrange [0:10]=, =yrange [*:5]= | The axes' ranges, low to high; =*= or nothing leaves an end to the data | |
820 | | =xrange [0:10]=, =yrange [*:5]= | The axes' ranges, low to high; =*= or nothing leaves an end to the data | |
| 821 | | =logscale=, =logscale x=, =logscale y= | Logarithmic axes | |
821 | | =logscale=, =logscale x=, =logscale y= | Logarithmic axes | |
| 822 | | =grid= | Grid lines at the marks | |
822 | | =grid= | Grid lines at the marks | |
| 823 | | =key off=, =key top left= | Hides or places the legend (top, bottom, left, right) | |
823 | | =key off=, =key top left= | Hides or places the legend (top, bottom, left, right; else top right) | |
| 824 | | =xtics 2=, =ytics 0.5= | A mark every so many units, on an axis that isn't logarithmic | |
824 | | =xtics 2=, =ytics 0.5= | A mark every so many units, on an axis that isn't logarithmic | |
| 825 | |
825 | |
| 826 | =2d= plots follow all of them, except that an x axis of text or dates takes no range, steps or |
826 | =2d= plots follow all of them, except that an x axis of text or dates takes no range, steps or |