2011-04-13 83 views
5

我已經嘗試通過核心繪圖中的所有示例,尤其是日期繪圖示例,但仍然困在實施沿着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; 
} 

回答

2

你需要爲x軸創建自定義標籤。請參閱core-plot的條形圖示例。它有自定義標籤。這樣你可以顯示小時,日期或任何你想要的東西。

+0

我會嘗試使用自定義標籤。但是我也面臨着存儲X值的問題。我的圖中所有的值都繪製在y軸上,也就是說,數組將所有X值存儲爲零。你能幫我找出我錯在哪裏嗎? – jeevangs 2011-04-13 04:51:39

+0

@jeevangs您上面張貼的代碼缺少y軸的繪圖範圍。嘗試調試您的代碼,並將您的代碼與示例應用程序進行比較。您只需在您的應用程序中複製和粘貼示例代碼並根據您的要求修改代碼即可。希望這可以幫助 – 2011-04-13 06:15:35

+0

我沒有故意發佈y軸的代碼,因爲它工作正常,並且不想放在這裏。 – jeevangs 2011-04-13 18:41:12

3

沒關係......!我找到了解決方案。我使我的x軸使用自定義標籤(如Rahul Vyas所建議的)。這解決了我面臨的一個問題。而對於另一個問題,我試圖從NSDate使用NSDateformatter和字符串獲取字符串。我張貼下面的代碼。希望它有助於某個人。 用於在x軸上使用時間自定義標籤的代碼,這在條形圖示例中已經存在。

-(void)configureXAxisForGraph:(CPXYGraph*)graphForAxis 
{ 
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet; 
CPXYAxis *x=axisSet.xAxis; 
x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue]; 
CPPlotRange *xAxisRange=[CPPlotRange plotRangeWithLocation:CPDecimalFromString(@"0.0") length:CPDecimalFromString(@"24.0")]; 
x.minorTicksPerInterval = 4; 
x.majorTickLineStyle = lineStyle; 
x.minorTickLineStyle = lineStyle; 
x.axisLineStyle = lineStyle; 
x.minorTickLength = 5.0f; 
x.majorTickLength = 7.0f; 
x.visibleRange=xAxisRange; 
x.orthogonalCoordinateDecimal=CPDecimalFromString(@"0"); 
[email protected]"Hours"; 
x.titleOffset=47.0f; 
x.labelRotation=M_PI/4; 
x.labelingPolicy=CPAxisLabelingPolicyNone; 
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:6], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:12], 
           [NSDecimalNumber numberWithInt:15], [NSDecimalNumber numberWithInt:18], [NSDecimalNumber numberWithInt:21], [NSDecimalNumber numberWithInt:24],nil]; 
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"3 AM", @"6 AM", @"9 AM", @"12PM", @"3 PM",@"6 PM",@"9 PM", @"12 AM", nil]; 
NSUInteger labelLocation = 0; 
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; 
for (NSNumber *tickLocation in customTickLocations) 
{ 
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; 
    newLabel.tickLocation = [tickLocation decimalValue]; 
    newLabel.offset = x.labelOffset + x.majorTickLength; 
    newLabel.rotation = M_PI/4; 
    [customLabels addObject:newLabel]; 
    [newLabel release]; 
} 
x.axisLabels = [NSSet setWithArray:customLabels]; 
} 

這裏是代碼對我來說繪製數值和時間(小時)的值。

self.dataToBePlotted = [NSMutableArray array]; 
NSUInteger i; 
for (i = 0; i < [dummyDataArray count]; i++) 
{ 
    Chirp *retValue=[[Chirp alloc]init]; 
    retValue=[dummyDataArray objectAtIndex:i]; 
    NSDate *tempDate=retValue.startingAt; 
    NSString *dateFromString=[dateFormatter1 stringFromDate:tempDate]; 
    NSLog(@"%@", dateFromString); 
    id x=dateFromString; 
    id y=[NSDecimalNumber numberWithFloat:[retValue.chipCount floatValue]]; 
    [self.dataToBePlotted addObject: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
     x, [NSNumber numberWithInt:CPScatterPlotFieldX], 
     y, [NSNumber numberWithInt:CPScatterPlotFieldY], 
     nil]]; 
} 
self.plotData = self.dataToBePlotted; 


-(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; 
} 
+0

非常感謝你的代碼,我有問題:你只工作一小時而不是一分鐘,對嗎?是否有可能在xaxis或日期和時間一起爲xaxis繪製折線圖 – Pooja 2011-05-25 09:39:16

+0

我想實現折線圖y值包含數字和x軸仍然我不確定,但可能會跟隨1.時間與小時,2.時間與小時+分鐘3.只有約會,仍然我不確定什麼核心情節支持....你能告訴我哪一個將支持核心情節 – Pooja 2011-05-25 09:43:49

+0

@Pooja:它通常取決於你的價值想要將圖存儲在數組中。在我的情況下,我存儲了時間戳值,我可以將其與日期和時間一起降到毫秒。在顯示自定義標籤的同時,我只需要顯示我想要的內容。 – jeevangs 2011-05-31 19:16:07