I am trying to create a graph view that has annotations constantly attached to the end displaying the value of the graph. It updates in real time as a slider is moved and as the graph value changes so does the annotation text.
I put together a quick bit of code and got it working fine. However this involved pressing the bar and handling that press with:
- (void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)idx;
This allowed me to get hold of the CPTBarPlot and check against that. I would like the annotation to be there when the graph loads and remain there indefinitely. So using the above method is not feasible.
Here is my numberForPlot
method and in it I am calling a method that I would like to handle the updating of the annotation.
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
double ageNew = self.sliderOne.value - [self.budgetObject.larcNum doubleValue];
self.budgeObject = [self.budgetImpactFormula calculateBudgetImpactData:ageNew andOrganisation:self.organisationObject];
BudgetGraphObject *graphObject = [self getGraphScale];
if ((fieldEnum == CPTBarPlotFieldBarTip) && (index < 2)) {
if ([plot.identifier isEqual:@"UIPCurrent"]) {
return graphObject.uipCur;
}
else if ([plot.identifier isEqual:@"UIPProjected"]) {
[self addAnnotationToBar:plot value:[graphObject.uipProj doubleValue] atIndex:index];
return graphObject.uipProj;
}
In this code I am calling my method addAnnotationToBar: value: atIndex:
. However the application crashed and I realise it was because I was passing in a CPTPlot and not CPTBarPlot. The line crashing the app is: [plot.graph.plotAreaFrame.plotArea addAnnotation:self.uipProjectedAnnotation];
After further investigation this seems to be the best place for me to handle this up update of the annotation. Is there anyway I can get hold of the CPTBarPlots from CPTPlot?
I have used the debugger to find the following:
I am, however, unable to access this array.
Any help on how to get hold of the CPTBarPlots within a CPTPlot would be much appreciated.
Many thanks.
Don't do that in the datasource. Add the annotations to the plot when setting up the graph. You can update them later if the plot data changes.