Image 如何将Core Plot中的自定义记号设置为图标?

Image 如何将Core Plot中的自定义记号设置为图标?,image,plot,core-plot,core,Image,Plot,Core Plot,Core,有没有办法让勾号主勾号成为UIImage自定义图形而不是文本? 我需要在其中一个轴中显示为图标的值。 我没有看到任何代理,但可能有人知道这个诀窍?轴标签可以有任何CPTLayer作为其内容,它是Core Animation的CALayer的直接子类。将图像设置为图层背景,并使用此图层构建自定义标签。一些核心绘图示例应用程序演示了自定义标签,尽管它们都使用文本标签 将自定义标签添加到图形中有两个选项: 使用CPTAxisLabelingPolicyNone标签策略。创建包含标签的NSSet,并将其

有没有办法让勾号主勾号成为UIImage自定义图形而不是文本? 我需要在其中一个轴中显示为图标的值。 我没有看到任何代理,但可能有人知道这个诀窍?

轴标签可以有任何CPTLayer作为其内容,它是Core Animation的CALayer的直接子类。将图像设置为图层背景,并使用此图层构建自定义标签。一些核心绘图示例应用程序演示了自定义标签,尽管它们都使用文本标签

将自定义标签添加到图形中有两个选项:

使用CPTAxisLabelingPolicyNone标签策略。创建包含标签的NSSet,并将其设置为轴上的axisLabels属性。如果使用此选项,请记住,除了自定义标签外,还必须提供主刻度和/或次刻度位置

使用任何其他标记策略生成标记位置并实现-axis:shouldUpdateAxisLabelsLocations:axis委托方法。在代理中,在提供的位置创建新标签,并返回“否”以抑制自动标签

Eric

轴标签可以将任何CPTLayer作为其内容,CPTLayer是Core Animation的CALayer的直接子类。将图像设置为图层背景,并使用此图层构建自定义标签。一些核心绘图示例应用程序演示了自定义标签,尽管它们都使用文本标签

将自定义标签添加到图形中有两个选项:

使用CPTAxisLabelingPolicyNone标签策略。创建包含标签的NSSet,并将其设置为轴上的axisLabels属性。如果使用此选项,请记住,除了自定义标签外,还必须提供主刻度和/或次刻度位置

使用任何其他标记策略生成标记位置并实现-axis:shouldUpdateAxisLabelsLocations:axis委托方法。在代理中,在提供的位置创建新标签,并返回“否”以抑制自动标签


Eric

接受Eric的问题并感谢他,我为他建议的解决方案提供了工作代码。 也许这对某人有帮助:

if (yAxisIcons) {


    int custonLabelsCount = [self.yAxisIcons count];

    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:custonLabelsCount];

    for (NSUInteger i = 0; i < custonLabelsCount; i++) {

        NSNumber *tickLocation = [NSNumber numberWithInt:i];
        NSString *file = [yAxisIcons objectAtIndex:i];
        UIImage *icon = [UIImage imageNamed:file];

        CPImageLayer *layer; // My custom CPLayer subclass - see code below

          CGFloat nativeHeight = 1;
          CGFloat nativeWidth = 1;


        if (icon) {

            layer = [[CPImageLayer alloc] initWithImage:icon];
            nativeWidth = 20;//CGImageGetWidth(icon.CGImage);
            nativeHeight = 20;//CGImageGetHeight(icon.CGImage);
            //layer.contents = (id)icon.CGImage;

            if (nativeWidth > biggestCustomIconWidth) {
                biggestCustomIconWidth = nativeWidth;
            }

        }else{
            layer = [[CPImageLayer alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        }

            CGRect  startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight);

            layer.frame = startFrame;
            layer.backgroundColor = [UIColor clearColor].CGColor;
            CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithContentLayer:layer];
            newLabel.tickLocation = [tickLocation decimalValue];
            newLabel.offset = x.labelOffset + x.majorTickLength;
            [customLabels addObject:newLabel];
            [newLabel release];
            [layer release];

    }

    y.axisLabels =  [NSSet setWithArray:customLabels];

}
CPImageLayer.m


享受接受Eric的问题并感谢他我为他建议的解决方案提供了工作代码。 也许这对某人有帮助:

if (yAxisIcons) {


    int custonLabelsCount = [self.yAxisIcons count];

    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:custonLabelsCount];

    for (NSUInteger i = 0; i < custonLabelsCount; i++) {

        NSNumber *tickLocation = [NSNumber numberWithInt:i];
        NSString *file = [yAxisIcons objectAtIndex:i];
        UIImage *icon = [UIImage imageNamed:file];

        CPImageLayer *layer; // My custom CPLayer subclass - see code below

          CGFloat nativeHeight = 1;
          CGFloat nativeWidth = 1;


        if (icon) {

            layer = [[CPImageLayer alloc] initWithImage:icon];
            nativeWidth = 20;//CGImageGetWidth(icon.CGImage);
            nativeHeight = 20;//CGImageGetHeight(icon.CGImage);
            //layer.contents = (id)icon.CGImage;

            if (nativeWidth > biggestCustomIconWidth) {
                biggestCustomIconWidth = nativeWidth;
            }

        }else{
            layer = [[CPImageLayer alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        }

            CGRect  startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight);

            layer.frame = startFrame;
            layer.backgroundColor = [UIColor clearColor].CGColor;
            CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithContentLayer:layer];
            newLabel.tickLocation = [tickLocation decimalValue];
            newLabel.offset = x.labelOffset + x.majorTickLength;
            [customLabels addObject:newLabel];
            [newLabel release];
            [layer release];

    }

    y.axisLabels =  [NSSet setWithArray:customLabels];

}
CPImageLayer.m


享受

你的回答让我走了很远,但我对在CPLayer上绘制图标有意见。正在正确绘制CPLayer,但没有图像层的内容。请参阅使用代码更新问题。当您说在提供的位置创建新标签时,您是指tickLocation属性权利?我已经根据代理位置的参数在我的代理中设置了它们,但它们都设置在位置=0。您的回答花了我很长时间,但我对在CPLayer上绘制图标有问题。正在正确绘制CPLayer,但没有图像层的内容。请参阅使用代码更新问题。当您说在提供的位置创建新标签时,您是指tickLocation属性权利?我一直在根据代理位置的参数在我的代理中设置它们,但它们都是在位置=0处设置的。
#import "CPImageLayer.h"
#import "CPLayer.h"

@implementation CPImageLayer

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