Skip to content

Commit 6f81bea

Browse files
WilliamHYZhangKarthikRIyer
authored andcommitted
Add Support for Coordinate Conversion (#88)
* chor: cleanup rotatePoint arguments * intial coordinate conversion commit * Revert "chor: cleanup rotatePoint arguments" This reverts commit c72986e. * feat: add coordiante type, overhaul annotations * feat: remove installation output * Revert "feat: remove installation output" This reverts commit 5bcf936. * implement protocols and methods * bfix * bfix * work on resolve function * add figure/axes fraction resolving support
1 parent f5c22e6 commit 6f81bea

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

Sources/SwiftPlot/GraphLayout.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public struct GraphLayout {
2222
/// The amount of (horizontal) space to reserve for markers on the Y-axis.
2323
var yMarkerMaxWidth: Float = 40
2424

25-
struct Results {
25+
struct Results : CoordinateResolver {
2626
/// The size these results have been calculated for; the entire size of the plot.
2727
let totalSize: Size
2828

@@ -51,6 +51,25 @@ public struct GraphLayout {
5151

5252
var legendLabels: [(String, LegendIcon)] = []
5353
var legendRect: Rect?
54+
55+
func resolve(_ coordinate: Coordinate) -> Point {
56+
let x = coordinate.point.x
57+
let y = coordinate.point.y
58+
switch(coordinate.coordinateSpace) {
59+
case .figurePoints:
60+
return Point(x, y)
61+
case .axesPoints:
62+
return Point(x, y) + plotBorderRect.origin
63+
case .figureFraction:
64+
let maxX = plotBorderRect.origin.x + plotBorderRect.size.width
65+
let maxY = plotBorderRect.origin.y + plotBorderRect.size.height
66+
return Point(x * maxX, y * maxY)
67+
case .axesFraction:
68+
let maxX = plotBorderRect.size.width
69+
let maxY = plotBorderRect.size.height
70+
return Point(x * maxX, y * maxY) + plotBorderRect.origin
71+
}
72+
}
5473
}
5574

5675
// Layout.
@@ -206,7 +225,7 @@ public struct GraphLayout {
206225
drawTitle(results: results, renderer: renderer)
207226
drawLabels(results: results, renderer: renderer)
208227
drawLegend(results.legendLabels, results: results, renderer: renderer)
209-
drawAnnotations(renderer: renderer)
228+
drawAnnotations(resolver: results, renderer: renderer)
210229
}
211230

212231
private func drawTitle(results: Results, renderer: Renderer) {
@@ -379,9 +398,9 @@ public struct GraphLayout {
379398
}
380399
}
381400

382-
func drawAnnotations(renderer: Renderer) {
401+
func drawAnnotations(resolver: CoordinateResolver, renderer: Renderer) {
383402
for var annotation in annotations{
384-
annotation.draw(renderer: renderer)
403+
annotation.draw(resolver: resolver, renderer: renderer)
385404
}
386405
}
387406
}

Sources/SwiftPlot/PlotStyleHelpers.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,30 @@ public struct PlotLegend {
4343
public init() {}
4444
}
4545

46+
public struct Coordinate {
47+
public enum CoordinateSpace {
48+
case figurePoints
49+
case figureFraction
50+
case axesPoints
51+
case axesFraction
52+
}
53+
public var coordinateSpace: CoordinateSpace = .figurePoints
54+
public var point: Point = Point(0.0, 0.0)
55+
}
56+
57+
public protocol CoordinateResolver {
58+
func resolve(_ coordinate: Coordinate) -> Point
59+
}
60+
4661
public protocol Annotation {
47-
mutating func draw(renderer: Renderer)
62+
mutating func draw(resolver: CoordinateResolver, renderer: Renderer)
4863
}
4964

5065
struct Box: Annotation {
5166
public var color = Color.black
5267
public var location = Point(0.0, 0.0)
5368
public var size = Size(width: 0.0, height: 0.0)
54-
public func draw(renderer: Renderer) {
69+
public func draw(resolver: CoordinateResolver, renderer: Renderer) {
5570
renderer.drawSolidRect(Rect(origin: location, size: size),
5671
fillColor: color,
5772
hatchPattern: .none)
@@ -70,14 +85,14 @@ struct Text : Annotation {
7085
public var location = Point(0.0, 0.0)
7186
public var boundingBox: Box?
7287
public var borderWidth: Float = 5
73-
public mutating func draw(renderer: Renderer) {
88+
public mutating func draw(resolver: CoordinateResolver, renderer: Renderer) {
7489
if boundingBox != nil {
7590
var bboxSize = renderer.getTextLayoutSize(text: text, textSize: size)
7691
bboxSize.width += 2 * borderWidth
7792
bboxSize.height += 2 * borderWidth
7893
boundingBox?.location = Point(location.x - borderWidth, location.y - borderWidth)
7994
boundingBox?.size = bboxSize
80-
boundingBox?.draw(renderer: renderer)
95+
boundingBox?.draw(resolver: resolver, renderer: renderer)
8196
}
8297
renderer.drawText(text: text,
8398
location: location,
@@ -107,7 +122,7 @@ struct Arrow : Annotation {
107122
public var isFilled: Bool = false
108123
public var startAnnotation: Annotation?
109124
public var endAnnotation: Annotation?
110-
public mutating func draw(renderer: Renderer) {
125+
public mutating func draw(resolver: CoordinateResolver, renderer: Renderer) {
111126
// Draws arrow body.
112127
renderer.drawPlotLines(points: [start, end],
113128
strokeWidth: strokeWidth,
@@ -134,8 +149,8 @@ struct Arrow : Annotation {
134149
}
135150

136151
//Draws start and end annotations if specified.
137-
startAnnotation?.draw(renderer: renderer)
138-
endAnnotation?.draw(renderer: renderer)
152+
startAnnotation?.draw(resolver: resolver, renderer: renderer)
153+
endAnnotation?.draw(resolver: resolver, renderer: renderer)
139154

140155
}
141156
public init(color: Color = .black, start: Point = Point(0.0, 0.0), end: Point = Point(0.0, 0.0), strokeWidth: Float = 5, headLength: Float = 10, headAngle: Float = 20, isDashed: Bool = false, isFilled: Bool = false, startAnnotation: Annotation? = nil, endAnnotation: Annotation? = nil) {

0 commit comments

Comments
 (0)