Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot labels #143

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Classes/Plots/Plot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ open class Plot {
/// If adaptAnimationType is set to .Custom, then this is the easing function you would like applied for the animation.
open var customAnimationEasingFunction: ((_ t: Double) -> Double)?

// Labels
// #####################

/// The font to be used for the value label.
open var labelFont = UIFont.systemFont(ofSize: 8)
/// The colour of the value label font.
open var labelColor: UIColor = UIColor.black
/// How far to offset the vertical position of the label.
open var labelVerticalOffset: CGFloat = 0

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

Expand Down
7 changes: 7 additions & 0 deletions Classes/Protocols/ScrollableGraphViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ public protocol ScrollableGraphViewDataSource : class {
func value(forPlot plot: Plot, atIndex pointIndex: Int) -> Double
func label(atIndex pointIndex: Int) -> String
func numberOfPoints() -> Int // This now forces the same number of points in each plot.
func plotLabel(shouldShowPlotLabel plot: Plot, atIndex pointIndex: Int) -> Bool
func plotLabel(forPlot plot: Plot, atIndex pointIndex: Int) -> String?
}

extension ScrollableGraphViewDataSource {
func plotLabel(shouldShowPlotLabel plot: Plot, atIndex pointIndex: Int) -> Bool { return false }
func plotLabel(forPlot plot: Plot, atIndex pointIndex: Int) -> String? { return nil }
}
40 changes: 40 additions & 0 deletions Classes/ScrollableGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import UIKit
// Labels
private var labelsView = UIView()
private var labelPool = LabelPool()
private var plotLabelPool = [LabelPool]()

// Data Source
weak open var dataSource: ScrollableGraphViewDataSource? {
Expand Down Expand Up @@ -478,6 +479,7 @@ import UIKit
private func addPlotToGraph(plot: Plot, activePointsInterval: CountableRange<Int>) {
plot.graphViewDrawingDelegate = self
self.plots.append(plot)
self.plotLabelPool.append(LabelPool())
initPlot(plot: plot, activePointsInterval: activePointsInterval)
startAnimations(withStaggerValue: 0.15)
}
Expand Down Expand Up @@ -708,6 +710,7 @@ import UIKit
let deactivatedLabelPoints = filterPointsForLabels(fromPoints: deactivatedPoints)
let activatedLabelPoints = filterPointsForLabels(fromPoints: activatedPoints)
updateLabels(deactivatedPoints: deactivatedLabelPoints, activatedPoints: activatedLabelPoints)
updatePlotLabels(deactivatedPoints: deactivatedLabelPoints, activatedPoints: activatedLabelPoints)
}
}
}
Expand Down Expand Up @@ -833,6 +836,42 @@ import UIKit
}
}

private func updatePlotLabels(deactivatedPoints: [Int], activatedPoints: [Int]) {
guard let dataSource = self.dataSource else {
return
}

for (index, plot) in plots.enumerated() {
let labelPool = plotLabelPool[index]

// Disable any labels for the deactivated points.
for point in deactivatedPoints {
labelPool.deactivateLabel(forPointIndex: point)
}

// Grab an unused label and update it to the right position for the newly activated poitns
for point in activatedPoints {
guard let plotLabelText = dataSource.plotLabel(forPlot: plot, atIndex: point) else {
continue
}
let label = labelPool.activateLabel(forPointIndex: point)
label.text = plotLabelText
label.isHidden = !dataSource.plotLabel(shouldShowPlotLabel: plot, atIndex: point)
label.textColor = plot.labelColor
label.font = plot.labelFont

label.sizeToFit()

let position = calculatePosition(atIndex: point, value: dataSource.value(forPlot: plot, atIndex: point))
label.frame = CGRect(origin: CGPoint(x: position.x - label.frame.width / 2, y: position.y - label.frame.height + plot.labelVerticalOffset), size: label.frame.size)

_ = labelsView.subviews.filter { $0.frame.origin == label.frame.origin }.map { $0.removeFromSuperview() }

labelsView.addSubview(label)
}
}
}

private func updateLabelsForCurrentInterval() {
// Have to ensure that the labels are added if we are supposed to be showing them.
if let ref = self.referenceLines {
Expand All @@ -845,6 +884,7 @@ import UIKit

let filteredPoints = filterPointsForLabels(fromPoints: activatedPoints)
updateLabels(deactivatedPoints: filteredPoints, activatedPoints: filteredPoints)
updatePlotLabels(deactivatedPoints: filteredPoints, activatedPoints: filteredPoints)
}
}
}
Expand Down
31 changes: 22 additions & 9 deletions graphview_example_code/GraphView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {
// off many graphs with different plots, we are using one big switch
// statement.
func value(forPlot plot: Plot, atIndex pointIndex: Int) -> Double {
return dataValue(forPlot: plot, atIndex: pointIndex)
}

func label(atIndex pointIndex: Int) -> String {
// Ensure that you have a label to return for the index
return xAxisLabels[pointIndex]
}

// Uncomment the below to enable plot labels
// func plotLabel(forPlot plot: Plot, atIndex pointIndex: Int) -> String? {
// return "\(dataValue(forPlot: plot, atIndex: pointIndex))"
// }
//
// func plotLabel(shouldShowPlotLabel plot: Plot, atIndex pointIndex: Int) -> Bool {
// return true
// }

func numberOfPoints() -> Int {
return numberOfDataItems
}

func dataValue(forPlot plot: Plot, atIndex pointIndex: Int) -> Double {

switch(plot.identifier) {

Expand Down Expand Up @@ -89,15 +111,6 @@ class ViewController: UIViewController, ScrollableGraphViewDataSource {
}
}

func label(atIndex pointIndex: Int) -> String {
// Ensure that you have a label to return for the index
return xAxisLabels[pointIndex]
}

func numberOfPoints() -> Int {
return numberOfDataItems
}

// Creating Different Kinds of Graphs
// ##################################

Expand Down