3

我在使用 core-plot 库时遇到问题,如果我尝试添加一个点,我会收到错误消息。我不知道如何解决这个问题。

以下是未完成的块:

-(void)addNewData:(NSArray*)data{}

谁能帮我解决这个问题,谢谢。

我的错误:

I get the following error:
2012-09-24 14:51:09.790 sense_01[6719:907] ACC Connect
2012-09-24 14:51:15.270 sense_01[6719:907] *** Assertion failure in -[CPTScatterPlot reloadDataInIndexRange:], /Users/eskroch/Projects/Core Plot/framework/Source/CPTPlot.m:442
2012-09-24 14:51:15.272 sense_01[6719:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: NSMaxRange(indexRange) <= [self.dataSource numberOfRecordsForPlot:self]'
*** First throw call stack:
(0x394702a3 0x31d8f97f 0x3947015d 0x383da2af 0xaba75 0xaeccb 0xabcbb 0xaefeb 0xa7235 0xa360f 0x363df0ad 0x363df05f 0x363df03d 0x363de8f3 0x363dede9 0x363075f9 0x362f4809 0x362f4123 0x352f15a3 0x352f11d3 0x39445173 0x39445117 0x39443f99 0x393b6ebd 0x393b6d49 0x352f02eb 0x36348301 0xa2795 0xa2730)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

我的代码:

//
//  SingleSensorViewController.m
//  sense_01
//
//  Created by Morten Ydefeldt on 9/21/12.
//  Copyright (c) 2012 ydefeldt. All rights reserved.
//

#import "SingleSensorViewController.h"



@interface SingleSensorViewController ()

@end


@implementation SingleSensorViewController

@synthesize plot,graphView,label,SingleGraphView,portNumberForGraph,plotValues,plotLine;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        SingleGraphView = self.view;
        CATransition *transition = [CATransition animation];
        transition.duration = .5;
        transition.type = kCATransitionMoveIn; //choose your animation
        transition.subtype = kCATransitionFromTop;
        [SingleGraphView.layer addAnimation:transition forKey:nil];
        plotLine = [plot plotWithIdentifier:@"1"];

    }
    return self;
}

-(void)addNewData:(NSArray*)data{

    plotValues = [NSMutableArray arrayWithArray:data];
    int i = [plotValues count];
    [plotLine insertDataAtIndex:[plotValues count]-1 numberOfRecords:1];
    NSLog(@"DONE");
}

-(void)pointReceived:(NSNumber *) point{

    NSLog(@"some number");

}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{

    return [plotValues count];
}


-(NSNumber *)numberForPlot:(CPTPlot*)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{

    if (fieldEnum == CPTScatterPlotFieldX){
        return [NSNumber numberWithInt:[plotValues count]];

    }else{
        //return [plotValues objectAtIndex:plotValues.count-1];
        return [NSNumber numberWithInt:[plotValues count]];

    }

}


-(id)getView{
    return SingleGraphView;
}

//-(CGPoint)plotSpace:(CPTPlotSpace *)space willDisplaceBy:(CGPoint)proposedDisplacementVector
//{
//    NSLog(@"PLOT SPACE");
//
//return CGPointMake(proposedDisplacementVector.x, proposedDisplacementVector.y);
//
//}
//
//-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
//{
//    
//    
//    
//    //    if (coordinate == CPTCoordinateY) {
//    //        newRange = ((CPTXYPlotSpace*)space).yRange;
//    //    }
//    NSLog(@"NEW RANGE");
//    return newRange;
//    
//    
//}

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

    plot = [[CPTXYGraph alloc] initWithFrame:CGRectMake(100, 10, 800, 180)];
    //CPTGraphHostingView *layerHostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(100, 10, 800, 180)];
    graphView.collapsesLayers = NO;
    graphView.hostedGraph = plot;

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace* )plot.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.delegate = self;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(0.0) length: CPTDecimalFromDouble(20.0)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(0.0) length: CPTDecimalFromDouble(50.0)];

    CPTScatterPlot *line = [[CPTScatterPlot alloc]init];
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];

    //Fill colors
    float c= 0.2;
    plot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:c green:c blue:c alpha:0]];
    plot.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:c green:c blue:c alpha:0.3]];
    plot.plotAreaFrame.borderLineStyle = nil;

    //Plot border
    lineStyle.lineColor = [CPTColor grayColor];
    lineStyle.lineWidth = 1.2;
    //plot.borderLineStyle = lineStyle;

    //Plot padding
    plot.paddingBottom = 5;
    plot.paddingLeft = 5;
    plot.paddingRight = 5;
    plot.paddingTop = 5;

    //axis range
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)plot.axisSet;
    CPTXYAxis *y = axisSet.yAxis;
    CPTXYAxis *x = axisSet.xAxis;
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");

    y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;

    x.axisConstraints = [CPTConstraints constraintWithLowerOffset:15.0];
    y.axisConstraints = [CPTConstraints constraintWithLowerOffset:35.0];

    y.minorTickLength = 0;
    y.majorTickLength = 0;
    x.minorTickLength = 0;
    x.majorTickLength = 0;

    //axis theming
    c = 0.4;
    lineStyle.lineColor = [CPTColor colorWithComponentRed:c green:c blue:c alpha:1];
    lineStyle.lineWidth = 0.7f;
    y.axisLineStyle = lineStyle;
    x.axisLineStyle = lineStyle;


    //label theming
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor whiteColor];
    textStyle.fontName = @"Helvetica-Bold";
    x.labelTextStyle = textStyle;
    y.labelTextStyle = textStyle;

    //grid theming
    c = 0.3;
    lineStyle.lineColor = [CPTColor colorWithComponentRed:c green:c blue:c alpha:1];
    lineStyle.lineWidth = 0.1f;
    x.minorGridLineStyle = lineStyle;
    y.minorGridLineStyle = lineStyle;


    //curve theming
    lineStyle.miterLimit = 1.0f;
    lineStyle.lineWidth = 1.5f;
    lineStyle.lineColor = [CPTColor whiteColor];
    line.dataLineStyle = lineStyle;
    line.identifier = @"1";
    line.dataSource = self;


    //plotsymbol
    CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    CPTGradient *blueGradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithComponentRed:62.0f/255.0f green:112.0/255.0f blue:184.0f/255.0f alpha:1] endingColor:[CPTColor colorWithComponentRed:43.0f/255.0f green:63.0/255.0f blue:153.0f/255.0f alpha:1]];
    lineStyle.lineColor = [CPTColor whiteColor];
    lineStyle.lineWidth = 1.0f;
    plotSymbol.lineStyle = lineStyle;
    plotSymbol.fill = [CPTFill fillWithGradient:blueGradient];
    plotSymbol.size = CGSizeMake(11.0, 11.0);
    line.plotSymbol = plotSymbol;

    line.delegate = self;
    line.plotSymbolMarginForHitDetection = 5.0f;

    [plot addPlot:line];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setGraphView:nil];
    [self setLabel:nil];
    [self setIconimage:nil];
    [super viewDidUnload];



}


- (IBAction)addPoint:(id)sender {
    NSLog(@"ADD POINT");
}
@end
4

2 回答 2

2
  1. 由于您要替换整个数据数组,因此请使用该-reloadData方法而不是- insertDataAtIndex:numberOfRecords:. 当您需要将数据点添加到现有数据集时,您可以插入数据。

  2. 您需要保留plotValues数组。否则,当 Core Plot 尝试读取它时,数​​组可能会被释放,你会得到垃圾数据或崩溃。

于 2012-09-25T00:57:25.897 回答
0

我发现导致这个问题的问题是,当我尝试在图表上添加一个点时,视图不是必需的,所以如果我包括

[self addNewData:[NSArray arrayWithObject:@"23"]];

在 viewdidload 的末尾添加了点,一切正常!

于 2012-09-25T01:42:45.083 回答