ioscoreplot - x轴上显示日期,y轴上显示双数

时间:2022-10-19 00:16:33

I need help in plotting date vs number graph using CorePlot. I have already checked out DatePlot. But my requirement is bit different which is as follows.

我需要用CorePlot来帮助绘制日期和数字图。我已经查过DatePlot了。但是我的要求有点不同,如下所示。

I have an array of objects where each object has got a NSDate and a Double number. For ex: Array of 5 objects: (NSDate in format yyyy-mm-dd)

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

  • Object1 - 2012-05-01 - 10.34
  • Object1 - 2012-05-01 - 10.34。
  • Object2 - 2012-05-02 - 10.56
  • 目的2- 2012-05-02 - 10.56
  • Object3 - 2012-05-03 - 10.12
  • 目的3 - 2012-05-03 - 10.12
  • Object4 - 2012-05-04 - 10.78
  • 目的4 - 2012-05-04 - 10.78
  • Object5 - 2012-05-05 - 10.65
  • 目的5- 2012-05-05 - 10.65

This data comes from a service and would differ every time.

这些数据来自一个服务,每次都会有所不同。

Please advise.

请建议。

1 个解决方案

#1


4  

I used a CPTScatterPlot to display a graph of time series data like yours.

我使用CPTScatterPlot来显示像您这样的时间序列数据图。

You need to create a data source class which will be queried by core plot when it is drawing the graph. My data source object contains an NSArray of objects with two attributes: observationDate and observationValue. The class has to implement the CPTPlotDataSource protocol. These are the protocol methods that I implemented:

您需要创建一个数据源类,该数据源类在绘制图形时将被core plot查询。我的数据源对象包含一个具有两个属性的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;
}

Note that I am converting the date to a double - dates cannot be plotted directly. I implement other methods on the class to return values such as the start and end dates of the time series and the min/max values - these are useful when configuring the PlotSpace of your graph.

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

Once you have initialised your data source you then assign it to the dataSource property of your CPTScatterPlot:

初始化数据源后,将其分配给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];
...

Have a look at the CPTTestApp in the core plot download for details of configuring your graph and plotspace. If you need any more details please ask. Good luck!

查看core plot下载中的CPTTestApp,了解关于配置图形和plotspace的详细信息。如果你需要更多的细节,请询问。好运!

#1


4  

I used a CPTScatterPlot to display a graph of time series data like yours.

我使用CPTScatterPlot来显示像您这样的时间序列数据图。

You need to create a data source class which will be queried by core plot when it is drawing the graph. My data source object contains an NSArray of objects with two attributes: observationDate and observationValue. The class has to implement the CPTPlotDataSource protocol. These are the protocol methods that I implemented:

您需要创建一个数据源类,该数据源类在绘制图形时将被core plot查询。我的数据源对象包含一个具有两个属性的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;
}

Note that I am converting the date to a double - dates cannot be plotted directly. I implement other methods on the class to return values such as the start and end dates of the time series and the min/max values - these are useful when configuring the PlotSpace of your graph.

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

Once you have initialised your data source you then assign it to the dataSource property of your CPTScatterPlot:

初始化数据源后,将其分配给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];
...

Have a look at the CPTTestApp in the core plot download for details of configuring your graph and plotspace. If you need any more details please ask. Good luck!

查看core plot下载中的CPTTestApp,了解关于配置图形和plotspace的详细信息。如果你需要更多的细节,请询问。好运!