Core Plot を使用して iPhone アプリでグラフを描画しようとしています。アプリからのそのようなグラフの 1 つを次に示します。
ご覧のとおり、X 軸のラベルには 1,2,3... のラベルしか表示されていませんが、対応する日付が表示されるように構成しています。このグラフは数分ごとに再描画されます。再描画すると、以下に示すように、x 軸のラベル (つまり、日付の値) が正しく表示されます。
初めて、それはちょうど1,2,3を示しています....
X 軸ラベルを設定するにはどうすればよいですか?
グラフの他のすべてのパラメーター (色、パディングなど) を設定するときではなく、「numberOfRecordsForPlot」で実行しています。これは、グラフの色などを設定した後にのみプロットの値を取得するためです。そのため、numberOfRecordsForPlot() で、両方の軸の軸範囲を計算します (数分ごとに取得される値に応じて)また、カスタムの目盛りの位置と x 軸ラベルを計算し、そこに設定します。コード スニペットは次のとおりです。
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
...
...
NSArray *customTickLocations = [NSArray arrayWithObjects :
[NSDecimalNumber numberWithInt:0],
[NSDecimalNumber numberWithInt:1],
[NSDecimalNumber numberWithInt:2],
[NSDecimalNumber numberWithInt:3],
[NSDecimalNumber numberWithInt:4],
nil];
// assume that we are plotting 5 points at a time
NSMutableArray *xAxisLabels = [[NSMutableArray alloc] init];
for (int i=0; i<self.arrayKeys.count ; i++) {
NSString *myData = [dict objectForKey:[self.arrayKeys objectAtIndex:i]];
if ((myData == nil) || (myData == NULL)) {
NSLog(@"WARN : Invalid data");
continue;
}
NSDate *date = (NSDate *) [self.arrayKeys objectAtIndex:i];
[xAxisLabels addObject:date];
}
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:xAxisLabels.count];
CPTMutableTextStyle *textStyle = [[CPTMutableTextStyle alloc] init];
textStyle.fontSize = 10.0;
textStyle.color = [CPTColor darkGrayColor];
for (int i=0 ; i<dictHealth.count ; i++) {
NSNumber *tickLocation = [customTickLocations objectAtIndex:i];
NSDateComponents *dateComponents = [self getComponentFromDate:
(NSDate *)[xAxisLabels objectAtIndex:labelLocation++]];
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc]
initWithText:[NSString stringWithFormat:@"%d-%d-%d\n%d:%d:%d",
dateComponents.year, dateComponents.month, dateComponents.day,
dateComponents.hour, dateComponents.minute, dateComponents.second]
textStyle:textStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = 7.5;
newLabel.rotation = M_PI/3;
[customLabels addObject:newLabel];
}
axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels];
...
...
}
誰か助けてくれませんか?