-
Notifications
You must be signed in to change notification settings - Fork 382
Getting Started
This page provides all the necessaries to implement BEMSimpleLineGraph to your project, and to draw your first graph!
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
and theBEMSimpleLineGraphDataSource
to the same view controller:@interface YourViewController : UIViewController <BEMSimpleLineGraphDelegate, BEMSimpleLineGraphDataSource>
-
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 and dataSource properties to your ViewController.
5 - Select theBEMSimpleLineGraphView
and open the Attributes Inspector. Most of the line graph's customizable properties can easily be set from the Attributes Inspector. The Sample App demonstrates this capability. Note that graph data will not be loaded in Interface Builder.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; myGraph.dataSource = self; [self.view addSubview:myGraph];
-
Implement the two required methods:
numberOfPointsInLineGraph:
andlineGraph:valueForPointAtIndex:
:
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.
}