One way to do this is to use CPTScatterPlot:
Add the following lines to your code after initialization and add your graph (or whatever your actual data graph is) to your graph.
// Before following code, initialize your data, actual data plot and add plot to graph CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease]; CPTMutableLineStyle * lineStyle = [CPTMutableLineStyle lineStyle]; lineStyle.lineWidth = 3.f; lineStyle.lineColor = [CPTColor blackColor]; lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:3.0f], [NSNumber numberWithFloat:3.0f], nil]; dataSourceLinePlot.dataLineStyle = lineStyle; dataSourceLinePlot.identifier = @"horizontalLineForAverage"; dataSourceLinePlot.dataSource = self; [barChart addPlot:dataSourceLinePlot toPlotSpace:plotSpace];
Then add the data source methods, in my case I installed the data source in the code above, so I define the data source methods in the same file:
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { // Note this method will return number of records for both my actual plot, and for scattered plot which is used to draw horizontal average line. For latter, this will decide the horizontal length of your line return [myDataArray count]; } -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { NSDecimalNumber *num = nil; // If method is called to fetch data about drawing horizontal average line, then return your generated average value. if( plot.identifier==@ "horizontalLineForAverage") { if(fieldEnum == CPTScatterPlotFieldX ) { // this line will remain as it is num =(NSDecimalNumber *)[NSDecimalNumber numberWithDouble:index]; } else { num = (NSDecimalNumber *) myDataAverageValue;// Here you generate average value for location of horizontal line. You should edit this line only; } } // handle other cases and return data for other plots return num; }
user517491
source share