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 Jun 30, 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 way to prevent this: * 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:

@property (strong, nonatomic) UIColor *colorXaxisLabel;
#Y-Axis The [master branch](https://github.com/Boris-Em/BEMSimpleLineGraph) of the project doesn't support labels on the Y-axis for now. It is expected to be supported with the release of the v3.0. However, this feature is supported on the [feature branch](https://github.com/Boris-Em/BEMSimpleLineGraph/tree/feature). Please make sure to read the [feature branch warning](https://github.com/Boris-Em/BEMSimpleLineGraph/tree/feature#feature-branch-warning) before working with it.