我已經嘗試通過核心繪圖中的所有示例,尤其是日期繪圖示例,但仍然困在實施沿着x軸數小時的繪圖。請幫我找出我錯在哪裏。如何使用核心繪圖沿X軸繪製NSDate
問題,我面對:
- 未能與均勻分佈小時,以獲得軸(按小時計算,0900 1000,....)
- ,當我將其存儲到plotData陣列,當繪製圖形時,所有的x值都是0.我的意思是,所有的值都只繪製在y軸上。 x值不存儲。
的代碼如下:
NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:@"HH:mm a"];
NSTimeInterval oneDay = 24 * 60 * 60;
NSTimeInterval xLow = 0.0f;
//This part is used to set up the plot space.
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction=YES;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow)
length:CPDecimalFromFloat(oneDay)];
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x=axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromFloat(1*60*60);
x.minorTicksPerInterval = 0;
x.axisLineStyle = lineStyle;
x.majorTickLength = 7.0f;
//x.visibleRange=xAxisRange;
CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc]
initWithDateFormatter:dateFormatter1] autorelease];
Chirp *retValue=[someTemp objectAtIndex:0];
NSDate *tempDate=retValue.startingAt;
timeFormatter.referenceDate = tempDate;
x.labelFormatter = timeFormatter;
x.orthogonalCoordinateDecimal=CPDecimalFromString(@"0");
NSMutableArray *newData = [NSMutableArray array];
NSUInteger i;
for (i = 0; i < [someTemp count]; i++)
{
Chirp *retValue=[[Chirp alloc]init];
retValue=[someTemp objectAtIndex:i];
NSDate *tempDate=retValue.startingAt;
NSString *dateFromString=[dateFormatter1 stringFromDate:tempDate];
NSLog(@"%@", dateFromString);
id x=dateFromString;
id y=[NSDecimalNumber numberWithFloat:[retValue.chipCount floatValue]];
[newData addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
x, [NSNumber numberWithInt:CPScatterPlotFieldX],
y, [NSNumber numberWithInt:CPScatterPlotFieldY],
nil]];
}
plotData = newData;
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return plotData.count;
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
我會嘗試使用自定義標籤。但是我也面臨着存儲X值的問題。我的圖中所有的值都繪製在y軸上,也就是說,數組將所有X值存儲爲零。你能幫我找出我錯在哪裏嗎? – jeevangs 2011-04-13 04:51:39
@jeevangs您上面張貼的代碼缺少y軸的繪圖範圍。嘗試調試您的代碼,並將您的代碼與示例應用程序進行比較。您只需在您的應用程序中複製和粘貼示例代碼並根據您的要求修改代碼即可。希望這可以幫助 – 2011-04-13 06:15:35
我沒有故意發佈y軸的代碼,因爲它工作正常,並且不想放在這裏。 – jeevangs 2011-04-13 18:41:12