Skip to content

Instantly share code, notes, and snippets.

@todd1251
Created August 21, 2011 22:21
Show Gist options
  • Save todd1251/1161262 to your computer and use it in GitHub Desktop.
Save todd1251/1161262 to your computer and use it in GitHub Desktop.
Working with Core Plot trunk as at 22/08/2011
// Modified from: http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application
// NOTE: needs -load_all linker flag; might not be quite right - red/blue data line styles are not working
- (void)viewDidLoad
{
[super viewDidLoad];
graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
hostingView.hostedGraph = graph;
graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 20.0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-6)
length:CPTDecimalFromFloat(12)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-5)
length:CPTDecimalFromFloat(30)];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
CPTAxis *xAxis = [graph.axisSet.axes objectAtIndex: 0];
CPTAxis *yAxis = [graph.axisSet.axes objectAtIndex: 1];
xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
xAxis.minorTicksPerInterval = 4;
xAxis.majorTickLineStyle = lineStyle;
xAxis.minorTickLineStyle = lineStyle;
xAxis.axisLineStyle = lineStyle;
xAxis.minorTickLength = 5.0f;
xAxis.majorTickLength = 7.0f;
xAxis.labelOffset = 3.0f;
yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
yAxis.minorTicksPerInterval = 4;
yAxis.majorTickLineStyle = lineStyle;
yAxis.minorTickLineStyle = lineStyle;
yAxis.axisLineStyle = lineStyle;
yAxis.minorTickLength = 5.0f;
yAxis.majorTickLength = 7.0f;
yAxis.labelOffset = 3.0f;
CPTScatterPlot *xSquaredPlot = [[CPTScatterPlot alloc] init];
xSquaredPlot.identifier = @"X Squared Plot";
CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor redColor];
lineStyle.lineWidth = 1.0f;
xSquaredPlot.dataLineStyle = redLineStyle;
xSquaredPlot.dataSource = self;
[graph addPlot:xSquaredPlot];
CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
xSquaredPlot.plotSymbol = greenCirclePlotSymbol;
CPTScatterPlot *xInversePlot = [[CPTScatterPlot alloc] init];
xInversePlot.identifier = @"X Inverse Plot";
CPTMutableLineStyle *blueLineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blueColor];
lineStyle.lineWidth = 1.0f;
xInversePlot.dataLineStyle = blueLineStyle;
xInversePlot.dataSource = self;
[graph addPlot:xInversePlot];
}
#pragma mark - CPTPlotDataSource
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return 51;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
double val = (index/5.0)-5;
if(fieldEnum == CPTScatterPlotFieldX)
{ return [NSNumber numberWithDouble:val]; }
else
{
if(plot.identifier == @"X Squared Plot")
{ return [NSNumber numberWithDouble:val*val]; }
else
{ return [NSNumber numberWithDouble:1/val]; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment