1

I have a requirement of showing images at the plot symbols in my scattering Graph. Right now I am using CPTPlotSymbol to plot symbols on the graph. But all I can get with it are the predefined objects. Eg. ellipsePlotSymbol, dashPlotSymbol, trianglePlotSymbol.

But I need to plot an image instead.

Could you help me in acheiving this. Thanks a lot.

enter image description here

mayuur
  • 4,736
  • 4
  • 30
  • 65

3 Answers3

4

Got the answer after trying a bit.

First of all, I built a Custom CPTLayer and inside it used the image to be filled.

CustomImageForScatterPlot.h file

 #import "CPTLayer.h"


 @interface CustomImageForScatterPlot : CPTLayer
 {    
     UIImage *_image;

 }
 -(id)initWithImage:(UIImage *)image;

 @end

CustomImageForScatterPlot.m file

#import "CustomImageForScatterPlot.h"
#import "CPTLayer.h"

@implementation CustomImageForScatterPlot

-(void)dealloc{

    [_image release];
    [super dealloc];
}
-(id)initWithImage:(UIImage *)image{

    CGRect f = CGRectMake(0, 0, image.size.width, image.size.height);

    if (self = [super initWithFrame:f]) {

        _image = [image retain];
    }

    return self;

}

-(void)drawInContext:(CGContextRef)ctx{

    CGContextDrawImage(ctx, self.bounds, _image.CGImage);

}
@end

NOW, in the Scatter plot graph file, I used the following delegate method to return the images

- (CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{       
     CustomImageForScatterPlot* layer = [[CustomImageForScatterPlot alloc] initWithImage:[UIImage imageNamed:@"dots.png"]];
     layer.frame = CGRectMake(2, 0, 34, 6);

     return layer;        
}

ENJOY CODING.... :)

mayuur
  • 4,736
  • 4
  • 30
  • 65
  • it's not working for me.. dataLabelForPlot is called, but image doesn't appear.. any idea? – Frade May 22 '14 at 19:09
3

Use the -symbolForScatterPlot:recordIndex: datasource method and return a different symbol for the highlighted point. A standard rectangular symbol with an image fill will do what you want.

Eric Skroch
  • 27,381
  • 3
  • 29
  • 36
1

I see two different ways of accomplishing this, the first would be to use one of the default symbols, then iterate through your data and add a CPTPlotSpaceAnnotation with a CPTLayer containing an image at each point. The other way would to create a subclass of CPTPlotSymbol and override renderAsVectorInContext to draw an image.

There may be a direct way to do this that i don't know of.

Jesse Crocker
  • 863
  • 5
  • 14
  • i have this method... - (CPTPlotSymbol *)symbolForScatterPlot:(CPTScatterPlot *)plot recordIndex:(NSUInteger)index could i provide an image inside CPTPlotSymbol itself? – mayuur Jul 06 '12 at 13:18
  • The only method CPTPlotSymbol provides for creating a custom symbol is +(CPTPlotSymbol *)customPlotSymbolWithPath:(CGPathRef)aPath; So if you can make your custom symbol with a CGPathRef that will work, but otherwise i think you will need to create a subclass of CPTPlotSymbol – Jesse Crocker Jul 06 '12 at 16:59