I am having trouble with the core-plot library, if I try to add a point I get the an error. I dont know how to fix this issue.
It is the following block which does not complete:
-(void)addNewData:(NSArray*)data{}
Can someone please help me resolve this problem, thanks.
My error:
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)
My code:
//
// 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
Since you’re replacing the entire data array, use the
-reloadDatamethod instead of- insertDataAtIndex:numberOfRecords:. You insert data when you need to add data points to an existing data set.You need to retain the
plotValuesarray. Otherwise, the array might be deallocated by the time Core Plot tries to read it and you’ll get junk data or a crash.