我正在尝试使用 Core Plot 在 iPhone 应用程序中绘制一些图表。这是来自应用程序的一张这样的图表:
如您所见,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];
...
...
}
有人可以帮忙吗?