我正在使用 Core-Plot Chart 制作 iphone 应用程序,但 core-plot 使用 x、y 轴作为数字,并且我想使用 x 轴作为 DateTime,我已经阅读了这样做的方法,但是我该如何转换将 String DateTime 转换为 int 数字,以便在 core-plot 中使用它。
例子:
2012-07-16 10:20:25
我正在使用 Core-Plot Chart 制作 iphone 应用程序,但 core-plot 使用 x、y 轴作为数字,并且我想使用 x 轴作为 DateTime,我已经阅读了这样做的方法,但是我该如何转换将 String DateTime 转换为 int 数字,以便在 core-plot 中使用它。
例子:
2012-07-16 10:20:25
假设您有一个 NSDates 数组按升序排列为“日期”。现在,我们将从它创建 NSTimeIntervals 'xValues' 数组。
NSDate * lastDate = [dates objectAtIndex:0]; // first date
for (NSDate * todaysDate in dates)
NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow]; // time Interval since first date
NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];// time Interval since current date
NSTimeInterval dateDiff = todaysDiff - lastDiff; // actual difference
[xValues addObject:[NSString stringWithFormat:@"%f",dateDiff ]];
}
现在一旦你有了这个数组,在你的核心绘图代码中,你正在创建 X 轴,做这样的事情。设置x轴的'labelFormatter'如下
CPXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = your call;
x.orthogonalCoordinateDecimal = your call;
x.minorTicksPerInterval = 5;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/YYYY"]; // you can have your date format
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = some reference date;
x.labelFormatter = timeFormatter;