Add multiple annotations to a Candlestick chart
I have a candle stick plot for a financial time series. Lets say there are 100 candles on the chart, based on certain criteria I short-list 20 points to annotate (i.e. show an image near the corresponding candle). All 20 points should carry the annotation simultaneously. A single annotation can be added, like so :
//the image to display UIImage *flag = [UIImage imageNamed:@"flag.png"]; CPTImage *flagImage = [CPTImage imageWithCGImage:flag.CGImage scale:flag.scale]; CPTBorderedLayer *borderedLayer = [[CPTBorderedLayer alloc] init]; CPTMutableLineStyle *blackLineStyle = [CPTMutableLineStyle lineStyle]; [blackLineStyle setLineColor:[CPTColor blackColor]]; [blackLineStyle setLineWidth:1.0f]; [borderedLayer setBorderLineStyle:blackLineStyle]; [borderedLayer setFill:[CPTFill fillWithImage:flagImage]]; //the 'i' in the next statement is from a 'for' loop iterating over NSArrays: xCoordinates & yCoordinates, containing the corresponding coordinates for short-listed data points NSArray *anchorPoint = [NSArray arrayWithObjects:[xCoordinates objectAtIndex:i], [yCoordinates objectAtIndex:i], nil]; CPTPlotSpaceAnnotation *alertAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:[[[self hostView] hostedGraph] defaultPlotSpace] anchorPlotPoint:anchorPoint]; [alertAnnotation setContentLayer:borderedLayer]; [alertAnnotation setDisplacement:CGPointMake(0.0f, 10.0f)]; [[[[self hostView] hostedGraph] plotAreaFrame] addAnnotation:alertAnnotation];
The problem is that only one CPTPlotSpaceAnnotation can be hosted at a time, so each time I loop through and perform addAnnotation: again the previous annotation is lost. Based on my research it figures that a CPTLayer (or subclass) has to be added for each annotation separately. Even after multiple attempts, I am unable to correctly add a layer and a corresponding image annotation to it on the chart. I unsuccessfully added multiple CPTLayers (using calls to addSublayer:) but they were not part of the Plot-space (i.e. they did not zoom or pan with the chart, which is the desired behaviour in my case)
Can someone help with how to achieve this behaviour? If needed, I will be happy to furnish more information about the problem/code.
There is no limit to the number of annotations you can add to a layer. Set the size of the annotation content layer to make sure it is visible. Create it with -initWithFrame: or set the bounds later.