【问题标题】:iOS CorePlot - Line Graph with Date on x-axis and double numbers on y-axisiOS CorePlot - x 轴上的日期和 y 轴上的双数字的折线图
【发布时间】:2012-05-24 15:36:46
【问题描述】:

我在使用 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

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

请指教。

【问题讨论】:

    标签: iphone ios core-plot


    【解决方案1】:

    我使用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;
    }
    

    请注意,我将日期转换为双精度 - 无法直接绘制日期。我在该类上实现了其他方法来返回值,例如时间序列的开始和结束日期以及最小/最大值 - 这些在配置图形的 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,了解配置图表和绘图空间的详细信息。如果您需要更多详细信息,请询问。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多