I'm a beginner in Stackoverflow and Xcode for iPhone.
English is not my native language, so please bear with me.
I've been trying to make a graph between dates (x axis) and the Fuel Economy value (y axis).
I'm using core plot 1.4.
My problem are:
The data somehow is not sync directly to x axis.
Major checklist somehow is not shown. (solved)
Is there any way to make the graph only shows the positif axis? (solved)
I've doing a lot of NSLog to debug the data. I've search google (including this site) for the tutorial of how to use the custom label and how to make scatter plot graph).
I have the following data:
(
{
0 = "31-Jan-2014";
1 = 10;
},
{
0 = "02-Feb-2014";
1 = "10.07";
},
{
0 = "24-Feb-2014";
1 = "8.75";
}
)
I've got strange result with above data. Please see the included graph
this is the picture of my graph:
in the graph, the y data is not mapped to x axis.
here's the relevant code
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
NSLog(#"Number of Records for Plot = %lu",(unsigned long)[self.plotData count]);
return [_plotData count];
}
- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{
//NSDecimalNumber *result = [[_plotData objectAtIndex:idx]objectForKey:[NSNumber numberWithInt:fieldEnum]];
NSNumber *result;
switch (fieldEnum) {
case CPTScatterPlotFieldX:
// NSDate * observationDate = [[_plotData objectAtIndex:idx]objectForKey:[NSNumber numberWithInt:fieldEnum]];
// NSTimeInterval secondsSince1970 = [_observationDate timeIntervalSince1970];
result = [[_plotData objectAtIndex:idx]objectForKey:[NSNumber numberWithInt:fieldEnum]];
break;
case CPTScatterPlotFieldY:
result = [[_plotData objectAtIndex:idx] objectForKey:[NSNumber numberWithInt:fieldEnum]];
}
//result = [[_plotData objectAtIndex:idx] objectForKey:[NSNumber numberWithInt:fieldEnum]];
NSLog(#"Number tobe Plot with index = %# %lu, %lu",result,(unsigned long)idx,(unsigned long)fieldEnum);
return result;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initPlot];
// Do any additional setup after loading the view.
}
- (void)initPlot
{
//NSDate *refDate = [NSDate dateWithNaturalLanguageString:#"12:00 Oct 29, 2009"];
[self fetchResultOneWeek];
[self configureHost];
[self configureFEGraph];
[self configurePlots];
[self configureAxis];
}
- (void)configureHost
{
CGRect frame1 = CGRectMake(0,80,320,200);
FESubView = [[CPTGraphHostingView alloc]initWithFrame:frame1];
FESubView.allowPinchScaling = YES;
[self.view addSubview:FESubView];
CGRect frame2 = CGRectMake(0,410,320,400);
GasPriceSubView = [[CPTGraphHostingView alloc]initWithFrame:frame2];
GasPriceSubView.allowPinchScaling = YES;
[self.view addSubview:GasPriceSubView];
}
- (void)configureFEGraph
{
CPTGraph *graph = [[CPTXYGraph alloc]initWithFrame:FESubView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTStocksTheme]];
FESubView.hostedGraph = graph;
graph.title = #"Fuel Economy Graph";
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = #"Helvetica-Bold";
titleStyle.fontSize = 14.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
// 4 - Set padding for plot area
[graph.plotAreaFrame setPaddingLeft:10.0f];
[graph.plotAreaFrame setPaddingBottom:10.0f];
[graph.plotAreaFrame setPaddingTop:10.0f];
[graph.plotAreaFrame setPaddingRight:10.0f];
// 5 - Enable user interactions for plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
}
- (void)configurePlots
{
NSMutableArray *dataTobePlotted = [NSMutableArray array];
NSUInteger i;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd-MMM-YYY"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:07"]];
//NSTimeInterval oneDay = 24 * 60 * 60;
for (i = 0;i < [[self.fullTransaction valueForKey:#"tDate"]count]; i++)
{
NSDate *observationDate = [[self.fullTransaction valueForKey:#"tDate"]objectAtIndex:i];
NSLog(#"observationDate is : %#",observationDate);
NSTimeInterval x = [observationDate timeIntervalSince1970];
NSLog(#"x is : %f",x);
id y = [[self.fullTransaction valueForKey:#"tFuelEconomy"]objectAtIndex:i];
NSLog(#"y is : %#",y);
[dataTobePlotted addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:x],[NSNumber numberWithInt:CPTScatterPlotFieldX],y,[NSNumber numberWithInt:CPTScatterPlotFieldY],nil]];
//[dataTobePlotted addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSDecimalNumber numberWithFloat:x],[NSNumber numberWithInt:CPTScatterPlotFieldX],y,[NSNumber numberWithInt:CPTScatterPlotFieldY],nil]];
NSLog(#" int X is %#",[NSNumber numberWithInt:CPTScatterPlotFieldX]);
NSLog(#" int Y is %#",[NSNumber numberWithInt:CPTScatterPlotFieldY]);
NSLog(#"ScatterPlotField X is %#",[NSNumber numberWithInt:CPTScatterPlotFieldX]);
NSLog(#"ScatterPlotField Y is %#",[NSNumber numberWithInt:CPTScatterPlotFieldY]);
NSLog(#"Data to be Plotted: %#",dataTobePlotted);
}
self.plotData = dataTobePlotted;
CPTGraph *graph = FESubView.hostedGraph;
[graph applyTheme:[CPTTheme themeNamed:kCPTStocksTheme]];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
//create plot
CPTScatterPlot *fePlot = [[CPTScatterPlot alloc]init];
fePlot.dataSource = self;
CPTColor *feColor = [CPTColor greenColor];
[graph addPlot:fePlot];
//setup plot space
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects: fePlot,Nil]];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(#"3.0") length:CPTDecimalFromString(#"10.0")];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(#"8.0") length:CPTDecimalFromString(#"2.0")];
//set up plot space
//[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:fePlot,nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(2.0f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(5.0f)];
plotSpace.yRange = yRange;
// //create styles and symbols
CPTMutableLineStyle *feLineStyle = [fePlot.dataLineStyle mutableCopy];
CPTColor *areaColor = [CPTColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.3];
CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor:areaColor endingColor:[CPTColor clearColor]];
areaGradient.angle = -90.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient];
fePlot.areaFill = areaGradientFill;
feLineStyle.lineWidth = 2.5;
feLineStyle.lineColor = feColor;
fePlot.dataLineStyle = feLineStyle;
CPTMutableLineStyle *feSymbolLineStyle = [CPTMutableLineStyle lineStyle];
feSymbolLineStyle.lineColor = feColor;
CPTPlotSymbol *feSymbol = [CPTPlotSymbol ellipsePlotSymbol];
feSymbol.fill = [CPTFill fillWithColor:feColor];
feSymbol.lineStyle = feSymbolLineStyle;
feSymbol.size = CGSizeMake(6.0f, 6.0f);
fePlot.plotSymbol = feSymbol;
}
- (void)configureAxis
{
// 1 - Create styles
// 2 - Get axis set
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;
CPTMutableTextStyle *labelXTextStyle = [CPTMutableTextStyle textStyle];
labelXTextStyle.fontName = #"Helvetica";
labelXTextStyle.fontSize = 10;
labelXTextStyle.color = [CPTColor whiteColor];
// 2 - Get Axis Set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) FESubView.hostedGraph.axisSet;
// 3 Configure x-Axis
CPTXYAxis *x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"4");
x.title = #"Date";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 47.0f;
//x.title = #"Date";
x.minorTicksPerInterval = 0;
x.majorTickLength = 10.0f;
x.tickDirection = CPTSignPositive;
//NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15],
// [NSDecimalNumber numberWithInt:20], [NSDecimalNumber numberWithInt:25],[NSDecimalNumber numberWithInt:30],nil];
NSInteger i;
NSMutableArray *customTickLocations = [NSMutableArray array];
for (i = 0; i < [[self.fullTransaction valueForKey:#"tDate"]count];i++)
{
[customTickLocations addObject:[NSDecimalNumber numberWithInt:((i+1)*5)]];
}
CPTPlotRange *xAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(#"0.0") length:CPTDecimalFromString(#"24.0")];
x.visibleRange=xAxisRange;
NSLog(#"dates are %#", [self.fullTransaction valueForKey:#"tDate"]);
NSLog(#"Transaction FE %#", [self.fullTransaction valueForKey:#"tFuelEconomy"]);
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[self.plotData count]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd-MMM-YYY"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:07"]];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:[[self.fullTransaction valueForKey:#"tDate"]count]];
x.labelingPolicy=CPTAxisLabelingPolicyNone;
x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"1"] decimalValue];
NSInteger location;
for (location = 0; location < [[self.fullTransaction valueForKey:#"tDate"]count];location++)
{
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [dateFormatter stringFromDate:[[self.fullTransaction valueForKey:#"tDate"] objectAtIndex:location]] textStyle:labelXTextStyle];
NSLog(#"Custom Labels = %#",newLabel);
NSLog(#"Real Date = %#",[[self.fullTransaction valueForKey:#"tDate"] objectAtIndex:location]);
NSLog(#"After assigned date = %#",[dateFormatter stringFromDate:[[self.fullTransaction valueForKey:#"tDate"] objectAtIndex:location]]);
newLabel.tickLocation = CPTDecimalFromInt([[customTickLocations objectAtIndex:location] integerValue]);
newLabel.offset = 2;
newLabel.rotation = (1*M_PI)/4;
[customLabels addObject:newLabel];
[xLocations addObject:[NSNumber numberWithInteger:[[customTickLocations objectAtIndex:location] integerValue]]];
}
x.axisLabels = [NSSet setWithArray:customLabels];
NSLog(#"Custom Labels = %#",customLabels);
x.majorTickLocations = xLocations;
//x.majorTickLocations = customTickLocations;
x.majorTickLength = 5.0f;
NSLog(#"Major TickLocations: %#",customTickLocations);
CPTXYAxis *y = axisSet.yAxis;
//y.orthogonalCoordinateDecimal = CPTDecimalFromCGFloat(0.0);
y.majorIntervalLength = CPTDecimalFromString(#"1.0");
y.minorTicksPerInterval = 5;
y.majorTickLength = 5.0f;
y.preferredNumberOfMajorTicks = 5;
y.labelTextStyle = labelXTextStyle;
}
The following is the result of nslog for the id x and id y data
2014-02-25 16:09:01.855 Fuel Eco[12079:70b] x is : 31-Jan-2014
2014-02-25 16:09:01.855 Fuel Eco[12079:70b] y is : 10
2014-02-25 16:09:01.855 Fuel Eco[12079:70b] int X is 0
2014-02-25 16:09:01.856 Fuel Eco[12079:70b] int Y is 1
2014-02-25 16:09:01.856 Fuel Eco[12079:70b] ScatterPlotField X is 0
2014-02-25 16:09:01.856 Fuel Eco[12079:70b] ScatterPlotField Y is 1
2014-02-25 16:09:01.857 Fuel Eco[12079:70b] Data to be Plotted: (
{
0 = "31-Jan-2014";
1 = 10;
}
)
2014-02-25 16:09:01.857 Fuel Eco[12079:70b] x is : 02-Feb-2014
2014-02-25 16:09:01.857 Fuel Eco[12079:70b] y is : 10.07
2014-02-25 16:09:01.858 Fuel Eco[12079:70b] int X is 0
2014-02-25 16:09:01.858 Fuel Eco[12079:70b] int Y is 1
2014-02-25 16:09:01.858 Fuel Eco[12079:70b] ScatterPlotField X is 0
2014-02-25 16:09:01.859 Fuel Eco[12079:70b] ScatterPlotField Y is 1
2014-02-25 16:09:01.859 Fuel Eco[12079:70b] Data to be Plotted: (
{
0 = "31-Jan-2014";
1 = 10;
},
{
0 = "02-Feb-2014";
1 = "10.07";
}
)
2014-02-25 16:09:01.859 Fuel Eco[12079:70b] x is : 24-Feb-2014
2014-02-25 16:09:01.860 Fuel Eco[12079:70b] y is : 8.75
2014-02-25 16:09:01.860 Fuel Eco[12079:70b] int X is 0
2014-02-25 16:09:01.860 Fuel Eco[12079:70b] int Y is 1
2014-02-25 16:09:01.861 Fuel Eco[12079:70b] ScatterPlotField X is 0
2014-02-25 16:09:01.861 Fuel Eco[12079:70b] ScatterPlotField Y is 1
2014-02-25 16:09:01.861 Fuel Eco[12079:70b] Data to be Plotted: (
{
0 = "31-Jan-2014";
1 = 10;
},
{
0 = "02-Feb-2014";
1 = "10.07";
},
{
0 = "24-Feb-2014";
1 = "8.75";
}
)
Below is the result of the nslog to show the result of "- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx" with the data,idx,fieldEnum.
2014-02-25 16:09:01.862 Fuel Eco[12079:70b] Number of Records for Plot = 3
2014-02-25 16:09:01.862 Fuel Eco[12079:70b] Number of Records for Plot = 3
2014-02-25 16:09:01.863 Fuel Eco[12079:70b] Number tobe Plot with index = 31-Jan-2014 0, 0
2014-02-25 16:09:01.863 Fuel Eco[12079:70b] Number tobe Plot with index = 02-Feb-2014 1, 0
2014-02-25 16:09:01.863 Fuel Eco[12079:70b] Number tobe Plot with index = 24-Feb-2014 2, 0
2014-02-25 16:09:01.864 Fuel Eco[12079:70b] Number of Records for Plot = 3
2014-02-25 16:09:01.864 Fuel Eco[12079:70b] Number tobe Plot with index = 10 0, 1
2014-02-25 16:09:01.864 Fuel Eco[12079:70b] Number tobe Plot with index = 10.07 1, 1
2014-02-25 16:09:01.865 Fuel Eco[12079:70b] Number tobe Plot with index = 8.75 2, 1
2014-02-25 16:09:01.865 Fuel Eco[12079:70b] Number of Records for Plot = 3
Thanks all
UPDATE ***
I rewrote the code to become like this, and it works:
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
switch (fieldEnum)
{
case CPTScatterPlotFieldX:
{
NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
case CPTScatterPlotFieldY:
{
if ([plot.identifier isEqual:#"Honda Plot"])
{
NSDecimalNumber *num = [[plotData1 objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
else
{
NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
}
}
return nil;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchResult];
[self filterarray];
[self filterArray1];
//CGRect frame1 = CGRectMake(0,200,300,600);
[self generateData];
//FESubView = [[CPTGraphHostingView alloc]initWithFrame:frame1];
[self renderInLayer:FESubView withTheme:Nil animated:NO];
}
-(void)generateData
{
if ( !plotData ) {
//const NSTimeInterval oneDay = 24 * 60 * 60 ;
gregorian = [self setCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[self.fullTransaction valueForKey:#"tDate" ] objectAtIndex:0]];
NSDate *refDate = [gregorian dateFromComponents:dateComponents];
NSDate *dataDate = [[NSDate alloc]init];
NSDateComponents *comps;
int days;
// Add some data
NSMutableArray *newData = [NSMutableArray array];
NSMutableArray *newData1 = [NSMutableArray array];
NSUInteger i;
for ( i = 0; i < [[self.fullTransaction valueForKey:#"tDate"]count]; i++ ) {
dataDate = [[self.fullTransaction valueForKey:#"tDate"]objectAtIndex:i];
NSLog(#"RefDate is: %#",refDate);
NSLog(#"DateDate is: %#",dataDate);
comps = [gregorian components:NSDayCalendarUnit fromDate:refDate toDate:dataDate options:0];
days = [comps day];
NSTimeInterval x = oneDay *days;
//id y = [[self.fullTransaction valueForKey:#"tFuelEconomy"]objectAtIndex:i];
int counts = [self.filteredTransaction count];
id y;
id z;
int count1 = [self.filteredTransaction1 count];
if (i < counts)
{
y = [[self.filteredTransaction valueForKey:#"tFuelEconomy"]objectAtIndex:i];
}else y = NULL;
if (i < count1)
{
z = [[self.filteredTransaction1 valueForKey:#"tFuelEconomy"]objectAtIndex:i];
}else z = NULL;
[newData addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX],
y, [NSNumber numberWithInt:CPTScatterPlotFieldY],
nil]];
[newData1 addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX],
z, [NSNumber numberWithInt:CPTScatterPlotFieldY],
nil]];
}
plotData = newData;
plotData1 = newData1;
NSLog(#"Data are: %#",plotData);
NSLog(#"Data are: %#",plotData1);
}
}
-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated
{
// If you make sure your dates are calculated at noon, you shouldn't have to
// worry about daylight savings. If you use midnight, you will have to adjust
// for daylight savings time.
CGRect frame1 = CGRectMake(10,150,320,250);
FESubView = [[CPTGraphHostingView alloc]initWithFrame:frame1];
FESubView.allowPinchScaling = YES;
[self.view addSubview:FESubView];
//NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
gregorian = [self setCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[self.fullTransaction valueForKey:#"tDate" ] objectAtIndex:0]];
NSDate *refDate = [gregorian dateFromComponents:dateComponents];
NSLog(#"RefDate is :%#",refDate);
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
CGRect bounds = layerHostingView.bounds;
#else
CGRect bounds = NSRectToCGRect(layerHostingView.bounds);
#endif
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
FESubView.hostedGraph = graph;
graph.title = #"Fuel Economy Graph";
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = #"Helvetica-Bold";
titleStyle.fontSize = 14.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
// 4 - Set padding for plot area
[graph.plotAreaFrame setPaddingLeft:40.0f];
[graph.plotAreaFrame setPaddingBottom:65.0f];
[graph.plotAreaFrame setPaddingTop:3.0f];
[graph.plotAreaFrame setPaddingRight:3.0f];
graph.legend = [CPTLegend legendWithGraph:graph];
graph.legend.fill = [CPTFill fillWithColor:[CPTColor darkGrayColor]];
graph.legend.cornerRadius = 5.0;
graph.legend.swatchSize = CGSizeMake(25.0, 25.0);
graph.legendAnchor = CPTRectAnchorCenter;
graph.legendDisplacement = CGPointMake(2.40, 12.0);
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) length:CPTDecimalFromFloat(oneDay * 10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(5.0) length:CPTDecimalFromFloat(10.0f)];
//symbol
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1)];
//
plotSpace.allowsUserInteraction = YES;
// Create a plot that uses the data source method
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.identifier = #"Nisan Plot";
CPTColor *feColor = [CPTColor redColor];
CPTMutableLineStyle *feLineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
feLineStyle.lineWidth = 2.5;
feLineStyle.lineColor = feColor;
dataSourceLinePlot.dataLineStyle = feLineStyle;
CPTMutableLineStyle *feSymbolLineStyle = [CPTMutableLineStyle lineStyle];
feSymbolLineStyle.lineColor = feColor;
CPTPlotSymbol *feSymbol = [CPTPlotSymbol ellipsePlotSymbol];
feSymbol.fill = [CPTFill fillWithColor:feColor];
feSymbol.lineStyle = feSymbolLineStyle;
feSymbol.size = CGSizeMake(6.0f, 6.0f);
dataSourceLinePlot.plotSymbol = feSymbol;
CPTMutableTextStyle *feLabelStyle = [CPTMutableTextStyle textStyle];
feLabelStyle.fontSize = 10;
feLabelStyle.color = [CPTColor whiteColor];
dataSourceLinePlot.labelTextStyle = feLabelStyle;
dataSourceLinePlot.title = #"Nissan Terano";
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = #"Helvetica-Bold";
axisTextStyle.fontSize = 8.0f;
CPTScatterPlot *dataSourceLinePlot1 = [[CPTScatterPlot alloc] init];
dataSourceLinePlot1.identifier = #"Honda Plot";
CPTColor *feColor1 = [CPTColor blueColor];
CPTMutableLineStyle *feLineStyle1 = [dataSourceLinePlot1.dataLineStyle mutableCopy];
feLineStyle1.lineWidth = 2.5;
feLineStyle1.lineColor = feColor1;
dataSourceLinePlot1.dataLineStyle = feLineStyle1;
CPTMutableLineStyle *feSymbolLineStyle1 = [CPTMutableLineStyle lineStyle];
feSymbolLineStyle1.lineColor = feColor1;
CPTPlotSymbol *feSymbol1 = [CPTPlotSymbol ellipsePlotSymbol];
feSymbol1.fill = [CPTFill fillWithColor:feColor1];
feSymbol1.lineStyle = feSymbolLineStyle1;
feSymbol1.size = CGSizeMake(6.0f, 6.0f);
dataSourceLinePlot1.plotSymbol = feSymbol1;
//CPTTextStyle *test = [CPTTextStyle textStyle];
//test.fontSize = 12;
CPTMutableTextStyle *feLabelStyle1 = [CPTMutableTextStyle textStyle];
feLabelStyle1.fontSize = 10;
feLabelStyle1.color = [CPTColor whiteColor];
dataSourceLinePlot1.labelTextStyle = feLabelStyle1;
dataSourceLinePlot1.dataSource = self;
[graph addPlot:dataSourceLinePlot1];
dataSourceLinePlot1.title = #"Honda Jazz";
//
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromFloat(oneDay);
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"5");
x.minorTicksPerInterval = 0;
x.labelTextStyle = axisTextStyle;
//x.axisLineStyle = axisLineStyle;
//x.axisLineStyle = feLabelStyle;
dateFormatter = [self setDateFormatterLabel];
//dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
x.labelFormatter = timeFormatter;
x.labelRotation = M_PI / 4;
x.tickLabelDirection = CPTSignPositive;
x.tickDirection = CPTSignPositive;
//x.labelTextStyle = feLabelStyle;
//x.visibleRange=plotSpace.xRange;
x.labelOffset = -70;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromString(#"5.0");
y.minorTicksPerInterval = 10;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"-1");
}
I'm not sure what you mean. Can you explain the question more clearly?
You never add any locations to the xLocations set. If the customTickLocations array contains all of the locations you need, just set the tick locations directly:
x.majorTickLocations = [NSSet setWithArray:customTickLocations];
Why are you using -expandRangeByFactor:? If you know the plot range you want, just set it directly:
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0)
length:CPTDecimalFromDouble(10.0)];
I encountered a problem when using a custom label in Core Plot:
My Core Plot supports zoom.
The x-axis label is custom x.axisLabels
On first load, the interface is normal, but when I narrow Core Plot, the axis labels overlap together.
How to design a level on the label? For example, when the minimum level only shows a year, in level display date, the maximum level shows the date.
- (void)initPlotGraph
{
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
self.hostedGraph = graph;
locationLabels = [[NSMutableArray alloc]init];
graph.plotAreaFrame.paddingLeft += 5.0;
graph.plotAreaFrame.paddingTop += 5.0;
graph.plotAreaFrame.paddingRight += 5.0;
graph.plotAreaFrame.paddingBottom += 17.5;
//[self setAllowPinchScaling:NO];
// Setup scatter plot space
plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1+0.3)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)length:CPTDecimalFromFloat(1+0.3)];
[self initPlotAxis];
}
- (void)initPlotAxis
{
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2;
axisLineStyle.lineColor = [CPTColor colorWithComponentRed:CPTFloat(0.11765) green:CPTFloat(0.49804) blue:CPTFloat(0.87451) alpha:CPTFloat(1)];
CPTMutableLineStyle *axisTickLineStyle = [CPTMutableLineStyle lineStyle];
axisTickLineStyle.lineWidth = 0;
CPTLineCap *lineCap = [CPTLineCap sweptArrowPlotLineCap];
lineCap.size = CGSizeMake(10, 10);
lineCap.lineCapType = CPTLineCapTypeNone;
// Axes
// Label x axis with a fixed interval policy
lineCap.lineStyle = axisLineStyle;
lineCap.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:CPTFloat(0.29019) green:CPTFloat(0.54118) blue:CPTFloat(0.76471) alpha:CPTFloat(1)]];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromInt(1);
x.axisLineStyle = axisLineStyle;
x.axisConstraints = [CPTConstraints constraintWithRelativeOffset:0];
x.axisLineCapMax = lineCap;
x.axisLabels = [self buildLabelTitle];
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.minorTickLocations = [NSSet setWithArray:locationLabels];
// Label y with an automatic label policy.
lineCap.lineStyle = axisLineStyle;
lineCap.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:CPTFloat(0.29019) green:CPTFloat(0.54118) blue:CPTFloat(0.76471) alpha:CPTFloat(1)]];
CPTXYAxis *y = axisSet.yAxis;
// y.majorIntervalLength = CPTDecimalFromDouble(5);
y.majorTickLineStyle = axisTickLineStyle;
y.minorTickLineStyle = axisTickLineStyle;
y.axisLineStyle = axisLineStyle;
y.axisLineCapMax = lineCap;
y.axisConstraints = [CPTConstraints constraintWithRelativeOffset:0];
// Set axes
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
}
- (NSMutableSet*)buildLabelTitle
{
NSMutableSet *newAxisLabels = [NSMutableSet set];
CPTMutableTextStyle *textStyleB = [CPTMutableTextStyle textStyle];
textStyleB.color = [CPTColor colorWithComponentRed:CPTFloat((float)0x09/0xFF) green:CPTFloat((float)0x31/0xFF) blue:CPTFloat((float)0x4A/0xFF) alpha:CPTFloat(1)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(corePlotNums - 9)length:CPTDecimalFromInt(10)];
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(corePlotNums + 1)];
for ( NSUInteger i = 1; i < corePlotNums + 1; i++)
{
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[[m_labelStr objectAtIndex:i-1] substringWithRange:NSMakeRange(5, 11)] textStyle:textStyleB];
newLabel.tickLocation = CPTDecimalFromUnsignedInteger(i);
newLabel.offset = 5;
[newAxisLabels addObject:newLabel];
[newLabel release];
}
return newAxisLabels;
}
If the user is allowed to change the plot ranges (i.e., by pinch zooming), use a plot space delegate to monitor changes to the plot space. Use the new plot ranges and the size of the plot area to determine what time scale to use and how many labels to display. Adjust the axis labeling properties as needed.
Am breaking my head on this since yesterday and can't get it to work! All I get is a plain x-axis line, y-axis line (not even the ticks or labels!) and chart title! Please help! Seems so simple, but so elusive!
Am plotting the last 5 values of memory usage of a server. The values are for each minute approximately (sometimes, it's for two minutes once). For example, one set of last 5 values I might want to show is like this:
Date (vs) Memory Usage (bytes)
2013-05-02 11:50:33 +0000 - 157888512.000000
2013-05-02 11:51:33 +0000 - 157839360.000000
2013-05-02 11:52:14 +0000 - 157888512.000000
2013-05-02 11:56:36 +0000 - 157863936.000000
2013-05-02 11:57:48 +0000 - 157888512.000000
I refresh this graph every minute.
The 'numberForPlot', 'numberOfRecordsForPlot' seems to get called, but no plotting is seen/visible in the graph.
My graph is initialized with this method :
-(void) initializeGraph {
NSLog(#"View_Visualizer.initializeGraph");
if ((self.graph == nil) || (self.graph == NULL)) {
self.graphHostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
graph.paddingLeft = 10.0;
graph.paddingRight = 10.0;
graph.paddingTop = 10.0;
graph.paddingBottom = 10.0;
graph.title = #"Processor Usage";
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
NSTimeInterval oneMin = 60.0f;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow)
length:CPTDecimalFromFloat(oneMin*6.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(6)];
// plotting style is set to line plots
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
NSTimeInterval oneMin = 60.0f;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.graph.axisSet;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneMin);
axisSet.xAxis.minorTicksPerInterval = 0;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorTickLength = 7.0f;
//axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneMin);
//axisSet.xAxis.majorTickLineStyle = lineStyle;
//axisSet.xAxis.minorTickLineStyle = lineStyle;
//axisSet.xAxis.axisLineStyle = lineStyle;
//axisSet.xAxis.minorTickLength = 5.0f;
//axisSet.xAxis.majorTickLength = 5.0f;
//axisSet.xAxis.labelOffset = 3.0f;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Example date format : 2013-05-02 11:23:16 +0000
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
//dateFormatter.dateStyle = kCFDateFormatterShortStyle;
//dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = [NSDate dateWithTimeIntervalSince1970:([[NSDate date] timeIntervalSince1970] - (6*oneMin))];
axisSet.xAxis.labelFormatter = timeFormatter;
//axisSet.yAxis.majorIntervalLength = [NSDecimal decimalNumberWithString:#"5"];
//axisSet.yAxis.minorTicksPerInterval = 4;
//axisSet.yAxis.majorTickLineStyle = lineStyle;
//axisSet.yAxis.minorTickLineStyle = lineStyle;
//axisSet.yAxis.axisLineStyle = lineStyle;
//axisSet.yAxis.minorTickLength = 5.0f;
//axisSet.yAxis.majorTickLength = 5.0f;
//axisSet.yAxis.labelOffset = 3.0f;
CPTScatterPlot *xSquaredPlot = [[CPTScatterPlot alloc]
initWithFrame:self.graph.bounds];
xSquaredPlot.identifier = #"X Squared Plot";
CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
xSquaredPlot.dataLineStyle = dataLineStyle;
xSquaredPlot.dataSource = self;
[self.graph addPlot:xSquaredPlot];
self.graphHostView.allowPinchScaling = NO;
self.graphHostView.hostedGraph = self.graph;
[self.view addSubview:self.graphHostView];
}
}
#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [dictHealth count];
}
-(NSNumber *) numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index {
Health_MemUse *healthData = (Health_MemUse *) [dictHealth objectForKey: [self.arrayHealthKeys objectAtIndex:index]];
if ( fieldEnum == CPTScatterPlotFieldX ) {
return [NSDecimalNumber numberWithFloat:[healthData.currDate timeIntervalSince1970]];
/*
return [NSNumber numberWithDouble:([healthData.currDate timeIntervalSince1970] + [[NSTimeZone localTimeZone] secondsFromGMT])];
*/
} else {
NSLog(#"Plot memuse %f on date %# for index %d",
healthData.memUse, healthData.currDate, index);
return [NSNumber numberWithFloat:(healthData.memUse/(1024*1024*1024))]; // convert bytes to GB
}
}
Please help! I realize I might mostly be wrong in setting the range for the axes, but I tried Googl'ing, Stackoverflow'ing and tried several variations to it, all in vain.
I have to draw multiple graphs .I have to consider one Y Values to red, blue graphs and Y2 Axis values for green graph.I am using core plat library ......I done something like below and getting graph like below image .But i need set different values for y and y2. I am not getting how to draw...please help me...
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(self.xRangeMinVal) length:CPTDecimalFromInt(self.xRangeMaxVal)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(self.yRangeMinVal) length:CPTDecimalFromInt(self.yRangeMaxVal)];
//plotSpace.allowsUserInteraction = YES;
//plotSpace.delegate = self;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:2.0f], nil];
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.4] colorWithAlphaComponent:0.4];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.4] colorWithAlphaComponent:0.1];
CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
textStyle.color = [CPTColor blackColor];
textStyle.fontSize = 16.0f;
textStyle.textAlignment = CPTTextAlignmentCenter;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 3.0;
axisLineStyle.lineCap = kCGLineCapRound;
// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(#"2.0");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0.0");
x.minorTicksPerInterval = 1;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
//x.preferredNumberOfMajorTicks=;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
//x.title = [NSString stringWithFormat:#"goat"];
//x.titleOffset = 10.0;
//x.titleLocation = CPTDecimalFromString(#"0.0");
x.title = self.xLineTitle;
x.axisLineStyle = axisLineStyle;
x.titleTextStyle = textStyle;
CPTMutableLineStyle *dottedStyle=[CPTMutableLineStyle lineStyle];
x.minorGridLineStyle=dottedStyle;
// Label y with an automatic label policy.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
// y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"-10.0");
y.minorTicksPerInterval = 2;
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
//y.labelOffset = 1.0;
y.title = self.yLineTitle;
y.titleTextStyle = textStyle;
y.axisLineStyle = axisLineStyle;
y.titleRotation = M_PI * 0.5;
y.minorGridLineStyle=dottedStyle;
CPTXYPlotSpace *plotSpace1 = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
// CPTXYPlotSpace *plotSpace1 = [[[CPTXYPlotSpace alloc] init] autorelease];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(self.xRangeMinVal) length:CPTDecimalFromInt(self.xRangeMaxVal)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(60)];
CPTXYAxis *y2 = [[[CPTXYAxis alloc] init] autorelease];
y2.coordinate = CPTCoordinateY;
y2.plotSpace = plotSpace1;
y2.majorGridLineStyle = majorGridLineStyle;
y2.minorGridLineStyle = minorGridLineStyle;
y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(self.xRangeMaxVal);
y2.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
//y2.separateLayers = NO;
y2.preferredNumberOfMajorTicks = 7;
y2.minorTicksPerInterval = 2;//
y2.tickDirection = CPTSignPositive;
y2.axisLineStyle = axisLineStyle;
//y2.majorTickLength = 6.0;
y2.majorTickLineStyle = axisLineStyle;
//y2.minorTickLength = 4.0;
y2.title = #"Y2 Axis";
//y2.titleTextStyle = axisTitleTextStyle;
y2.titleOffset = 40.0;
//graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
self.graph.axisSet.axes = [NSArray arrayWithObjects:x, y,y2,nil];
Sorry you can't add multiple x or y axis in one plot space.
so whatever you want to do is just make dynamic y or x-axis in your plot.
Just calculate that there is maximum value of y-axis is this and for x-axis this.
but you must have to use only one y-axis and x-axis for same plot space.
The problem may be that you are adding both plot spaces to self.graph.defaultPlotSpace. Try using the addPlotSpace Method in CPTGraph. And also try using a unique CPTPlotSpace identifier attribute for each of your CPTPlotSpaces.
I'm attempting to load a CPXYGraph into the detail view of a split view controller. I'm getting an EXC_BAD_ACCESS when it attempts to display the plot data.
I create a new project based on "Split View-based application". After adding the CorePlot framework I make the following modifications:
1- add a GraphController (.m, .h and .xib). The xib contains a UIView with a subordinate view of type CPLayerHostingView.
2- add the following line to the app delegate didFinishLaunchingWithOptions
[detailViewController performSelector:#selector(configureView) withObject:nil afterDelay:0];
3- add the following to DetailViewController configureView
CGRect graphFrame = CGRectMake(0, 43, 662, 450);
GraphController *graphController = [[[GraphController alloc]
initWithNibName:#"GraphController" bundle:nil] autorelease];
[graphController.view setFrame:graphFrame];
[self.view addSubview:graphController.view];
[graphController reloadData];
4- the reloadData method in GraphController is pretty much pasted from one of the CorePlot samples (DatePlot) and I will copy and paste (most of) it here-
-(void)reloadData
{
if (!graph)
{
[self parentViewController];
[self.view addSubview:layerHost];
// Create graph from theme
graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:#"Dark Gradients"];
[graph applyTheme:theme];
....
[layerHost setHostedLayer: graph];
....
// Setup scatter plot space
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow) length:CPDecimalFromFloat(oneDay*5.0f)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(3.0)];
// Axes
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromFloat(oneDay);
x.orthogonalCoordinateDecimal = CPDecimalFromString(#"2");
x.minorTicksPerInterval = 0;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
x.labelFormatter = timeFormatter;
CPXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPDecimalFromString(#"0.5");
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPDecimalFromFloat(oneDay);
// Create a plot that uses the data source method
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = #"Date Plot";
dataSourceLinePlot.dataLineStyle.lineWidth = 3.f;
dataSourceLinePlot.dataLineStyle.lineColor = [CPColor greenColor];
dataSourceLinePlot.dataSource = self;
**[graph addPlot:dataSourceLinePlot];**
// Add some data
NSMutableArray *newData = [NSMutableArray array];
NSUInteger i;
for ( i = 0; i < 5; i++ ) {
NSTimeInterval x = oneDay*i;
id y = [NSDecimalNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
[newData addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPScatterPlotFieldX],
y, [NSNumber numberWithInt:CPScatterPlotFieldY],
nil]];
}
plotData = newData;
}
}
The offending line is [graph addPlot:dataSourceLinePlot]; If I comment this out the simulator comes up and displays the x and y axis of the graph and of course no data. Adding this line back causes the following SIGART-
2010-09-15 14:35:58.959 SplitViewWithCorePlot[17301:207] relabel <<CPScatterPlot: 0x4c458c0> bounds: {{0, 0}, {558, 386}}>
Program received signal: “EXC_BAD_ACCESS”.
Can anyone help?
It doesn't look like you're retaining the data array anywhere. Try changing the last statement to
plotData = [newData retain];
or, if you have a property defined for it,
self.plotData = newData;
Eric