我正試圖使用核心繪圖在iPhone應用程序中繪製一些圖形。下面是從App一個這樣的圖表:核心繪圖:X軸標籤在第一次繪製圖形時不會出現
正如你所看到的,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];
...
...
}
有人可以幫忙嗎?
您使用的是哪個版本的Core Plot?一些較舊的版本有這樣的問題,但它已在1.3和更高版本中修復。 –
@EricSkroch,我只使用1.3版本。當我開始編寫這個應用程序時,我最初使用的是早期版本。但是現在,上個月,我升級到了最新版本。問題依然存在。你可以請指導如何解決它? – Jean
我誤解了你的問題。請參閱下面我的詳細答案。 –