A reusable graphing view for iOS to easily plot continuous functions.
Use CocoaPods!
Add pod 'DPGraphView'
to your Podfile
Note that DPGraphView
requires iOS 6.0 or later (not fully tested 😟)
Otherwise, feel free to copy-paste the source into your project.
- Set the
dataSource
property of aDPGraphView
to be the data source for the plotting (probablyself
). - Implement the required methods in the
DPGraphViewDataSource
protocol:-(CGFloat)graphView:(DPGraphView *)graphView yValueForXValue:(CGFloat)x onPlotWithIndex:(NSUInteger)plotIndex
and-(NSUInteger)numberOfPlotsInGraphView:(DPGraphView *)graphView
- Optionally implement
-(UIColor *)graphView:(DPGraphView *)graphView colorForPlotIndex:(NSUInteger)plotIndex
to color specific plots. If you don't implement this, all plots will simply be black. - Optionally implement
-(UIColor *)graphViewColorForGrid:(DPGraphView *)graphView
to color the gridlines. If you don't implement this, the gridlines will simply be black. - Optionally implement
-(UIColor *)graphViewColorFor[X][Y]Axis:(DPGraphView *)graphView
to color the X and Y axes. If you don't implement this, the axes will simply be black. - Set
minX
,maxX
,minY
, andmaxY
properties of theDPGraphView
. - Optionally set
displayGridlines
,dashGridlines
, anddisplayAxes
properties of theDPGraphView
to enable and configure the display of gridline and axes. If you don't set these properties, no axes or gridlines will be rendered. - Add the
DPGraphView
, or do whatever you want with it.
For this example code, I have my DPGraphView
created in a Storyboard and connected to my Controller via an outlet. I simply override the setter to setup the properties of the DPGraphView
, and then implement the methods in the protocol.
#import "ViewController.h"
#import "DPGraphView.h"
@interface ViewController () <DPGraphViewDataSource>
@property (nonatomic, weak) IBOutlet DPGraphView *graphView;
@end
@implementation ViewController
- (void) setGraphView:(DPGraphView *)graphView
{
_graphView = graphView;
_graphView.dataSource = self;
_graphView.minX = -3;
_graphView.maxX = 3;
_graphView.minY = -4;
_graphView.maxY = 4;
_graphView.displayGridlines = YES;
_graphView.displayAxes = YES;
_graphView.dashGridlines = YES;
}
- (NSUInteger) numberOfPlotsInGraphView:(DPGraphView *)graphView
{
return 3;
}
- (CGFloat) graphView:(DPGraphView *)graphView yValueForXValue:(CGFloat)x onPlotWithIndex:(NSUInteger)plotIndex
{
switch (plotIndex) {
case 0:
return powf(x, 2);
case 1:
return expf(-x*x)*4;
case 2:
return powf(x, 3);
default:
return 0;
}
}
- (UIColor *) graphView:(DPGraphView *)graphView colorForPlotIndex:(NSUInteger)plotIndex
{
switch (plotIndex) {
case 0:
return [UIColor redColor];
case 1:
return [UIColor blueColor];
case 2:
return [UIColor yellowColor];
default:
return [UIColor blackColor];
}
}
- (UIColor *) graphViewColorForGrid:(DPGraphView *)graphView
{
return [UIColor colorWithRed:0.000 green:0.502 blue:0.251 alpha:1.000];
}
- (UIColor *) graphViewColorForXAxis:(DPGraphView *)graphView
{
return [UIColor greenColor];
}
- (UIColor *) graphViewColorForYAxis:(DPGraphView *)graphView
{
return [UIColor greenColor];
}
@end
And the result of the above code should look something like:
DPGraphView is available under the MIT license. See the LICENSE file for more info.