2012-05-24 57 views
3

我需要使用CorePlot繪製日期與數字圖形的幫助。我已經檢出了DatePlot。但我的要求有點不同,如下所示。iOS CorePlot - 在x軸上有日期和在y軸上有雙數的線圖

我有一個對象數組,其中每個對象都有一個NSDate和一個雙數。 對於離: 陣列5個的對象:(在的NSDate格式YYYY-MM-DD)

  • Object1 - 2012-05-01 - 10.34
  • Object2的 - 2012-05-02 - 10.56
  • Object3 - 2012-05-03 - 10.12
  • Object4 - 2012-05-04 - 10.78
  • Object5 - 2012-05-05 - 10.65

該數據來自服務和每次都會有所不同。

請指教。

回答

4

我用一個CPTScatterPlot來顯示像你這樣的時間序列數據的圖表。

您需要創建一個數據源類,它在繪製圖形時將通過核心繪圖進行查詢。我的數據源對象包含具有兩個屬性的NSArray對象:observationDateobservationValue。該課程必須實施CPTPlotDataSource協議。這些協議的方法,我實現的:

#pragma mark- CPPlotDataSource protocol methods 
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
    // return the number of objects in the time series 
    return [self.timeSeries count]; 
} 

-(NSNumber *)numberForPlot:(CPTPlot *)plot 
        field:(NSUInteger)fieldEnum 
       recordIndex:(NSUInteger)index 
{ 
    NSNumber * result = [[NSNumber alloc] init]; 
    // This method returns x and y values. Check which is being requested here. 
    if (fieldEnum == CPTScatterPlotFieldX) 
    { 
    // x axis - return observation date converted to UNIX TS as NSNumber 
    NSDate * observationDate = [[self.timeSeries objectAtIndex:index] observationDate]; 
    NSTimeInterval secondsSince1970 = [observationDate timeIntervalSince1970]; 
    result = [NSNumber numberWithDouble:secondsSince1970]; 
    } 
    else 
    { 
    // y axis - return the observation value 
    result = [[self.timeSeries objectAtIndex:index] observationValue]; 
    } 
    return result; 
} 

注意,我的日期轉換爲雙 - 日期不能直接繪製。我在類上實現了其他方法來返回值,例如時間序列的開始和結束日期以及最小/最大值 - 這些在配置圖的繪圖空間時非常有用。

一旦你初始化你的數據源,然後將其分配給您的CPTScatterPlot的DataSource屬性:

... 
CPTXYGraph * myGraph = [[CPTXYGraph alloc] initWithFrame:self.bounds]; 

// define your plot space here (xRange, yRange etc.) 
... 

CPTScatterPlot * myPlot = [[CPTScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.accessibilityFrame]; 

// graphDataSource is your data source class 
myPlot.dataSource = graphDataSource; 
[myGraph addPlot:myPlot]; 
... 

看一看的CPTTestApp核心情節下載用於配置圖和plotspace的細節。如果您需要更多詳細信息,請詢問。祝你好運!

+0

它工作。非常感謝。 –