I asked this question yesterday and got great help from Eric Skroch. However, I'm still a bit confused by looking at all the documentation on the Core Plot website, as well as some examples. So I'm going to post the code I have and see if anyone else can help me. Maybe Eric will be easier to help. My problem is that I need to delete the plot and then replace it with another one in the tabbed application. Eric and others have said to use the reloadData method. But I do not see where I can use it in my code? Here are my header and implementation files.
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface GraphViewController : UIViewController <CPTPlotDataSource>
@property (nonatomic, strong) CPTGraphHostingView *hostView;
@property (nonatomic, strong) NSMutableArray *myData;
@property (nonatomic) float XAXISMIN;
@property (nonatomic) float XAXISMAX;
@property (nonatomic) float YAXISMIN;
@property (nonatomic) float YAXISMAX;
@property (strong, nonatomic) IBOutlet UILabel *noGraph;
@end
and
#import "GraphViewController.h"
#import "MyVariables.h" // a singleton class for my variables
@interface GraphViewController ()
@end
@implementation GraphViewController
@synthesize hostView = _hostView;
@synthesize myData = _myData;
@synthesize XAXISMIN;
@synthesize XAXISMAX;
@synthesize YAXISMAX;
@synthesize noGraph;
@synthesize YAXISMIN;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[noGraph setText:@""];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
MyVariables *sharedVariables2 = [MyVariables sharedVariables2];
float a = [sharedVariables2.AA floatValue];
MyVariables *sharedVariables3 = [MyVariables sharedVariables3];
float b = [sharedVariables3.BB floatValue];
MyVariables *sharedVariables4 = [MyVariables sharedVariables4];
float c = [sharedVariables4.CC floatValue];
float d = (4.0f)*a*c;
float e = (b*b);
float f = (e - d);
float g = sqrtf(f);
float h = -b;
float i = h + g;
float ii = h - g;
float j = i/(2*a);
float k = ii/(2*a);
XAXISMIN = j - (5.0);
XAXISMAX = k + (5.0);
float z = c - ((b*b)/((4.0f)*a));
YAXISMAX = z + 10.0f;
YAXISMIN = -z;
float inc = (XAXISMAX - XAXISMIN) / 100.0f;
float l = XAXISMIN;
if ((isnan(g)) || (a == 0))
{
[noGraph setText:@"This is not graphable"];
[_hostView setHidden:YES];
}
else
{
NSMutableArray *data = [NSMutableArray array];
for (int i = 0; i < 100; i ++)
{
float y = (a * (l*l)) + (b * l) + c;
NSString *str4 = [NSString stringWithFormat:@"%.3f", l];
NSString *str5 = [NSString stringWithFormat:@"%.3f", y];
NSLog(@"Our X and Y are : %@, %@", str4, str5);
[data addObject:[NSValue valueWithCGPoint:CGPointMake(l, y)]];
l = l + inc;
}
self.myData = data;
[self initPlot];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setNoGraph:nil];
[super viewDidUnload];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return [_myData count];
}
- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex: (NSUInteger)index
{
if ([plot.identifier isEqual:@"Your data"])
{
NSValue *value = [self.myData objectAtIndex:index];
CGPoint point = [value CGPointValue];
if (fieldEnum == CPTScatterPlotFieldX)
{
return [NSNumber numberWithFloat:point.x];
}
else
{
return [NSNumber numberWithFloat:point.y];
}
}
return [NSNumber numberWithFloat:0];
}
- (void)initPlot
{
[self configureHost];
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
- (void)configureHost
{
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.hostView.allowPinchScaling = YES;
[self.view addSubview:self.hostView];
}
- (void)configureGraph
{
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
self.hostView.hostedGraph = graph;
NSString *title = @"Your Quadratic";
graph.title = title;
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = @"Helvetica-Bold";
titleStyle.fontSize = 16.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
[graph.plotAreaFrame setPaddingTop:30.0f];
[graph.plotAreaFrame setPaddingRight:30.0f];
[graph.plotAreaFrame setPaddingLeft:30.0f];
[graph.plotAreaFrame setPaddingBottom:30.0f];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
}
- (void)configurePlots
{
CPTGraph *graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
float xAxisMin = XAXISMIN;
float xAxisMax = XAXISMAX;
float yAxisMin = YAXISMIN;
float yAxisMax = YAXISMAX;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)];
CPTScatterPlot *quadGraph = [[CPTScatterPlot alloc] init];
quadGraph.dataSource = self;
quadGraph.identifier = @"Your data";
CPTColor *quadColor = [CPTColor blackColor];
[graph addPlot:quadGraph toPlotSpace:plotSpace];
CPTMutableLineStyle *quadLineStyle = [quadGraph.dataLineStyle mutableCopy];
quadLineStyle.lineWidth = 2.5;
quadLineStyle.lineColor = quadColor;
quadGraph.dataLineStyle = quadLineStyle;
CPTMutableLineStyle *quadSymbolLineStyle = [CPTMutableLineStyle lineStyle];
quadSymbolLineStyle.lineColor = quadColor;
CPTPlotSymbol *quadSymbol = [CPTPlotSymbol ellipsePlotSymbol];
quadSymbol.fill = [CPTFill fillWithColor:quadColor];
quadSymbol.lineStyle = quadSymbolLineStyle;
quadSymbol.size = CGSizeMake(6.0f, 6.0f);
quadGraph.plotSymbol = quadSymbol;
}
- (void)configureAxes
{
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
CPTAxis *x = axisSet.xAxis;
x.title = @"X Axis";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 1.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignPositive;
CPTAxis *y = axisSet.yAxis;
y.title = @"Y Axis";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -40.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 16.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
y.minorTickLength = 2.0f;
y.tickDirection = CPTSignNegative;
}
@end
Therefore, I hope someone can tell me where to clear my data / graph, so I only have one graph in the graph tab at a time. Thanks for that in advance!