Skip to content

Commit

Permalink
Add xLabelsSkipLast option
Browse files Browse the repository at this point in the history
Closes #37
  • Loading branch information
gpbl committed May 20, 2017
1 parent f4f429a commit 73cc103
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Some tips for debugging an hidden chart:
* `xLabelsFormatter` – formats the labels on the x-axis.
* `xLabelsOrientation` – sets the x-axis labels orientation to vertical or horizontal.
* `xLabelsTextAlignment` – text-alignment for the x-labels.
* `xLabelsSkipLast` (default `true`) - Skip the last x-label. Setting this to `false` will make the label overflow the frame width, so use carefully!
* `yLabelsFormatter` – formats the labels on the y-axis.
* `yLabelsOnRightSide` – place the y-labels on the right side.

Expand Down
14 changes: 9 additions & 5 deletions Source/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ open class Chart: UIControl {
*/
open var xLabelsOrientation: ChartLabelOrientation = .horizontal

/**
Skip the last x-label. Setting this to false may make the label overflow the frame width.
*/
open var xLabelsSkipLast: Bool = true

/**
Values to display as labels of the y-axis. If not specified, will display the
lowest, the middle and the highest values.
Expand Down Expand Up @@ -561,9 +566,9 @@ open class Chart: UIControl {

let scaled = scaleValuesOnXAxis(labels)
let padding: CGFloat = 5

scaled.enumerated().forEach { (i, value) in
let x = CGFloat(value)
let isLastLabel = x == drawingWidth

// Add vertical grid for each label, except axes on the left and right

Expand All @@ -573,7 +578,7 @@ open class Chart: UIControl {
context.strokePath()
}

if x == drawingWidth {
if xLabelsSkipLast && isLastLabel {
// Do not add label at the most right position
return
}
Expand All @@ -590,8 +595,8 @@ open class Chart: UIControl {
label.frame.origin.y += topInset
if xLabelsOrientation == .horizontal {
// Add left padding
label.frame.origin.x += padding
label.frame.origin.y -= (label.frame.height - bottomInset) / 2
label.frame.origin.x += padding

// Set label's text alignment
label.frame.size.width = (drawingWidth / CGFloat(labels.count)) - padding * 2
Expand All @@ -609,12 +614,11 @@ open class Chart: UIControl {
label.frame.origin.x += ((drawingWidth / CGFloat(labels.count)) / 2) - (label.frame.size.width / 2)
} else {
// Give some space from the vertical line
label.frame.origin.x += 4
label.frame.origin.x += padding
}
}

self.addSubview(label)

}

}
Expand Down

0 comments on commit 73cc103

Please sign in to comment.