2012-07-06 84 views
1

我有要求在散射圖中的散點圖符處顯示圖像。現在我正在使用CPTPlotSymbol在圖上繪製符號。但我所能得到的只是預定義的對象。例如。 ellipsePlotSymbol,dashPlotSymbol,trianglePlotSymbol。在散點圖上顯示散點圖上的圖像作爲繪圖符號iOS

但我需要繪製一個圖像,而不是。

你能幫我實現這個目標嗎?非常感謝。

enter image description here

回答

4

嘗試了一下後得到了答案。

首先,我構建了一個自定義CPTLayer,並在其中使用了要填充的圖像。

CustomImageForScatterPlot.h文件

#import "CPTLayer.h" 


@interface CustomImageForScatterPlot : CPTLayer 
{  
    UIImage *_image; 

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

@end 

CustomImageForScatterPlot.m文件

#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 

現在,在散點圖圖形文件,我用下面的委託方法返回圖像

- (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;   
} 

享受CODING .... :)

+0

它不適用於我.. dataLabelForPlot被調用,但圖像不會出現.. 任何想法? – Frade 2014-05-22 19:09:20

1

我看到完成這兩種不同的方式,第一個是使用默認的標誌之一,然後通過你的數據迭代,並添加CPTPlotSpaceAnnotation在每個點包含圖像的CPTLayer。另一種方法是創建CPTPlotSymbol的子類並重寫renderAsVectorInContext來繪製圖像。

有可能是一種直接的方式來做到這一點,我不知道。

+0

我有這樣的方法 - (CPTPlotSymbol *)symbolForScatterPlot:(CPTScatterPlot *)情節recordIndex:(NSUInteger)指數 可我提供內部CPTPlotSymbol本身的形象呢? – mayuur 2012-07-06 13:18:08

+0

CPTPlotSymbol爲創建自定義符號提供的唯一方法是+(CPTPlotSymbol *)customPlotSymbolWithPath:(CGPathRef)aPath;所以,如果你可以使用CGPathRef來創建自定義符號,但是否則我認爲你需要創建一個CPTPlotSymbol的子類 – 2012-07-06 16:59:30

3

使用-symbolForScatterPlot:recordIndex:數據源方法併爲高亮顯示的點返回不同的符號。帶圖像fill的標準矩形符號將按照您的要求進行操作。