objective-c - 由于 NSMaxRange(indexRange) <= [self.dataSource numberOfRecordsForPlot :self] 无法绘制

标签 objective-c ios ipad core-plot

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

未完成的是以下 block :

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

最佳答案

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

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

关于objective-c - 由于 NSMaxRange(indexRange) <= [self.dataSource numberOfRecordsForPlot :self] 无法绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12565597/

相关文章:

objective-c - 如何使用关闭按钮关闭 UIActionSheet 并避免崩溃?

ios - "Access to PassKit Shared Cache file denied. ..."?

iphone - 由于 UIImageViews,应用程序运行缓慢

ios - 将数据从文本字段发送到可变数组

在没有 XCode 的情况下使用 CGL/NSOpenGL 在 OSX 上使用 OpenGL 的 C++ 代码?

ios - 使用嵌套上下文时如何自动设置核心数据关系

iphone - 当我的 iPad 关闭时只收到最后的推送通知

iphone - iOS5无法自定义标签栏

ios - 在 StoreKit 拒绝交易

iphone - iOS SDK - 以编程方式生成 PDF 文件