Skip to content

Commit

Permalink
Make drawing layers get the data from their own plot. This means each…
Browse files Browse the repository at this point in the history
… plot can show different data
  • Loading branch information
philackm committed Jul 3, 2017
1 parent 8b88448 commit cdd81af
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Classes/Drawing/BarDrawingLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class BarDrawingLayer: ScrollableGraphViewDrawingLayer {

var location = CGPoint.zero

if let pointLocation = self.graphViewDrawingDelegate?.graphPoint(forIndex: i).location {
if let pointLocation = owner?.graphPoint(forIndex: i).location {
location = pointLocation
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Drawing/DotDrawingLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class DotDrawingLayer: ScrollableGraphViewDrawingLayer {

var location = CGPoint.zero

if let pointLocation = self.graphViewDrawingDelegate?.graphPoint(forIndex: i).location {
if let pointLocation = owner?.graphPoint(forIndex: i).location {
location = pointLocation
}

Expand Down
15 changes: 9 additions & 6 deletions Classes/Drawing/LineDrawingLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal class LineDrawingLayer : ScrollableGraphViewDrawingLayer {
private var leftmostPointPadding: CGFloat = 50
private var rightmostPointPadding: CGFloat = 50


init(frame: CGRect, lineWidth: CGFloat, lineColor: UIColor, lineStyle: ScrollableGraphViewLineStyle, lineJoin: String, lineCap: String, shouldFill: Bool, lineCurviness: CGFloat) {

self.lineStyle = lineStyle
Expand All @@ -41,6 +40,10 @@ internal class LineDrawingLayer : ScrollableGraphViewDrawingLayer {

internal func createLinePath() -> UIBezierPath {

guard let owner = owner else {
return UIBezierPath()
}

// Can't really do anything without the delegate.
guard let delegate = self.graphViewDrawingDelegate else {
return currentLinePath
Expand All @@ -62,7 +65,7 @@ internal class LineDrawingLayer : ScrollableGraphViewDrawingLayer {
// Connect the line to the starting edge if we are filling it.
if(shouldFill) {
// Add a line from the base of the graph to the first data point.
let firstDataPoint = delegate.graphPoint(forIndex: activePointsInterval.lowerBound)
let firstDataPoint = owner.graphPoint(forIndex: activePointsInterval.lowerBound)

let viewportLeftZero = CGPoint(x: firstDataPoint.location.x - (leftmostPointPadding), y: zeroYPosition)
let leftFarEdgeTop = CGPoint(x: firstDataPoint.location.x - (leftmostPointPadding + viewportWidth), y: zeroYPosition)
Expand All @@ -74,23 +77,23 @@ internal class LineDrawingLayer : ScrollableGraphViewDrawingLayer {
pathSegmentAdder(viewportLeftZero, CGPoint(x: firstDataPoint.location.x, y: firstDataPoint.location.y), currentLinePath)
}
else {
let firstDataPoint = delegate.graphPoint(forIndex: activePointsInterval.lowerBound)
let firstDataPoint = owner.graphPoint(forIndex: activePointsInterval.lowerBound)
currentLinePath.move(to: firstDataPoint.location)
}

// Connect each point on the graph with a segment.
for i in activePointsInterval.lowerBound ..< activePointsInterval.upperBound - 1 {

let startPoint = delegate.graphPoint(forIndex: i).location
let endPoint = delegate.graphPoint(forIndex: i+1).location
let startPoint = owner.graphPoint(forIndex: i).location
let endPoint = owner.graphPoint(forIndex: i+1).location

pathSegmentAdder(startPoint, endPoint, currentLinePath)
}

// Connect the line to the ending edge if we are filling it.
if(shouldFill) {
// Add a line from the last data point to the base of the graph.
let lastDataPoint = delegate.graphPoint(forIndex: activePointsInterval.upperBound - 1).location
let lastDataPoint = owner.graphPoint(forIndex: activePointsInterval.upperBound - 1).location

let viewportRightZero = CGPoint(x: lastDataPoint.x + (rightmostPointPadding), y: zeroYPosition)
let rightFarEdgeTop = CGPoint(x: lastDataPoint.x + (rightmostPointPadding + viewportWidth), y: zeroYPosition)
Expand Down
1 change: 1 addition & 0 deletions Classes/Drawing/ScrollableGraphViewDrawingLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class ScrollableGraphViewDrawingLayer : CAShapeLayer {
var viewportHeight: CGFloat = 0
var zeroYPosition: CGFloat = 0

weak var owner: Plot?
var graphViewDrawingDelegate: ScrollableGraphViewDrawingDelegate? = nil // TODO: Move this to the plot. Plots have 1 or more drawing layers. Can access the drawing delegate through the parent plot.

var active = true
Expand Down
2 changes: 2 additions & 0 deletions Classes/Plots/BarPlot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ open class BarPlot : Plot {
barLineWidth: barLineWidth,
barLineColor: barLineColor,
shouldRoundCorners: shouldRoundBarCorners)

barLayer?.owner = self
}
}
}
2 changes: 2 additions & 0 deletions Classes/Plots/DotPlot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ open class DotPlot : Plot {
// Data Point layer
if(shouldDrawDataPoint) {
dataPointLayer = DotDrawingLayer(frame: viewport, fillColor: dataPointFillColor, dataPointType: dataPointType, dataPointSize: dataPointSize, customDataPointPath: customDataPointPath)

dataPointLayer?.owner = self
}
}
}
4 changes: 4 additions & 0 deletions Classes/Plots/LinePlot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,9 @@ open class LinePlot : Plot {
gradientLayer = GradientDrawingLayer(frame: viewport, startColor: fillGradientStartColor, endColor: fillGradientEndColor, gradientType: fillGradientType, lineDrawingLayer: lineLayer!)
}
}

lineLayer?.owner = self
fillLayer?.owner = self
gradientLayer?.owner = self
}
}
1 change: 0 additions & 1 deletion Classes/Protocols/ScrollableGraphViewDrawingDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import UIKit
internal protocol ScrollableGraphViewDrawingDelegate {
func intervalForActivePoints() -> CountableRange<Int>
func rangeForActivePoints() -> (min: Double, max: Double)
func graphPoint(forIndex index: Int) -> GraphPoint
func calculatePosition(atIndex index: Int, value: Double) -> CGPoint
func currentViewport() -> CGRect
func updatePaths()
Expand Down
2 changes: 2 additions & 0 deletions Classes/ScrollableGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,12 @@ import UIKit
}

// TODO, make the drawing layer get this from its owning plot instead of going all the away around.
/*
internal func graphPoint(forIndex index: Int) -> GraphPoint {
// TODO: For testing: Need to just return any of the graph points, because they should be the same at this point.
return plots.first?.graphPoint(forIndex: index) ?? GraphPoint()
}
*/

internal func currentViewport() -> CGRect {
return CGRect(x: 0, y: 0, width: viewportWidth, height: viewportHeight)
Expand Down
98 changes: 92 additions & 6 deletions graphview_example_code/GraphView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {

// Data for new delegate based method
lazy var blueLinePlotData: [Double] = self.generateRandomData(self.numberOfDataItems, max: 60)
lazy var orangeLinePlotData: [Double] = self.generateRandomData(self.numberOfDataItems, max: 20)
lazy var orangeLinePlotData: [Double] = self.generateRandomData(self.numberOfDataItems, max: 40)

lazy var linePlotData: [Double] = self.generateRandomData(self.numberOfDataItems, max: 50)
//lazy var barPlotData: [Double] = self.generateRandomData(self.numberOfDataItems, max: 50)
Expand Down Expand Up @@ -73,19 +73,21 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {
setupConstraints()
}

// Multi plot v1
/*
fileprivate func createMultiPlotGraph(_ frame: CGRect) -> ScrollableGraphView {
let graphView = ScrollableGraphView(frame: frame, dataSource: self)
// Setup the line plot.
let blueLinePlot = LinePlot(identifier: "multiBlue")
blueLinePlot.lineWidth = 1
blueLinePlot.lineColor = UIColor.colorFromHex(hexString: "#0000ff")
blueLinePlot.lineColor = UIColor.colorFromHex(hexString: "#16aafc")
blueLinePlot.lineStyle = ScrollableGraphViewLineStyle.smooth
blueLinePlot.shouldFill = true
blueLinePlot.fillType = ScrollableGraphViewFillType.solid
blueLinePlot.fillColor = UIColor.colorFromHex(hexString: "#0000ff").withAlphaComponent(0.5)
blueLinePlot.fillColor = UIColor.colorFromHex(hexString: "#16aafc").withAlphaComponent(0.5)
blueLinePlot.adaptAnimationType = ScrollableGraphViewAnimationType.elastic
blueLinePlot.animationDuration = 1.5
Expand All @@ -94,9 +96,9 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {
let orangeLinePlot = LinePlot(identifier: "multiOrange")
orangeLinePlot.lineWidth = 1
orangeLinePlot.lineColor = UIColor.colorFromHex(hexString: "#ff0000")
orangeLinePlot.lineColor = UIColor.colorFromHex(hexString: "#ff7d78")
orangeLinePlot.lineStyle = ScrollableGraphViewLineStyle.smooth
orangeLinePlot.fillColor = UIColor.colorFromHex(hexString: "#ff0000").withAlphaComponent(0.5)
orangeLinePlot.fillColor = UIColor.colorFromHex(hexString: "#ff7d78").withAlphaComponent(0.5)
orangeLinePlot.shouldFill = true
orangeLinePlot.fillType = ScrollableGraphViewFillType.solid
Expand All @@ -116,7 +118,85 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {
graphView.backgroundFillColor = UIColor.colorFromHex(hexString: "#333333")
graphView.dataPointSpacing = 80
graphView.dataPointLabelColor = UIColor.white.withAlphaComponent(0.5)
graphView.dataPointLabelColor = UIColor.white.withAlphaComponent(1)
graphView.shouldAnimateOnStartup = true
graphView.shouldAdaptRange = true
// graphView.adaptAnimationType = ScrollableGraphViewAnimationType.elastic // moved to plot
// graphView.animationDuration = 1.5 // moved to plot
graphView.rangeMax = 50
graphView.shouldRangeAlwaysStartAtZero = true
// Add everything to the graph.
graphView.addReferenceLines(referenceLines: referenceLines)
graphView.addPlot(plot: blueLinePlot)
graphView.addPlot(plot: orangeLinePlot)
return graphView
}
*/

// Multi plot v2
// TODO: This is obviously not great. Need to incorporate the dot drawing layer into
// the line plot as well.
fileprivate func createMultiPlotGraph(_ frame: CGRect) -> ScrollableGraphView {
let graphView = ScrollableGraphView(frame: frame, dataSource: self)

// Setup the line plot.
let blueLinePlot = LinePlot(identifier: "multiBlue")

blueLinePlot.lineWidth = 1
blueLinePlot.lineColor = UIColor.colorFromHex(hexString: "#16aafc")
blueLinePlot.lineStyle = ScrollableGraphViewLineStyle.straight

blueLinePlot.shouldFill = false

blueLinePlot.adaptAnimationType = ScrollableGraphViewAnimationType.elastic
blueLinePlot.animationDuration = 1.5

// dots on the line
let blueDotPlot = DotPlot(identifier: "multiBlueDot")
blueDotPlot.dataPointType = ScrollableGraphViewDataPointType.circle
blueDotPlot.dataPointSize = 5
blueDotPlot.dataPointFillColor = UIColor.colorFromHex(hexString: "#16aafc")

blueDotPlot.adaptAnimationType = ScrollableGraphViewAnimationType.elastic
blueDotPlot.animationDuration = 1.5

// Setup the second line plot.
let orangeLinePlot = LinePlot(identifier: "multiOrange")

orangeLinePlot.lineWidth = 1
orangeLinePlot.lineColor = UIColor.colorFromHex(hexString: "#ff7d78")
orangeLinePlot.lineStyle = ScrollableGraphViewLineStyle.straight

orangeLinePlot.shouldFill = false

orangeLinePlot.adaptAnimationType = ScrollableGraphViewAnimationType.elastic
orangeLinePlot.animationDuration = 1.5

// squares on the line
let orangeSquarePlot = DotPlot(identifier: "multiOrangeSquare")
orangeSquarePlot.dataPointType = ScrollableGraphViewDataPointType.square
orangeSquarePlot.dataPointSize = 5
orangeSquarePlot.dataPointFillColor = UIColor.colorFromHex(hexString: "#ff7d78")

orangeSquarePlot.adaptAnimationType = ScrollableGraphViewAnimationType.elastic
orangeSquarePlot.animationDuration = 1.5

// Setup the reference lines.
let referenceLines = ReferenceLines()

referenceLines.referenceLineLabelFont = UIFont.boldSystemFont(ofSize: 8)
referenceLines.referenceLineColor = UIColor.white.withAlphaComponent(0.2)
referenceLines.referenceLineLabelColor = UIColor.white
referenceLines.numberOfIntermediateReferenceLines = 5

// Setup the graph
graphView.backgroundFillColor = UIColor.colorFromHex(hexString: "#333333")

graphView.dataPointSpacing = 80
graphView.dataPointLabelColor = UIColor.white.withAlphaComponent(1)

graphView.shouldAnimateOnStartup = true
graphView.shouldAdaptRange = true
Expand All @@ -128,7 +208,9 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {
// Add everything to the graph.
graphView.addReferenceLines(referenceLines: referenceLines)
graphView.addPlot(plot: blueLinePlot)
graphView.addPlot(plot: blueDotPlot)
graphView.addPlot(plot: orangeLinePlot)
graphView.addPlot(plot: orangeSquarePlot)

return graphView
}
Expand Down Expand Up @@ -421,8 +503,12 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {

case "multiBlue":
return blueLinePlotData[pointIndex]
case "multiBlueDot":
return blueLinePlotData[pointIndex]
case "multiOrange":
return orangeLinePlotData[pointIndex]
case "multiOrangeSquare":
return orangeLinePlotData[pointIndex]
case "line":
return linePlotData[pointIndex]
case "overlaydot":
Expand Down

0 comments on commit cdd81af

Please sign in to comment.