我用一個CPTScatterPlot
來顯示像你這樣的時間序列數據的圖表。
您需要創建一個數據源類,它在繪製圖形時將通過核心繪圖進行查詢。我的數據源對象包含具有兩個屬性的NSArray
對象:observationDate
和observationValue
。該課程必須實施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的細節。如果您需要更多詳細信息,請詢問。祝你好運!
它工作。非常感謝。 –