-
Notifications
You must be signed in to change notification settings - Fork 382
Axis Labels
BEMSimpleLineGraph offers easy ways to include labels along the graph's axis.
To add labels along the X-Axis, simply implement the following delegate method:
lineGraph:labelOnXAxisForIndex:
.
It should return the text to be displayed for each UILabel on the X-Axis at a given index. The number of points on the graph and the number of strings returned should be the same.
- (NSString *)lineGraph:(BEMSimpleLineGraphView *)graph labelOnXAxisForIndex:(NSInteger)index {
return …;
}
When only implementing lineGraph:labelOnXAxisForIndex:
, too many labels might be displayed. Thankfully, BEMSimpleLineGraph offers different ways to control how many labels will be displayed:
-
Using a modulo operation
Click here to learn more about modulo operations.
This is probably the easiest way to skip some labels in order to avoid overlaps.
In the following example, only every other label will be displayed:
- (NSString *)lineGraph:(BEMSimpleLineGraphView *)graph labelOnXAxisForIndex:(NSInteger)index {
if ((index % 2) == 1) return [self.ArrayOfDates objectAtIndex:index];
else return @"";
}
-
Using the method
numberOfGapsBetweenLabelsOnLineGraph:
numberOfGapsBetweenLabelsOnLineGraph:
informs how much empty space is needed between each displayed label. Returning 0 will display all of the labels (just like if this method wasn't implemented). Returning the total number of labels will only display the first and last label. See the image below for clarification.
- (NSInteger)numberOfGapsBetweenLabelsOnLineGraph:(BEMSimpleLineGraphView *)graph {
return X; // The number of hidden labels between each displayed label.
}
On the left, numberOfGapsBetweenLabelsOnLineGraph: returns 0, in the middle it returns 1 and on the right it returns the number of points in the graph.
Note
The overlapping labels on the X-Axis will automatically be removed if they overlap.
The property enableXAxisLabel
controls if the labels on the X-Axis should be displayed or not. Its default value is YES
.
@property (nonatomic) BOOL enableXAxisLabel;
The property colorXaxisLabel
controls the color of the text of the UILabels on the X-Axis. Its default value is blackColor
.
@property (strong, nonatomic) UIColor *colorXaxisLabel;
The properties colorBackgroundXaxis
& alphaBackgroundXaxis
control the color and alpha of the X-Axis background.
@property (strong, nonatomic) UIColor *colorBackgroundXaxis;
@property (strong, nonatomic) UIColor *alphaBackgroundXaxis;
To add labels along the Y-Axis, simply set the BOOL property enableYAxisLabel
to YES.
self.myGraph.enableYAxisLabel = YES
By default, three labels will be displayed: the maximum value, the minimum value and the average value.
The optional method numberOfYAxisLabelsOnLineGraph:
is here to display more labels on the Y-Axis. It places the labels evenly along the Y-Axis. If this method is not implemented, 3 labels will be displayed on the Y-Axis.
- (NSInteger)numberOfYAxisLabelsOnLineGraph:(BEMSimpleLineGraphView *)graph {
return 3;
}
The property colorYaxisLabel
controls the color of the text of the UILabels on the Y-Axis. Its default value is blackColor
.
@property (strong, nonatomic) UIColor *colorXaxisLabel;
The properties colorBackgroundYaxis
& alphaBackgroundYaxis
control the color and alpha of the Y-Axis background.
@property (strong, nonatomic) UIColor *colorBackgroundYaxis;
@property (strong, nonatomic) UIColor *alphaBackgroundYaxis;