Skip to content

Commit 73cc103

Browse files
committed
Add xLabelsSkipLast option
Closes #37
1 parent f4f429a commit 73cc103

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ Some tips for debugging an hidden chart:
206206
* `xLabelsFormatter` – formats the labels on the x-axis.
207207
* `xLabelsOrientation` – sets the x-axis labels orientation to vertical or horizontal.
208208
* `xLabelsTextAlignment` – text-alignment for the x-labels.
209+
* `xLabelsSkipLast` (default `true`) - Skip the last x-label. Setting this to `false` will make the label overflow the frame width, so use carefully!
209210
* `yLabelsFormatter` – formats the labels on the y-axis.
210211
* `yLabelsOnRightSide` – place the y-labels on the right side.
211212

Source/Chart.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ open class Chart: UIControl {
9090
*/
9191
open var xLabelsOrientation: ChartLabelOrientation = .horizontal
9292

93+
/**
94+
Skip the last x-label. Setting this to false may make the label overflow the frame width.
95+
*/
96+
open var xLabelsSkipLast: Bool = true
97+
9398
/**
9499
Values to display as labels of the y-axis. If not specified, will display the
95100
lowest, the middle and the highest values.
@@ -561,9 +566,9 @@ open class Chart: UIControl {
561566

562567
let scaled = scaleValuesOnXAxis(labels)
563568
let padding: CGFloat = 5
564-
565569
scaled.enumerated().forEach { (i, value) in
566570
let x = CGFloat(value)
571+
let isLastLabel = x == drawingWidth
567572

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

@@ -573,7 +578,7 @@ open class Chart: UIControl {
573578
context.strokePath()
574579
}
575580

576-
if x == drawingWidth {
581+
if xLabelsSkipLast && isLastLabel {
577582
// Do not add label at the most right position
578583
return
579584
}
@@ -590,8 +595,8 @@ open class Chart: UIControl {
590595
label.frame.origin.y += topInset
591596
if xLabelsOrientation == .horizontal {
592597
// Add left padding
593-
label.frame.origin.x += padding
594598
label.frame.origin.y -= (label.frame.height - bottomInset) / 2
599+
label.frame.origin.x += padding
595600

596601
// Set label's text alignment
597602
label.frame.size.width = (drawingWidth / CGFloat(labels.count)) - padding * 2
@@ -609,12 +614,11 @@ open class Chart: UIControl {
609614
label.frame.origin.x += ((drawingWidth / CGFloat(labels.count)) / 2) - (label.frame.size.width / 2)
610615
} else {
611616
// Give some space from the vertical line
612-
label.frame.origin.x += 4
617+
label.frame.origin.x += padding
613618
}
614619
}
615620

616621
self.addSubview(label)
617-
618622
}
619623

620624
}

0 commit comments

Comments
 (0)