iOS CorePlot - 在x轴上使用日期,y轴上使用双精度数字的折线图

3
我需要帮助使用CorePlot绘制日期与数字图表,我已经查看了DatePlot。但我的要求有点不同,如下所示。
我有一个对象数组,每个对象都有NSDate和Double数字。
例如:5个对象的数组:(NSDate格式为yyyy-mm-dd)
- 对象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
这些数据来自服务,并且每次可能会有所不同。
请给予建议。
1个回答

4
我使用了CPTScatterPlot来展示类似于您的时间序列数据的图表。
您需要创建一个数据源类,当Core Plot绘制图表时,该类将被查询。我的数据源对象包含具有两个属性的对象的NSArrayobservationDateobservationValue。该类必须实现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;
}

注意,我将日期转换为double类型-日期无法直接绘制。我在类上实现了其他方法,例如返回时间序列的开始和结束日期以及最小/最大值-这些在配置图形的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];
...

请查看core plot下载中的CPTTestApp,以获取有关配置图形和绘图空间的详细信息。如果需要更多信息,请询问。祝好运!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接