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

Axis Labels

Boris-Em edited this page Aug 14, 2014 · 19 revisions

BEMSimpleLineGraph offers easy ways to include labels along the graph's axis.

#X-Axis To add labels along the X-Axis, simply implement the following 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. ```Objective-C - (NSString *)lineGraph:(BEMSimpleLineGraphView *)graph labelOnXAxisForIndex:(NSInteger)index { return …; } ``` ###Gaps Between Labels When only implementing `lineGraph:labelOnXAxisForIndex:`, the labels might overlap. Thankfully, BEMSimpleLineGraph offers different ways to prevent this from happening: * Using a modulo operation Click [here](http://en.wikipedia.org/wiki/Modulo_operation) 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: ```Objective-C - (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.

  • Using the property removeOverlappingLabelsOnAxis
    If this property is set to YES, the overlapping labels on the axis will automatically be removed. It can be used as a "safety net" in combination of one of the previous way to skip labels.
    Its default value is NO.
self.myGraph.removeOverlappingLabelsOnAxis = YES;

###X-Axis Label Color The property colorXaxisLabel controls the color of the text of the UILabels on the X-Axis. It's default value is blackColor.

@property (strong, nonatomic) UIColor *colorXaxisLabel;
#Y-Axis To add labels along the Y-Axis, simply set the BOOL property `enableYAxisLabel` to YES. ```Objective-C self.myGraph.enableYAxisLabel = YES ``` By default, three labels will be displayed: the maximum value, the minimum value and the average value.

###Displaying More Labels On the Y-Axis The method numberOfYAxisLabelsOnLineGraph:is here to display more labels on the Y-Axis. It is only called if the property autoScaleYAxis is set to NO. It places the labels evenly along the Y-Axis.

- (NSInteger)numberOfYAxisLabelsOnLineGraph:(BEMSimpleLineGraphView *)graph {
    return 3;
}

###Y-Axis Label Color The property colorYaxisLabel controls the color of the text of the UILabels on the Y-Axis. It's default value is blackColor.

@property (strong, nonatomic) UIColor *colorXaxisLabel;