3

在使用 CorePlot 绘制日期与数字图时,我需要帮助。我已经检查了 DatePlot。但我的要求有点不同,如下所示。

我有一个对象数组,其中每个对象都有一个 NSDate 和一个 Double 数字。例如:5 个对象的数组:(格式为 yyyy-mm-dd 的 NSDate)

  • 对象 1 - 2012-05-01 - 10.34
  • 对象 2 - 2012-05-02 - 10.56
  • 对象 3 - 2012-05-03 - 10.12
  • 对象 4 - 2012-05-04 - 10.78
  • 对象 5 - 2012-05-05 - 10.65

此数据来自服务,并且每次都会有所不同。

请指教。

4

1 回答 1

4

我用 aCPTScatterPlot来显示像你这样的时间序列数据图。

您需要创建一个数据源类,它在绘制图形时将被核心图查询。我的数据源对象包含一个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;
}

请注意,我将日期转换为双精度 - 不能直接绘制日期。我在类上实现了其他方法来返回值,例如时间序列的开始和结束日期以及最小/最大值 - 这些在配置图形的 PlotSpace 时很有用。

初始化数据源后,将其分配给 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,了解配置图形和绘图空间的详细信息。如果您需要更多详细信息,请询问。祝你好运!

于 2012-05-24T21:43:03.447 回答