Change legend sybol background on select


#1

Hi everyone,

I had this code that worked on version 2.2.1 and now stop working on version 2.3:

What it is not working is the background of the text label of the sybom that never changes color now.

Right now the code is like this …

- (void)legendClick:(UITapGestureRecognizer*)sender {
    
    ShinobiChart *currentChart = [self getCurrentDisplayedChart];
    CGPoint tapPoint = [sender locationInView:currentChart.legend];
    
    int selectedIndex = -1;
    
    //find the item (if any) that we hit in the legend click
    for (int i=0; i < currentChart.legend.symbols.count; i++) {
        
        CGRect symbolFrame = [[currentChart.legend.symbols objectAtIndex:i] frame];
        CGRect legendFrame = [[currentChart.legend.labels objectAtIndex:i] frame];
        
        if (CGRectContainsPoint(symbolFrame , tapPoint) || CGRectContainsPoint(legendFrame, tapPoint)) {
            
            selectedIndex = i;
            break;
        }
    }
    
    if (selectedIndex != -1) {
        
        for (int j=0; j < currentChart.legend.symbols.count; j++) {
            SChartColumnSeries *series = (SChartColumnSeries*)[[currentChart series] objectAtIndex:j];
            UILabel *clickedLabel = [currentChart.legend.labels objectAtIndex:selectedIndex];
            
            SChartDataPoint *point = [series.dataSeries.dataPoints lastObject];
            if (j == selectedIndex) {
                [self doActionForSelectedDataPoint:point inSeries:series onChart:currentChart];
                [clickedLabel setBackgroundColor:LEGEND_BACKGROUND_COLOR_ON_SELECT];
            } else {
                [clickedLabel setBackgroundColor:[UIColor clearColor]];
            }
        }
    }
}

#2

Hi Moonlace,

In the above code snippet you are looping around your labels, but each time you enter the loop you are grabbing the clicked label rather than every label in your legend individually. This means that you are setting the selected label’s background colour, but then as the loop continues, you are setting the selected label’s background colour back to clear colour.

Try replacying the following line:

UILabel *clickedLabel = [currentChart.legend.labels objectAtIndex:selectedIndex];

With this:

UILabel *clickedLabel = [currentChart.legend.labels objectAtIndex:j];

Thanks,
Jan


#3

Thx for the help :slight_smile: