-
Notifications
You must be signed in to change notification settings - Fork 382
Data Source
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.PRE-RELEASE DOCUMENTATION
The master branch of the project doesn't use forBEMSimpleLineGraphDataSource
currently. This feature is supported on the feature branch. Please make sure to read the feature branch warning before working with it. On the master branch, all of the referenced methods are available onBEMSimpleLineGraphDelegate
.
###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). ```Objective-C - (void)anyMethodInYourOwnController { // Change graph properties // Update data source / arrays
// Reload the graph
[self.myGraph reloadGraph];
}
***
<a name='retrieving-data'/>
##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.
```Objective-C
// 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).