Core-Plot Chart を使って iphone アプリを作っているのですが、core-plot は x,y 軸を数字として使い、x 軸を DateTime として使いたいのですが、やり方は読んだのですが、どうすれば変換できますか? Core-plot で使用するための文字列 DateTime を int 番号に変換します。
例:
2012-07-16 10:20:25
Core-Plot Chart を使って iphone アプリを作っているのですが、core-plot は x,y 軸を数字として使い、x 軸を DateTime として使いたいのですが、やり方は読んだのですが、どうすれば変換できますか? Core-plot で使用するための文字列 DateTime を int 番号に変換します。
例:
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;