Skip to content

Commit

Permalink
allow reference lines to be customized by NSNumberFormatterStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
timbroder committed Jul 1, 2016
1 parent 5b03c2a commit 89e6fd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Classes/ScrollableGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ import UIKit
public var referenceLineUnits: String?
/// The number of decimal places that should be shown on the reference line labels.
public var referenceLineNumberOfDecimalPlaces: Int = 0
/// The NSNumberFormatterStyle that reference lines should use to display
public var referenceLineNumberStyle: NSNumberFormatterStyle = .NoStyle

// Data Point Labels
// #################
Expand Down Expand Up @@ -434,6 +436,7 @@ import UIKit
referenceLineView?.labelFont = self.referenceLineLabelFont
referenceLineView?.labelColor = self.referenceLineLabelColor
referenceLineView?.labelDecimalPlaces = self.referenceLineNumberOfDecimalPlaces
referenceLineView?.labelNumberStyle = self.referenceLineNumberStyle

referenceLineView?.setRange(self.range)
self.addSubview(referenceLineView!)
Expand Down Expand Up @@ -1548,6 +1551,7 @@ private class ReferenceLineDrawingView : UIView {
var labelFont: UIFont = UIFont.systemFontOfSize(8)
var labelColor: UIColor = UIColor.blackColor()
var labelDecimalPlaces: Int = 2
var labelNumberStyle: NSNumberFormatterStyle = .NoStyle

// PRIVATE PROPERTIES

Expand Down Expand Up @@ -1631,8 +1635,10 @@ private class ReferenceLineDrawingView : UIView {
let minLineStart = CGPoint(x: 0, y: self.bounds.height - bottomMargin)
let minLineEnd = CGPoint(x: lineWidth, y: self.bounds.height - bottomMargin)

let maxString = String(format: "%.\(labelDecimalPlaces)f", arguments: [self.currentRange.max]) + units
let minString = String(format: "%.\(labelDecimalPlaces)f", arguments: [self.currentRange.min]) + units
let numberFormatter = referenceNumberFormatter()

let maxString = numberFormatter.stringFromNumber(self.currentRange.max)! + units
let minString = numberFormatter.stringFromNumber(self.currentRange.min)! + units

addLineWithTag(maxString, from: maxLineStart, to: maxLineEnd, inPath: referenceLinePath)
addLineWithTag(minString, from: minLineStart, to: minLineEnd, inPath: referenceLinePath)
Expand All @@ -1644,6 +1650,15 @@ private class ReferenceLineDrawingView : UIView {
return referenceLinePath
}

private func referenceNumberFormatter() -> NSNumberFormatter {
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = labelNumberStyle
numberFormatter.minimumFractionDigits = labelDecimalPlaces
numberFormatter.maximumFractionDigits = labelDecimalPlaces

return numberFormatter
}

private func createIntermediateReferenceLines(rect: CGRect, numberOfIntermediateReferenceLines: Int, forPath path: UIBezierPath) {

let height = rect.size.height
Expand Down Expand Up @@ -1692,7 +1707,8 @@ private class ReferenceLineDrawingView : UIView {
if(shouldAddLabelsToIntermediateReferenceLines) {

let value = calculateYAxisValueForPoint(lineStart)
var valueString = String(format: "%.\(labelDecimalPlaces)f", arguments: [value])
let numberFormatter = referenceNumberFormatter()
var valueString = numberFormatter.stringFromNumber(value)!

if(shouldAddUnitsToIntermediateReferenceLineLabels) {
valueString += " \(units)"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ The graph can be customised by setting any of the following public properties be
| **shouldShowReferenceLineUnits**: Bool | Whether or not to show the units on the reference lines. |
| **referenceLineUnits**: String? | The units that the y-axis is in. This string is used for labels on the reference lines. |
| **referenceLineNumberOfDecimalPlaces**: Int | The number of decimal places that should be shown on the reference line labels. |
| **referenceLineNumberStyle**: NSNumberFormatterStyle | The number style that should be shown on the reference line labels. |

### Data Point Labels (x-axis)

Expand Down

0 comments on commit 89e6fd0

Please sign in to comment.