Skip to content

Commit 95c63bd

Browse files
authored
Merge pull request philackm#23 from graciborski/10_number_of_labels_in_x_axis
Add dataPointLabelsSparsity property to enable showing every n-th label
2 parents d184255 + 0f946f9 commit 95c63bd

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

Classes/ScrollableGraphView.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ import UIKit
8787
public var dataPointLabelBottomMargin: CGFloat = 0
8888
public var dataPointLabelColor = UIColor.blackColor()
8989
public var dataPointLabelFont: UIFont? = UIFont.systemFontOfSize(10)
90-
90+
public var dataPointLabelsSparsity: Int = 1 // Used to force the graph to show every n-th dataPoint label (by default displaying every label)
91+
9192
// MARK: - Private State
9293
// #####################
9394

@@ -760,15 +761,17 @@ import UIKit
760761
// If the active points (the points we can actually see) change, then we need to update the path.
761762
private func activePointsDidChange() {
762763

763-
let deactivatedPoints = determineDeactivatedPoints()
764-
let activatedPoints = determineActivatedPoints()
765-
766-
updatePaths()
767-
if(shouldShowLabels) {
768-
updateLabels(deactivatedPoints, activatedPoints)
769-
}
770-
}
771-
764+
let deactivatedPoints = determineDeactivatedPoints()
765+
let activatedPoints = determineActivatedPoints()
766+
767+
updatePaths()
768+
if(shouldShowLabels) {
769+
let deactivatedLabelPoints = filterPointsForLabels(fromPoints: deactivatedPoints)
770+
let activatedLabelPoints = filterPointsForLabels(fromPoints: activatedPoints)
771+
updateLabels(deactivatedLabelPoints, activatedLabelPoints)
772+
}
773+
}
774+
772775
private func rangeDidChange() {
773776

774777
// If shouldAnimateOnAdapt is enabled it will kickoff any animations that need to occur.
@@ -882,7 +885,15 @@ import UIKit
882885
}
883886
return set
884887
}
888+
889+
private func filterPointsForLabels(fromPoints points:[Int]) -> [Int] {
885890

891+
if(self.dataPointLabelsSparsity == 1) {
892+
return points
893+
}
894+
return points.filter({ $0 % self.dataPointLabelsSparsity == 0 })
895+
}
896+
886897
private func startAnimations(withStaggerValue stagger: Double = 0) {
887898

888899
var pointsToAnimate = 0 ..< 0

graphview_example/GraphView/ViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ class ViewController: UIViewController {
165165
graphView.fillColor = UIColor.colorFromHex("#FF0080")
166166

167167
graphView.shouldDrawDataPoint = false
168-
graphView.dataPointSpacing = 80
168+
graphView.dataPointSpacing = 20
169169
graphView.dataPointLabelFont = UIFont.boldSystemFontOfSize(10)
170170
graphView.dataPointLabelColor = UIColor.whiteColor()
171+
172+
graphView.dataPointLabelsSparsity = 3
171173

172174
graphView.referenceLineThickness = 1
173175
graphView.referenceLineLabelFont = UIFont.boldSystemFontOfSize(10)

0 commit comments

Comments
 (0)