【问题标题】:How to show/hide the plots when tapped on legend swatch color in coreplot in ios 5在ios 5的coreplot中点击图例样本颜色时如何显示/隐藏图
【发布时间】:2013-09-18 06:28:05
【问题描述】:

点击图例中的相关色板颜色时,我试图隐藏/显示图,这是我的代码:

-(void)configureLegend
{
    // 1 - Create styles
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = [CPTColor whiteColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor whiteColor];
    axisTextStyle.fontName = @"Helvetica-Bold";
    axisTextStyle.fontSize = 11.0f;

    // Add legend
    CPTGraph *graph = self.hostView.hostedGraph;
    graph.legend                 = [CPTLegend legendWithGraph:graph];
    graph.legend.delegate = self;
    graph.legend.numberOfRows    = 2;
    graph.legend.textStyle       = axisTitleStyle;
    graph.legend.fill            = [CPTFill fillWithColor:[CPTColor darkGrayColor]];
    graph.legend.borderLineStyle = axisLineStyle;
    graph.legend.cornerRadius    = 5.0;
    graph.legend.swatchSize      = CGSizeMake(5.0, 5.0);
    graph.legendAnchor           = CPTRectAnchorBottom;
    graph.legendDisplacement     = CGPointMake(20.0, 35.0);
}


-(BOOL)legend:(CPTLegend *)legend shouldDrawSwatchAtIndex:(NSUInteger)idx forPlot:(CPTPlot *)plot inRect:(CGRect)rect inContext:(CGContextRef)context
{
    NSLog(@"\n Swatch Index = %d",idx);
    CPTGraph *graph = self.hostView.hostedGraph;
    if (CGRectEqualToRect(plot.legendRect, CGRectZero)) {
        plot.legendRect = CGRectUnion(graph.legend.frame, rect);
    }
    return !plot.hidden;
}

-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(UIEvent *)event atPoint:(CGPoint)point
{
    CPTGraph *graph = self.hostView.hostedGraph;
    if (CGRectContainsPoint(graph.legend.frame, point)) {

        CGPoint pointInLegend = CGPointMake(point.x - graph.legend.frame.origin.x, point.y - graph.legend.frame.origin.y);

        [graph.allPlots enumerateObjectsUsingBlock:^(CPTPlot *plot, NSUInteger idx, BOOL *stop) {
            if (CGRectContainsPoint(plot.legendRect, pointInLegend))
            {
                NSLog(@"\n Index Value = %d",idx);
                //here you can do whatever you need
                plot.hidden = !plot.hidden;
                [self configureLegend];
                *stop = YES;
            }
        }];
    }
    return YES;
}

但我在这里面临的问题是我的图表包含 7 个图,然后我在图例中表示为 2 行。当我点击第一行中的第一个图例项(红色)时,隐藏了红色图。当我点击第二行中的第一项(黄色)时,黄色图被隐藏。这工作正常。如果我点击第一行中的 3 项(蓝色),红色图再次隐藏而不是蓝色图。第二行黄色颜色图发生的相同情况是隐藏而不是橙色。

任何人都建议我在哪里做错了,并采用获得正确结果的方法。

【问题讨论】:

  • 嗨,你能解释一下你到底想要什么,有点清楚
  • wt 我想要的是当我点击图例样本颜色时,它的相关图应该隐藏,当我再次点击它时应该是可见的。这正是我需要的重量..

标签: core-plot ios5.1


【解决方案1】:

当用户触摸图例条目时,使用图例委托接收通知。这些方法是在 1.3 版本之后添加的:

-(void)legend:(CPTLegend *)legend legendEntryForPlot:(CPTPlot *)plot wasSelectedAtIndex:(NSUInteger)idx;
-(void)legend:(CPTLegend *)legend legendEntryForPlot:(CPTPlot *)plot wasSelectedAtIndex:(NSUInteger)idx withEvent:(CPTNativeEvent *)event;

【讨论】:

  • 我下载了Core Plot 1.3,没有这样的方法吗?
  • -(BOOL)legend:(CPTLegend *)legend shouldDrawSwatchAtIndex:(NSUInteger)idx forPlot:(CPTPlot *)plot inRect:(CGRect)rect inContext:(CGContextRef)context;此方法的索引始终为零
  • 这些方法是在 1.3 版之后添加的。您需要使用 Mercurial 提取最新的源代码或等待下一个版本。现在应该很快 Xcode 5 就出来了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多