5

我昨天问了这个问题,并从 Eric Skroch 那里得到了很大的帮助。但是,在查看了 Core Plot 网站上的所有文档以及一些示例后,我仍然有点困惑。因此,我将发布到目前为止的代码,看看是否有人可以帮助我更多。也许埃里克也能更容易地提供帮助。我的问题是我需要删除一个情节,然后在选项卡式应用程序中将其替换为另一个情节。Eric 和其他人说要使用 reloadData 方法。但是我看不到我可以在我的代码中的哪里使用它?这是我的头文件和实现文件。

#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

#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) {
    // Custom initialization
}
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);

// Define x max and min with answers to quad
XAXISMIN = j - (5.0);
XAXISMAX = k + (5.0);

// Find range for the y axis  STILL NEEDS WORK!
// maybe if statement for negatives
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;  // put that data array into the array you synthesized
    [self initPlot];   
}
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{   
[self setNoGraph:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (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];

    // FieldEnum determines if we return an X or Y value.
    if (fieldEnum == CPTScatterPlotFieldX) 
    {
        return [NSNumber numberWithFloat:point.x];
    }
    else    // Y-Axis
    {
        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
{
// 1 - Create graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
self.hostView.hostedGraph = graph;


// 2 - Set graph title
NSString *title = @"Your Quadratic";
graph.title = title;

// 3 - Create and set text style
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);

// 4 - Set padding for plot area
[graph.plotAreaFrame setPaddingTop:30.0f];
[graph.plotAreaFrame setPaddingRight:30.0f];
[graph.plotAreaFrame setPaddingLeft:30.0f];
[graph.plotAreaFrame setPaddingBottom:30.0f];

// 5 - Enable use interaction for plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
}

- (void)configurePlots
{
// 1 - Get graph and plot spaces and set axis
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)];

// 2 - Create the plot
CPTScatterPlot *quadGraph = [[CPTScatterPlot alloc] init];
quadGraph.dataSource = self;
quadGraph.identifier = @"Your data";
CPTColor *quadColor = [CPTColor blackColor];
[graph addPlot:quadGraph toPlotSpace:plotSpace];

// 4 - Create styles and symbols
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
{
// 1 - Create styles
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;

// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;

// 3 - Configure x-axis
CPTAxis *x = axisSet.xAxis;
x.title = @"X Axis"; 
x.titleTextStyle = axisTitleStyle;    
x.titleOffset = 1.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions; //CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;    
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignPositive; //CPTSignNegative;

// 4 - Configure y-axis
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

所以我希望有人可以让我知道在哪里清除我的数据/绘图,这样我一次在图表选项卡上只有一个图表。提前非常感谢!

4

2 回答 2

6

您应该初始化和配置图形和托管视图(即调用-initPlot),-viewDidLoad以便它只发生一次。在-viewDidAppear中,执行您需要的任何数据处理,为图形做好准备,然后调用-reloadData.

于 2012-08-26T21:46:30.927 回答
4

好吧,对于任何关注这个问题的人,我已经找到了答案。在 Eric Skroch 的大力帮助和指导下,他带领我朝着正确的方向前进。至少我认为我做的是对的!由于我的图表仅是使用 viewDidAppear 方法创建的,因此我必须找到另一种使用 reloadData 的方法。所以我 NSLogged 几种方法来查看当视图消失时调用了哪个方法,我发现 viewWillDisappear 就是其中之一。因此,在这种方法中,我使用了以下方法;

self.hostView.hostedGraph = nil;

[_myData removeAllObjects];.

这将清除图表并为下一个做好准备,并清空我的 Array 以使其做好准备。我很想听听任何人对他们对我的解决方法的看法。不过,我会接受 Eric 的回答,因为没有他的帮助,我不会走到这一步。再次感谢!

于 2012-08-27T22:14:40.690 回答