Core plot : Unable to set date/minute in x-axis - iphone

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.

Related

scatterplot with Xcode for iPhone data not sync with axis and major thick not shown

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

How to dynamically change Core Plot‘s label

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.

Core plot : ios show two y axis and ScaterPlot and Bar Plot in Same CPTGraph

Hi please guide me write way to show two Y axis (left side and right side) with one x axis using core plot , and also i have to draw Scaterplot and barplot both in single CPTGraph.
CPTScatter plot show on right side Y axis with -ve direction .
My Code snippet
-(void)configureAxes
{
// 1 - Configure styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = #"Helvetica-Bold";
axisTitleStyle.fontSize = 8.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = #"Helvetica-Bold";
axisTextStyle.fontSize = 8.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 the graph's axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure the x-axis
// axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
// axisSet.xAxis.title = #"";
// axisSet.xAxis.labelAlignment = CPTAlignmentRight;
// axisSet.xAxis.titleTextStyle = axisTitleStyle;
// axisSet.xAxis.titleOffset = 38.0f;
// axisSet.xAxis.axisLineStyle = axisLineStyle;
// axisSet.xAxis.labelTextStyle = axisTextStyle;
CGFloat dateCount = [self.arrayData count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
NSInteger i = 0;
for (ResolutionData *d in self.arrayData)
{
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[[d.appName componentsSeparatedByString:#"-"]objectAtIndex:0] textStyle:axisSet.xAxis.labelTextStyle];
// [label setRotation:45.0];
CGFloat location = i++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = axisSet.xAxis.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
axisSet.xAxis.axisLabels = xLabels;
axisSet.xAxis.majorTickLocations = xLocations;
// 4 - Configre the y-axis
CPTAxis *y = axisSet.yAxis;
y.title = #"count";
y.labelAlignment = CPTAlignmentCenter;
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -40.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 16.0f;
y.majorTickLineStyle = axisLineStyle;
// y.majorTickLength = 4.0f;
// y.minorTickLength = 2.0f;
y.tickDirection = CPTSignPositive;
NSInteger majorIncrement = 20;
NSInteger minorIncrement = 10;
CGFloat yMax = 100.0f; // should determine dynamically based on max price
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
int x =1;
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
NSUInteger mod = j % majorIncrement;
if (mod == 0) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"%i", x] textStyle:y.labelTextStyle];
[label setAlignment:CPTAlignmentTop];
NSDecimal location = CPTDecimalFromInteger(j);x++;
label.tickLocation = location;
label.offset = -y.majorTickLength - y.labelOffset;
if (label) {
[yLabels addObject:label];
}
[yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
} else {
[yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
}
}
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;
}
Please help me.
Create a new y-axis object, configure it as desired, and add it to the axis set.
axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
If you want a different scale for the right y-axis, you need another plot space. Create a new one, configure it, set the xRange to the same xRange as the default plot space, and add it to the graph. Make sure the new y-axis has its plotSpace set to the new plot space object.
CPTXYPlotSpace *oldPlotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
CPTXYPlotSpace *newPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
newPlotSpace.xRange = oldPlotSpace.xRange;
newPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:/* location */
length:/* length */];
[graph addPlotSpace:linearPlotSpace];
See the "Axis Demo" and "Plot Space Demo" in the Plot Gallery example app for sample code.

Core-plot - plotting very small numbers on scatter plot

On scatter plot, my x-coordinate data is just long numbers representing time in seconds since epoch whereas y-coordinate data looks like this: 0.675088, 0.670629, 0.669599. What I want to be showing as major ticks on the y axis is something like this: 0.64, 0.65, 0.66, 0.67 but what is currently shown on the chart is: 0.5, 0.7, 1.0, 1.2.
First of all, I don't know why I'm getting irregular tick interval as mentioned above (0.5 to 0.7 = 0.2, 0.7 to 1.0 = 0.3?) but I'm guessing because I was experimenting with the labeling policy and have set it to CPTAxisLabelingPolicyEqualDivisions - can someone please explain what all of these labeling policies mean?
The real question is what sort of value should I be using for majorIntervalLength on the y axis considering I'm plotting very small numbers? At the moment I have 0.01 but as I adjust this value by order of magnitude of -n, it doesn't actually make any difference to my chart.
This is a snippet of my code based on Plot_Gallery_iOS DatePlot.m
-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme
{
CGRect bounds = layerHostingView.bounds;
NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:0];
NSTimeInterval oneDay = 24 * 60 * 60;
CPTGraph *graph = [[[CPTXYGraph alloc] initWithFrame:bounds] autorelease];
[self addGraph:graph toHostingView:layerHostingView];
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
[self setTitleDefaultsForGraph:graph withBounds:bounds];
[self setPaddingDefaultsForGraph:graph withBounds:bounds];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
NSTimeInterval threeMonthsago = now - (90*oneDay);
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(threeMonthsago) length:CPTDecimalFromFloat(now - threeMonthsago)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.6f) length:CPTDecimalFromFloat(0.7f)];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromFloat(oneDay*30);
x.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.6f);
x.minorTicksPerInterval = 7;
x.labelFormatter = timeFormatter;
x.labelRotation = M_PI / 4;
x.preferredNumberOfMajorTicks = 5;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromDouble(0.001);
y.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
y.preferredNumberOfMajorTicks = 5;
y.minorTicksPerInterval = 10;
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(now);
// y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.6f) length:CPTDecimalFromFloat(0.1f)];
CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = #"Date Plot";
// [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]];
CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
lineStyle.lineWidth = .5f;
lineStyle.lineColor = [CPTColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
// Auto scale the plot space to fit the plot data
// Extend the ranges by 30% for neatness
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]];
CPTMutablePlotRange *xRange = [[plotSpace.xRange mutableCopy] autorelease];
CPTMutablePlotRange *yRange = [[plotSpace.yRange mutableCopy] autorelease];
[xRange expandRangeByFactor:CPTDecimalFromDouble(1.3)];
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.3)];
plotSpace.xRange = xRange;
plotSpace.yRange = yRange;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
}
UPDATE:
Solved the problem by setting custom y major ticks by doing this:
y.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
NSSet *majorTickLoc = [NSSet setWithObjects:[NSDecimalNumber numberWithFloat:0.64f], [NSDecimalNumber numberWithFloat:0.65f], [NSDecimalNumber numberWithFloat:0.66f], [NSDecimalNumber numberWithFloat:0.67f],[NSDecimalNumber numberWithFloat:0.68f],nil];
y.majorTickLocations = majorTickLoc;
But the number still appeared to be rounded i.e. having only one fraction digit 0.6, 0.7, 0.7 etc and turns out that's just the labelling. Setting number formatter on the label works:
NSNumberFormatter *yFormatter = [[NSNumberFormatter alloc] init];
[yFormatter setMinimumFractionDigits:4];
[yFormatter setMaximumFractionDigits:4];
[yFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
y.labelFormatter = yFormatter;
Try this following code :
y.majorIntervalLength = CPTDecimalFromString(#"0.1")
Set the labelFormatter for the y-axis.
NSNumberFormatter *newFormatter = [[[NSNumberFormatter alloc] init] autorelease];
newFormatter.minimumIntegerDigits = 1;
newFormatter.maximumFractionDigits = 2;
newFormatter.minimumFractionDigits = 2;
y.labelFormatter = newFormatter;

Problems Running Core Plot Tutorial

I am following the tutorial here about core plot here....
http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application
I am getting errors with the following lines of code
//SAYING INCOMPATIBLE TYPE FOR AURGUMENT 1 'setMajorIntervalLength'
axisSet.xAxis.majorIntervalLength = [NSDecimalNumber decimalNumberWithString:#"5"];
// request for member 'axisLabelOffset' in something not a structure or union
axisSet.xAxis.axisLabelOffset = 3.0f;
//request for member 'bounds' in something not a structure or union
CPScatterPlot *xSquaredPlot = [[[CPScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.bounds] autorelease];
Here is my code now I am not getting any compiler errors anymore but its crashing and not loading the view please take a look if you can
#implementation FirstCorePlotViewController
(void)viewDidLoad
{
[super viewDidLoad];
graph = [[CPXYGraph alloc] initWithFrame: self.view.bounds];
CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
hostingView.hostedLayer = graph;
graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 20.0;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-6)
length:CPDecimalFromFloat(12)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-5)
length:CPDecimalFromFloat(30)];
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor blackColor];
lineStyle.lineWidth = 2.0f;
axisSet.xAxis.majorIntervalLength =CPDecimalFromString(#"5");
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPDecimalFromString(#"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 = 7.0f;
axisSet.yAxis.labelOffset = 3.0f;
CPScatterPlot *xSquaredPlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
xSquaredPlot.identifier = #"X Squared Plot";
xSquaredPlot.dataLineStyle.lineWidth = 1.0f;
xSquaredPlot.dataLineStyle.lineColor = [CPColor redColor];
xSquaredPlot.dataSource = self;
[graph addPlot:xSquaredPlot];
CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
//xSquaredPlot.defaultPlotSymbol = greenCirclePlotSymbol;
CPScatterPlot *xInversePlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
xInversePlot.identifier = #"X Inverse Plot";
xInversePlot.dataLineStyle.lineWidth = 1.0f;
xInversePlot.dataLineStyle.lineColor = [CPColor blueColor];
xInversePlot.dataSource = self;
[graph addPlot:xInversePlot];
}
-(NSUInteger)numberOfRecords
{
return 51;
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
double val = (index/5.0)-5;
if(fieldEnum == CPScatterPlotFieldX)
{
return [NSNumber numberWithDouble:val];
}
else
{
if(plot.identifier == #"X Squared Plot")
{
return [NSNumber numberWithDouble:val*val];
}
else
{
return [NSNumber numberWithDouble:1/val];
}
}
}
#end
None of these errors are caused by #import problems. That tutorial is known to be somewhat out of date and some parts of the Core Plot framework have changed.
The majorIntervalLength property expects an NSDecimal, not NSDecimalNumer. Core Plot includes several utility functions that convert other types to NSDecimal such as CPDecimalFromString and CPDecimalFromDouble.
axisSet.xAxis.majorIntervalLength = CPDecimalFromString(#"5");
The axisLabelOffset property has been renamed to labelOffset.
The third error is caused by two things. Both UIView and CPLayer (the root class for all Core Plot layers) having an -initWithFrame: method. Because -alloc returns an id, the compiler doesn't know which -initWithFrame: to use and sometimes guesses wrong. You can fix it with a typecast. Also, plot spaces are not layers; just get the bounds of the graph.
CPScatterPlot *xSquaredPlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
// request for member 'axisLabelOffset' in something not a structure or union
... means that the complier doesn't recognize the name provided in the dot syntax as belonging to the object. Typos are a common cause of this error. Another is not properly importing the header for class preceding the dot.
//SAYING INCOMPATIBLE TYPE FOR AURGUMENT 1 'setMajorIntervalLength'
This means that the property majorIntervalLength does not take a NSDecimalNumber.
I'm going to say that all your problems are caused by problems with #import statements. You not importing headers where you should be and the complier doesn't understand what symbol goes with which class.
There is a divide by zero error in your -numberForPlot:field:recordIndex: method. When index == 25, the statement 1/val will blow up.
Follow this Tute
This Ray's tutorial is really helpful.