BEMSimpleLineGraph makes it easy to create and customize line graphs for iOS.
Learn more about the BEMSimpleLineGraph project requirements, licensing, and contributions.
- Requires iOS 6 or later. The sample project is optimized for iOS 7.
- Requires Automatic Reference Counting (ARC).
- Optimized for ARM64 Architecture
See the License. You are free to make changes and use this in either personal or commercial projects. Attribution is not required, but it is appreciated. A little Thanks! (or something to that affect) would be much appreciated. If you use BEMSimpleLineGraph in your app, let us know.
Any contribution is more than welcome! You can contribute through pull requests and issues on GitHub.
The iOS Sample App included with this project demonstrates how to correctly setup and use BEMSimpleLineGraph. You can refer to the sample app for an understanding of how to use and setup BEMSimpleLineGraph.
Join us on Gitter if you need any help or want to talk about the project.
All methods, properties, types, and delegate methods available on the BEMSimpleLineGraph class are documented below. If you're using Xcode 5 with BEMSimpleLineGraph, documentation is available directly within Xcode (just Option-Click any method for Quick Help).
The easiest way to install BEMSimpleLineGraph is to use CocoaPods. To do so, simply add the following line to your Podfile
:
pod BEMSimpleLineGraph
The other way to install BEMSimpleLineGraph, is to drag and drop the Classes folder into your Xcode project. When you do so, check the "Copy items into destination group's folder" box.
Setting up BEMSimpleLineGraph in your project is simple. If you're familiar with UITableView, then BEMSimpleLineGraph should be a breeze. Follow the steps below to get everything up and running.
-
Import
"BEMSimpleLineGraphView.h"
to the header of your view controller:#import "BEMSimpleLineGraphView.h"
-
Implement the
BEMSimpleLineGraphDelegate
to the same view controller:@interface YourViewController : UIViewController <BEMSimpleLineGraphDelegate>
-
BEMSimpleLineGraphView can be initialized in one of two ways. You can either add it directly to your interface (storyboard file) OR through code. Both ways provide the same initialization, just different ways to do the same thing. Use the method that makes sense for your app or project.
Interface Initialization
1 - Add a UIView to your UIViewController
2 - Change the class type of the UIView toBEMSimpleLineGraphView
3 - Link the view to your code using anIBOutlet
. You can set the property toweak
andnonatomic
.
4 - Select the Connect theBEMSimpleLineGraphView
in your interface. Connect the delegate property to your ViewController.Code Initialization
Just add the following code to your implementation (usually theviewDidLoad
method).BEMSimpleLineGraphView *myGraph = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)]; myGraph.delegate = self; [self.view addSubview:myGraph];
-
Implement the two required methods:
numberOfPointsInLineGraph:
andlineGraph:valueForPointAtIndex:
. See documentation below for more information
Number of Points in Graph
Returns 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.
}
Value for Point at Index
Informs the position of each point on the Y-Axis at a given index. This method is called for every point specifed 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.
}
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];
}
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).
In addition to recording and displaying data, BEMSimpleLineGraph can also perform advanced calculations with your data. All calculation methods are available publically and begin with calculate
. BEMSimpleLineGraph can calculate Standard Deviation, Average, Median, Mode, Minimum, and Maximum values.
When the line graph starts or completes loading it will call a delegate method. When it begins reloading the lineGraphDidBeginLoading:
method is called. When loading is finished, the lineGraphDidFinishLoading:
method is called.
BEMSimpleLineGraph makes it possible to react to the user touching the graph.
When the user touches and moves his finger along the graph, the labels on top of the graph indicate the value of the closest point.
To do so, first toggle the enableTouchReport
property:
self.myGraph.enableTouchReport = YES;
Next, implement the two following methods: lineGraph:didTouchGraphWithClosestIndex
and lineGraph:didReleaseTouchFromGraphWithClosestIndex:
.
Did Touch Graph at Index
This method gets called when the user touches the graph. The parameter index
is the closest index (X-Axis) from the user's finger position.
- (void)lineGraph:(BEMSimpleLineGraphView *)graph didTouchGraphWithClosestIndex:(NSInteger)index {
// Here you could change the text of a UILabel with the value of the closest index for example.
}
Did Release Touch at Graph Index
This method gets called when the user stops touching the graph. The parameter index
is the closest index (X-Axis) from the user's last finger position.
- (void)lineGraph:(BEMSimpleLineGraphView *)graph didReleaseTouchFromGraphWithClosestIndex:(CGFloat)index {
// Set the UIlabel alpha to 0 for example.
}
BEMSimpleLineGraph makes it possible to add labels along the X-Axis. To do so, simply implement the two followings methods: numberOfGapsBetweenLabelsOnLineGraph:
and lineGraph:labelOnXAxisForIndex:
.
Gaps between labels
Informs how much empty space is needed between each displayed label. Returning 0 will display all of the labels. 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, on the middle it returns 1 and on the right it returns the number of points in the graph.
Label on X Axis
The text to be displayed for each UILabel on the X-Axis at a given index. It should return as many strings as the number of points on the graph.
- (NSString *)lineGraph:(BEMSimpleLineGraphView *)graph labelOnXAxisForIndex:(NSInteger)index {
return X;
}
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;
BEMSimpleLineGraph can be drawn with curved lines instead of directly connecting the dots with straight lines.
To do so, set the property enableBezierCurve
to YES.
self.myGraph.enableBezierCurve = YES;
Two delegate methods on the BEMSimpleLineGraphDelegate
let you customize the color and alpha of a specifc line in the graph.
Line Color Specify the color of the line at a specific index (the graph is made of multiple lines that appear as one).
- (UIColor *)lineGraph:(BEMSimpleLineGraphView *)graph lineColorForIndex:(NSInteger)index {
return [UIColor color];
}
Line Alpha Specify the alpha value of the line at a specific index (the graph is made of multiple lines that appear as one).
- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph lineAlphaForIndex:(NSInteger)index {
return 1.0;
}
BEMSimpleLineGraphs can be customized by using various properties. A variety of properties let you control the animation, colors, and alpha of the graph.
Entrance Animation
The animationGraphEntranceSpeed
property controls the speed of the entrance animation. It is an NSInteger who's value should be between 0 and 10.
A value of 0 will disable the animation. A value of 1 will make the animation very slow. A value of 10 or more will make it fast.
Custom Colors and Alpha
BEMSimpleLineGraphs are split into three parts - the top, the bottom, and the line. You can set the alpha and color of each of these parts separately.
- Top Section. The
colorTop
andalphaTop
properties control the color (UIColor) and alpha (float) of the top part of the graph. - Bottom Section. The
colorBottom
andalphaBottom
properties control the color (UIColor) and alpha (float) of the bottom part of the graph. - Line. The
colorLine
andalphaLine
properties control the color (UIColor) and alpha (float) of the line of the graph. ThewidthLine
property controls the width of the line of graph (float that defaults to 1.0).
On iOS 7.0 and above you can take a snapshot of the line graph view and get a UIImage representation of the snapshot. To do so, simply call the method below at anytime in the graph's lifecycle. Note that the snapshot is not of the completed graph, but of the graph in its current state (whether it is in mid-animation or not). You can use the lineGraphDidFinishLoading:
delegate method (coming soon) to find out when the graph has finished rendering and animating.
// Method Definition
- (UIImage *)graphSnapshotImage;
// Method Usage
UIImage *imageOfGraph = [self.myGraph graphSnapshotImage];