Showing images on Scattering Graph as plot symbols in Core plot iOS

前提是你 提交于 2019-12-08 09:21:37

问题


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.


回答1:


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.... :)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/11362277/showing-images-on-scattering-graph-as-plot-symbols-in-core-plot-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!