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 {
216216extension View {
217217 /// The legend as `set key` places it, else shown or not.
218218 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 }
233219 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
224extension 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 }
235235 }
236236}
237237
Tests/OrgAppTests/PlotChartTests.swift +12
@@ -1,3 +1,4 @@
1import Charts
12import Foundation
23import OrgCore
34@testable import OrgApp
@@ -148,6 +149,17 @@ struct PlotChartTests {
148149 }
149150 }
150151
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
151163 @Test func capsRadarRingsWithoutGnuplot() throws {
152164 let plot = try Self.plot("#+PLOT: type:radar ticks:2000\n| n | a | b |\n|---+---+---|\n| x | 1 | 5 |\n| y | 2 | 3 |\n")
153165 #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
820820| =xrange [0:10]=, =yrange [*:5]= | The axes' ranges, low to high; =*= or nothing leaves an end to the data |
821821| =logscale=, =logscale x=, =logscale y= | Logarithmic axes |
822822| =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) |
824824| =xtics 2=, =ytics 0.5= | A mark every so many units, on an axis that isn't logarithmic |
825825
826826=2d= plots follow all of them, except that an x axis of text or dates takes no range, steps or