IOS CorePlot - a line chart with a date on the x axis and double numbers on the y axis

I need help plotting a date and number using CorePlot. I already checked DatePlot. But my requirement is slightly different from the following.

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

  • 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 the service and will be different every time.

Please inform.

+3
source share
1 answer

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

, . 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;
}

, , . , , min/max - PlotSpace .

, dataSource 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];
...

CPTTestApp . , , . !

+4

All Articles