Skip to content

Commit

Permalink
Pull out bar plot into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
philackm committed Jun 30, 2017
1 parent ee73739 commit 4ce9648
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
40 changes: 40 additions & 0 deletions Classes/Plots/Plot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,50 @@ open class DataPointPlot : Plot {
}

open class BarPlot : Plot {

// Customisation
// #############

/// Whether bars should be drawn or not. If you want a bar graph, this should be set to true.
@IBInspectable open var shouldDrawBarLayer: Bool = false
/// The width of an individual bar on the graph.
@IBInspectable open var barWidth: CGFloat = 25;
/// The actual colour of the bar.
@IBInspectable open var barColor: UIColor = UIColor.gray
/// The width of the outline of the bar
@IBInspectable open var barLineWidth: CGFloat = 1
/// The colour of the bar outline
@IBInspectable open var barLineColor: UIColor = UIColor.darkGray
/// Whether the bars should be drawn with rounded corners
@IBInspectable open var shouldRoundBarCorners: Bool = false

// Private State
// #############

private var barLayer: BarDrawingLayer?

override init() {
super.init()
self.identifier = "BarPlot"
}

func layers(forViewport viewport: CGRect) -> [ScrollableGraphViewDrawingLayer?] {
createLayers(viewport: viewport)
return [barLayer]
}

private func createLayers(viewport: CGRect) {
// The bar layer
if (shouldDrawBarLayer) {
// Bar Layer
barLayer = BarDrawingLayer(frame: viewport,
barWidth: barWidth,
barColor: barColor,
barLineWidth: barLineWidth,
barLineColor: barLineColor,
shouldRoundCorners: shouldRoundBarCorners)
}
}
}


Expand Down
7 changes: 5 additions & 2 deletions Classes/ScrollableGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import UIKit
// Bar styles
// ##########

/*
/// Whether bars should be drawn or not. If you want a bar graph, this should be set to true.
@IBInspectable open var shouldDrawBarLayer: Bool = false
/// The width of an individual bar on the graph.
Expand All @@ -88,7 +89,7 @@ import UIKit
@IBInspectable open var barLineColor: UIColor = UIColor.darkGray
/// Whether the bars should be drawn with rounded corners
@IBInspectable open var shouldRoundBarCorners: Bool = false

*/

// Data Point Drawing
// ##################
Expand Down Expand Up @@ -439,7 +440,7 @@ import UIKit
case let plot as DataPointPlot:
addSubLayers(layers: plot.layers(forViewport: viewport))
case let plot as BarPlot:
print(plot.identifier)
addSubLayers(layers: plot.layers(forViewport: viewport))
default:
print("not a concrete plot")
}
Expand Down Expand Up @@ -498,6 +499,7 @@ import UIKit
}
*/

/*
// The bar layer
if (shouldDrawBarLayer) {
// Bar Layer
Expand All @@ -510,6 +512,7 @@ import UIKit
barLayer?.graphViewDrawingDelegate = self
drawingView.layer.insertSublayer (barLayer!, below: lineLayer)
}
*/
}

public func addReferenceLines(referenceLines: ReferenceLines) {
Expand Down
38 changes: 14 additions & 24 deletions graphview_example_code/GraphView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,28 @@ class ViewController: UIViewController {
}

private func createBarGraph(_ frame: CGRect) -> ScrollableGraphView {
let graphView = ScrollableGraphView(frame:frame)


let graphView = ScrollableGraphView(frame:frame)

// Setup the plot
let barPlot = BarPlot()

barPlot.shouldDrawBarLayer = true
barPlot.barWidth = 25
barPlot.barLineWidth = 1
barPlot.barLineColor = UIColor.colorFromHex(hexString: "#777777")
barPlot.barColor = UIColor.colorFromHex(hexString: "#555555")

// 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


// Add everything


let referenceLines = ReferenceLines()


graphView.shouldDrawBarLayer = true

//graphView.lineColor = UIColor.clear
graphView.barWidth = 25
graphView.barLineWidth = 1
graphView.barLineColor = UIColor.colorFromHex(hexString: "#777777")
graphView.barColor = UIColor.colorFromHex(hexString: "#555555")
graphView.backgroundFillColor = UIColor.colorFromHex(hexString: "#333333")

graphView.dataPointLabelColor = UIColor.white.withAlphaComponent(0.5)

graphView.shouldAnimateOnStartup = true
Expand All @@ -148,12 +142,8 @@ class ViewController: UIViewController {
graphView.rangeMax = 50
graphView.shouldRangeAlwaysStartAtZero = true


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

// Add everything
graphView.addPlot(plot: barPlot)
graphView.addReferenceLines(referenceLines: referenceLines)
return graphView
}
Expand Down

0 comments on commit 4ce9648

Please sign in to comment.