简体   繁体   中英

iOS CorePlot - Line Graph with Date on x-axis and double numbers on y-axis

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.

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)

  • Object1 - 2012-05-01 - 10.34
  • Object2 - 2012-05-02 - 10.56
  • Object3 - 2012-05-03 - 10.12
  • Object4 - 2012-05-04 - 10.78
  • Object5 - 2012-05-05 - 10.65

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

Please advise.

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

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:

#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.

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

...
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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM