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

Data Source

Samuel Spencer edited this page Aug 3, 2017 · 9 revisions

Similar to a UITableView, BEMSimpleLineGraph uses a data source to generate a line graph. You must setup a data source and implement the required methods to populate your graph with data. Please refer to Getting Started - Setup for instructions on setting-up your data source.

Adding Data

To populate your graph with data, you will need to implement at least two of the three available BEMSimpleLineGraphDataSource methods. The graph's data source (BEMSimpleLineGraphDataSource) is used to populate the graph with data. It works similar to some of Apple's UIKit objects. If you have ever used a UITableView or a UICollectionView, then this should be very straightforward, familiar, and flexible.

Number of Points on the Graph

Implement this method to specify the number of points on your graph. BEMSimpleLineGraph will pass the graph of interest in the graph parameter. You will need to return an NSInteger which represents the number of points in the line graph. The line graph gets the value returned by this method from its data source and caches it.

- (NSInteger)numberOfPointsInLineGraph:(BEMSimpleLineGraphView *)graph {
    return X; // Number of points in the graph.
}

Graph Values and Points

Informs the position of each point on the Y-Axis at a given index. This method is called for every point specified in the numberOfPointsInLineGraph: method. The parameter index is the position from left to right of the point on the X-Axis:

- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index {
        return …; // The value of the point on the Y-Axis for the index.
}

Reloading Data

Similar to a UITableView's reloadData method, BEMSimpleLineGraph has a reloadGraph method. Call this method to reload all the data that is used to construct the graph, including points, axis, index arrays, colors, alphas, and so on. Calling this method will cause the line graph to call layoutSubviews on itself. The line graph will also call all of its data source and delegate methods again (to get the updated data).

- (void)anyMethodInYourOwnController {
    // Change graph properties
    // Update data source / arrays

    // Reload the graph
    [self.myGraph reloadGraph];
} 

Retrieving Data

The values submitted through the data source delegate methods are saved / recorded. You can retrieve the line graph's current data points and X-Axis with the two methods below.

// Retrieve current X-Axis values
NSArray *arrayOfXLabels = [self.myGraph graphValuesForXAxis]; // The returned NSArray contains an array of NSString objects

// Retrieve the Data Points
NSArray *arrayOfDataPoints = [self.myGraph graphValuesForDataPoints]; // The returned NSArray contains an array of NSNumber objects (originally formatted as a float).