Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Commit

Permalink
Optimized the way the labels are displayed on the X-Axis
Browse files Browse the repository at this point in the history
The labels on the X-Axis are now offset to be centered if necessary and
possible.
  • Loading branch information
Boris-Em committed Feb 8, 2014
1 parent 86e0963 commit 07763d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Classes/BEMSimpleLineGraphView.m
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ - (void)drawXAxis {
lastLabel.backgroundColor = [UIColor clearColor];
[self addSubview:lastLabel];
} else {
NSInteger offset = [self offsetForXAxisWithNumberOfGaps:numberOfGaps]; // The offset (if possible and necessary) used to shift the Labels on the X-Axis for them to be centered.
for (int i = 1; i <= (numberOfPoints/numberOfGaps); i++) {
UILabel *labelXAxis = [[UILabel alloc] init];
labelXAxis.text = [self.delegate labelOnXAxisForIndex:(i * numberOfGaps - 1)];
labelXAxis.text = [self.delegate labelOnXAxisForIndex:(i * numberOfGaps - 1 - offset)];
[labelXAxis sizeToFit];
[labelXAxis setCenter:CGPointMake((self.viewForBaselineLayout.frame.size.width/(numberOfPoints-1))*(i*numberOfGaps - 1), self.frame.size.height - labelXaxisOffset)];
[labelXAxis setCenter:CGPointMake((self.viewForBaselineLayout.frame.size.width/(numberOfPoints-1))*(i*numberOfGaps - 1 - offset), self.frame.size.height - labelXaxisOffset)];
labelXAxis.font = self.labelFont;
labelXAxis.textAlignment = 1;
labelXAxis.textColor = self.colorXaxisLabel;
Expand All @@ -300,4 +301,21 @@ - (void)drawXAxis {
}
}

- (NSInteger)offsetForXAxisWithNumberOfGaps:(NSInteger)numberOfGaps
{
// Calculates the optimum offset needed for the Labels to be centered on the X-Axis.
NSInteger leftGap = numberOfGaps - 1;
NSInteger rightGap = numberOfPoints - (numberOfGaps*(numberOfPoints/numberOfGaps));
NSInteger offset = 0;

if (leftGap != rightGap) {
for (int i = 0; i <= numberOfGaps; i++) {
if (leftGap - i == rightGap + i) {
offset = i;
}
}
}
return offset;
}

@end
Binary file not shown.

0 comments on commit 07763d5

Please sign in to comment.