@@ -5,7 +5,7 @@ public enum LegendIcon {
5
5
case shape( ScatterPlotSeriesOptions . ScatterPattern , Color )
6
6
}
7
7
8
- public protocol PlotElement {
8
+ public protocol LayoutComponent {
9
9
10
10
/// Returns the minimum size required to display this element.
11
11
///
@@ -28,9 +28,9 @@ public protocol PlotElement {
28
28
func draw( _ rect: Rect , measuredSize: Size , edge: RectEdge , renderer: Renderer )
29
29
}
30
30
31
- /// A `PlotElement ` wrapper which adds padding to an internal `PlotElement `
31
+ /// A `LayoutComponent ` wrapper which adds padding to an internal `LayoutComponent `
32
32
///
33
- private struct PaddedPlotElement < T: PlotElement > : PlotElement {
33
+ private struct _Padded < T: LayoutComponent > : LayoutComponent {
34
34
var base : T
35
35
var padding : EdgeComponents < Float > = . zero
36
36
@@ -48,16 +48,16 @@ private struct PaddedPlotElement<T: PlotElement>: PlotElement {
48
48
base. draw ( adjustedRect, measuredSize: adjustedMeasuredSize, edge: edge, renderer: renderer)
49
49
}
50
50
}
51
- extension PlotElement {
51
+ extension LayoutComponent {
52
52
53
- /// Returns a new `PlotElement ` which adds the given padding to this `PlotElement `.
53
+ /// Returns a new `LayoutComponent ` which adds the given padding to this `LayoutComponent `.
54
54
///
55
- public func padding( _ padding: EdgeComponents < Float > ) -> PlotElement {
56
- return PaddedPlotElement ( base: self , padding: padding)
55
+ public func padding( _ padding: EdgeComponents < Float > ) -> LayoutComponent {
56
+ return _Padded ( base: self , padding: padding)
57
57
}
58
58
}
59
59
60
- public struct Label : PlotElement {
60
+ public struct Label : LayoutComponent {
61
61
var text : String = " "
62
62
var size : Float = 12
63
63
var color : Color = . black
@@ -149,7 +149,7 @@ extension Rect {
149
149
///
150
150
/// The principle 3 components of a `GraphLayout` are:
151
151
/// - The rectangular plot area itself,
152
- /// - Any `PlotElement `s that surround the plot and take up space (e.g. the title, axis markers and labels), and
152
+ /// - Any `LayoutComponent `s that surround the plot and take up space (e.g. the title, axis markers and labels), and
153
153
/// - Any `Annotation`s that are layered on top of the plot and do not take up space in a layout sense (e.g. arrows, watermarks).
154
154
///
155
155
public struct GraphLayout {
@@ -185,7 +185,7 @@ public struct GraphLayout {
185
185
let plotBorderRect : Rect
186
186
187
187
// TODO: Try to move this up to GraphLayout itself.
188
- let elements : EdgeComponents < [ PlotElement ] >
188
+ let components : EdgeComponents < [ LayoutComponent ] >
189
189
190
190
/// The measured sizes of the plot elements.
191
191
let sizes : EdgeComponents < [ Size ] >
@@ -225,7 +225,7 @@ extension GraphLayout {
225
225
226
226
// TODO: refactor "calculateMarkers":
227
227
// - Should be called "layoutContent" or something like that.
228
- // - PlotMarkers return value should be an array of `PlotElement `s.
228
+ // - PlotMarkers return value should be an array of `LayoutComponent `s.
229
229
// - Legend info should be handled by an annotation.
230
230
// - Possibly wrap T inside `Results` (as a generic parameter).
231
231
// - Rename `Results` to `LayoutPlan` or something like that.
@@ -234,23 +234,23 @@ extension GraphLayout {
234
234
calculateMarkers: ( Size ) -> ( T , PlotMarkers ? , [ ( String , LegendIcon ) ] ? ) ) -> ( T , Results ) {
235
235
236
236
// 1. Calculate the plot size. To do that, we first have measure everything outside of the plot.
237
- let elements = makePlotElements ( )
238
- let sizes = elements . mapByEdge { edge, edgeElements -> [ Size ] in
239
- return edgeElements . map { $0. measure ( edge: edge, renderer) }
237
+ let components = makeLayoutComponents ( )
238
+ let sizes = components . mapByEdge { edge, edgeComponents -> [ Size ] in
239
+ return edgeComponents . map { $0. measure ( edge: edge, renderer) }
240
240
}
241
- var plotSize = calcPlotSize ( totalSize: size, plotElements : sizes)
241
+ var plotSize = calcPlotSize ( totalSize: size, componentSizes : sizes)
242
242
243
243
// 2. Call back to the plot to lay out its data. It may ask to adjust the plot size.
244
244
let ( drawingData, markers, legendInfo) = calculateMarkers ( plotSize)
245
245
( drawingData as? AdjustsPlotSize ) . map { plotSize = adjustPlotSize ( plotSize, info: $0) }
246
246
247
247
// 3. Now that we have the final sizes of everything, we can calculate their locations.
248
- let plotRect = layoutPlotRect ( plotSize: plotSize, elementSizes : sizes)
248
+ let plotRect = layoutPlotRect ( plotSize: plotSize, componentSizes : sizes)
249
249
let roundedMarkers = markers. map { var m = $0; roundMarkers ( & m) ; return m } ?? PlotMarkers ( )
250
250
251
251
var results = Results ( totalSize: size,
252
252
plotBorderRect: plotRect,
253
- elements : elements ,
253
+ components : components ,
254
254
sizes: sizes,
255
255
plotMarkers: roundedMarkers,
256
256
legendLabels: legendInfo ?? [ ] )
@@ -263,10 +263,10 @@ extension GraphLayout {
263
263
static let yLabelPadding : Float = 12
264
264
static let titleLabelPadding : Float = 16
265
265
266
- // FIXME: To be removed. These items should already be PlotElements .
267
- private func makePlotElements ( ) -> EdgeComponents < [ PlotElement ] > {
268
- var elements = EdgeComponents < [ PlotElement ] > ( left: [ ] , top: [ ] , right: [ ] , bottom: [ ] )
269
- // TODO: Currently, only labels are "PlotElements" .
266
+ // FIXME: To be removed. These items should already be LayoutComponents .
267
+ private func makeLayoutComponents ( ) -> EdgeComponents < [ LayoutComponent ] > {
268
+ var elements = EdgeComponents < [ LayoutComponent ] > ( left: [ ] , top: [ ] , right: [ ] , bottom: [ ] )
269
+ // TODO: Currently, only labels are "LayoutComponent"s .
270
270
if !plotLabel. xLabel. isEmpty {
271
271
let label = Label ( text: plotLabel. xLabel, size: plotLabel. size)
272
272
. padding ( . all( Self . xLabelPadding) )
@@ -291,14 +291,14 @@ extension GraphLayout {
291
291
}
292
292
293
293
/// Calculates the region of the plot which is used for displaying the plot's data (inside all of the chrome).
294
- private func calcPlotSize( totalSize: Size , plotElements : EdgeComponents < [ Size ] > ) -> Size {
294
+ private func calcPlotSize( totalSize: Size , componentSizes : EdgeComponents < [ Size ] > ) -> Size {
295
295
var plotSize = totalSize
296
296
297
- // Subtract space for the plot elements .
298
- plotElements . left. forEach { plotSize. width -= $0. width }
299
- plotElements . right. forEach { plotSize. width -= $0. width }
300
- plotElements . top. forEach { plotSize. height -= $0. height }
301
- plotElements . bottom. forEach { plotSize. height -= $0. height }
297
+ // Subtract space for the LayoutComponents .
298
+ componentSizes . left. forEach { plotSize. width -= $0. width }
299
+ componentSizes . right. forEach { plotSize. width -= $0. width }
300
+ componentSizes . top. forEach { plotSize. height -= $0. height }
301
+ componentSizes . bottom. forEach { plotSize. height -= $0. height }
302
302
303
303
// Subtract space for the markers.
304
304
// TODO: Make this more accurate.
@@ -318,14 +318,14 @@ extension GraphLayout {
318
318
return plotSize
319
319
}
320
320
321
- private func layoutPlotRect( plotSize: Size , elementSizes : EdgeComponents < [ Size ] > ) -> Rect {
321
+ private func layoutPlotRect( plotSize: Size , componentSizes : EdgeComponents < [ Size ] > ) -> Rect {
322
322
// 1. Calculate the plotBorderRect.
323
323
// We already have the size, so we only need to calculate the origin.
324
324
var plotOrigin = Point . zero
325
325
326
326
// Offset by the left/bottom PlotElements.
327
- plotOrigin. x += elementSizes . left. reduce ( into: 0 ) { $0 += $1. width }
328
- plotOrigin. y += elementSizes . bottom. reduce ( into: 0 ) { $0 += $1. height }
327
+ plotOrigin. x += componentSizes . left. reduce ( into: 0 ) { $0 += $1. width }
328
+ plotOrigin. y += componentSizes . bottom. reduce ( into: 0 ) { $0 += $1. height }
329
329
// Offset by marker sizes (TODO: they are not PlotElements yet, so not handled above).
330
330
let xMarkerHeight = ( 2 * markerTextSize) + 10 // X markers
331
331
let yMarkerWidth = yMarkerMaxWidth + 10 // Y markers
@@ -471,14 +471,14 @@ extension GraphLayout {
471
471
if drawsGridOverForeground {
472
472
drawGrid ( results: results, renderer: renderer)
473
473
}
474
- drawPlotElements ( results. elements , plotRect: results. plotBorderRect,
475
- measuredSizes: results. sizes, renderer: renderer)
474
+ drawLayoutComponents ( results. components , plotRect: results. plotBorderRect,
475
+ measuredSizes: results. sizes, renderer: renderer)
476
476
drawLegend ( results. legendLabels, results: results, renderer: renderer)
477
477
drawAnnotations ( resolver: results, renderer: renderer)
478
478
}
479
479
480
- private func drawPlotElements ( _ plotElements : EdgeComponents < [ PlotElement ] > , plotRect: Rect ,
481
- measuredSizes: EdgeComponents < [ Size ] > , renderer: Renderer ) {
480
+ private func drawLayoutComponents ( _ components : EdgeComponents < [ LayoutComponent ] > , plotRect: Rect ,
481
+ measuredSizes: EdgeComponents < [ Size ] > , renderer: Renderer ) {
482
482
483
483
var plotExternalRect = plotRect
484
484
plotExternalRect. contract ( by: - 1 * plotBorder. thickness)
@@ -487,39 +487,39 @@ extension GraphLayout {
487
487
let yMarkerWidth = yMarkerMaxWidth + 10 // Y markers
488
488
489
489
// Elements are laid out so that [0] is closest to the plot.
490
- // Top elements .
490
+ // Top components .
491
491
var t_height : Float = 0
492
- for (item, idx) in zip ( plotElements . top, plotElements . top. indices) {
492
+ for (item, idx) in zip ( components . top, components . top. indices) {
493
493
let itemSize = measuredSizes. top [ idx]
494
494
let rect = Rect ( origin: Point ( plotExternalRect. minX, plotExternalRect. maxY + t_height) ,
495
495
size: Size ( width: plotExternalRect. width, height: itemSize. height) )
496
496
t_height += itemSize. height
497
497
renderer. debug? . drawSolidRect ( rect, fillColor: Color . random ( ) . withAlpha ( 1 ) , hatchPattern: . none)
498
498
item. draw ( rect, measuredSize: itemSize, edge: . top, renderer: renderer)
499
499
}
500
- // Bottom elements .
500
+ // Bottom components .
501
501
var b_height : Float = xMarkerHeight
502
- for (item, idx) in zip ( plotElements . bottom, plotElements . bottom. indices) {
502
+ for (item, idx) in zip ( components . bottom, components . bottom. indices) {
503
503
let itemSize = measuredSizes. bottom [ idx]
504
504
let rect = Rect ( origin: Point ( plotExternalRect. minX, plotExternalRect. minY - b_height - itemSize. height) ,
505
505
size: Size ( width: plotExternalRect. width, height: itemSize. height) )
506
506
b_height += itemSize. height
507
507
renderer. debug? . drawSolidRect ( rect, fillColor: Color . random ( ) . withAlpha ( 1 ) , hatchPattern: . none)
508
508
item. draw ( rect, measuredSize: itemSize, edge: . bottom, renderer: renderer)
509
509
}
510
- // Right elements .
510
+ // Right components .
511
511
var r_width : Float = yMarkerWidth //results.plotMarkers.y2Markers.isEmpty ? 0 : yMarkerWidth
512
- for (item, idx) in zip ( plotElements . right, plotElements . right. indices) {
512
+ for (item, idx) in zip ( components . right, components . right. indices) {
513
513
let itemSize = measuredSizes. right [ idx]
514
514
let rect = Rect ( origin: Point ( plotExternalRect. maxX + r_width, plotExternalRect. minY) ,
515
515
size: Size ( width: itemSize. width, height: plotExternalRect. height) )
516
516
r_width += itemSize. width
517
517
renderer. debug? . drawSolidRect ( rect, fillColor: Color . random ( ) . withAlpha ( 1 ) , hatchPattern: . none)
518
518
item. draw ( rect, measuredSize: itemSize, edge: . right, renderer: renderer)
519
519
}
520
- // Left elements .
520
+ // Left components .
521
521
var l_width : Float = yMarkerWidth
522
- for (item, idx) in zip ( plotElements . left, plotElements . left. indices) {
522
+ for (item, idx) in zip ( components . left, components . left. indices) {
523
523
let itemSize = measuredSizes. left [ idx]
524
524
let rect = Rect ( origin: Point ( plotExternalRect. minX - l_width - itemSize. width, plotExternalRect. minY) ,
525
525
size: Size ( width: itemSize. width, height: plotExternalRect. height) )
0 commit comments