Skip to content

Commit 0abb40b

Browse files
committed
Round markers (and hence gridlines) to integer coordinates. This results in a very tiny (like, sub-pixel) loss to accuracy, but results in a better image with sharper gridlines.
1 parent 5c0d5d2 commit 0abb40b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Sources/SwiftPlot/GraphLayout.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,14 @@ public struct GraphLayout {
8686
calcLabelLocations(&results)
8787
// 4. Let the plot calculate its scale, calculate marker positions.
8888
let (drawingData, markers, legendInfo) = calculateMarkers(results.plotBorderRect.size)
89-
markers.map { results.plotMarkers = $0 }
89+
markers.map {
90+
var markers = $0
91+
// 5. Round the markers to integer values.
92+
roundMarkers(&markers)
93+
results.plotMarkers = markers
94+
}
9095
legendInfo.map { results.legendLabels = $0 }
91-
// 5. Lay out remaining chrome.
96+
// 6. Lay out remaining chrome.
9297
calcMarkerTextLocations(renderer: renderer, results: &results)
9398
calcLegend(results.legendLabels, renderer: renderer, results: &results)
9499
return (drawingData, results)
@@ -153,6 +158,19 @@ public struct GraphLayout {
153158
borderRect.size.height.round(.down)
154159
return borderRect
155160
}
161+
162+
/// Rounds the given markers to integer pixel locations, for sharper gridlines.
163+
private func roundMarkers(_ markers: inout PlotMarkers) {
164+
for i in markers.xMarkers.indices {
165+
markers.xMarkers[i].round(.down)
166+
}
167+
for i in markers.yMarkers.indices {
168+
markers.yMarkers[i].round(.down)
169+
}
170+
for i in markers.y2Markers.indices {
171+
markers.y2Markers[i].round(.down)
172+
}
173+
}
156174

157175
/// Lays out the chrome elements outside the plot's borders (axis titles, plot title, etc).
158176
private func calcLabelLocations(_ results: inout Results) {

0 commit comments

Comments
 (0)