How to show/hide the plots when tapped on legend swatch color in coreplot in ios 5

528 views Asked by At

I tried to hide/show the plots when tapped on its related swatch color in legend here is my code:

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

But the problem I am facing it here is as my graph contains 7 plots and I am representing then as 2 rows in legend. When I tapped on the first legend item (red color) in first row, red color plot is hidden. when I tap on the first item (yellow color) in second row, yellow color plot is hidden. this is working fine. If I tap on to the 3 item (blue color) in first row, again the red color plot is hiding instead of blue plot. the same happening with the second row yello color plot is hiding instead of orange.

Anyone suggest me where I am doing wrong and wts the approach to get the correct result.

1

There are 1 answers

3
Eric Skroch On

Use a legend delegate to receive notification when the user touches a legend entry. These methods were added after release 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;