Eric is an expert, and his answer is good enough. But I want to contribute with more details, which are necessary for beginners like me, having a lot of troubles putting things into proper places.
First, you need to set proper range (I did learn a great deal from Eric on that).
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:25200]];
NSTimeInterval xLow = [[dateFormatter dateFromString:@"8:30"] timeIntervalSince1970];
NSTimeInterval xHigh = [[dateFormatter dateFromString:@"12:00"] timeIntervalSince1970];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xLow)
length:CPTDecimalFromDouble(xHigh-xLow)];
Next, for fixed intervals (e.g. half an hour), use the following:
x.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
NSTimeInterval x1 = [[dateFormatter dateFromString:@"8:30"] timeIntervalSince1970];
NSTimeInterval x2 = [[dateFormatter dateFromString:@"9:30"] timeIntervalSince1970];
x.majorIntervalLength = CPTDecimalFromDouble(x2-x1);
//Label time Format:
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
x.labelFormatter = timeFormatter;
For custom labelling, replace the above two sections with the following:
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:[self.arrXValues count]];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:[self.arrXValues count]];
for (NSString *string in self.arrXValues) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:string textStyle:axisTextStyle];
NSDate *time = [dateFormatter dateFromString:string];
NSTimeInterval interval = [time timeIntervalSince1970];
label.tickLocation = CPTDecimalFromDouble(interval);
label.rotation = M_PI * 1/4;
label.offset = 0.0f;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSString stringWithFormat:@"%f", interval]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
And finally, core plot delegate method,
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)indexPath
{
if(fieldEnum == CPTScatterPlotFieldX)
{
//return [NSNumber numberWithFloat:[[self.arrXValues objectAtIndex:indexPath] floatValue]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:25200]];
NSDate *time = [dateFormatter dateFromString:[self.arrXValues objectAtIndex:indexPath]];
NSTimeInterval interval = [time timeIntervalSince1970];
return [NSNumber numberWithDouble:interval];
}
else if(fieldEnum == CPTScatterPlotFieldY) {
return [NSNumber numberWithFloat:[[self.arrYValues objectAtIndex:indexPath] floatValue]];
}
return nil;
}