Legend displacement

5 views
Skip to first unread message

Laurynas Letkauskas

unread,
Jul 16, 2019, 6:06:20 PM7/16/19
to coreplot-discuss
how do I implement this method for drawing swatches in the middle:


- (BOOL)legend:(CPTLegend *)__unused legend shouldDrawSwatchAtIndex:(NSUInteger)idx forPlot:(CPTPlot *)__unused plot inRect:(CGRect)rect inContext:(CGContextRef)context {

    SWGAccountInfo *account = [_dataModel.chartList objectAtIndex:idx];

    CGColorRef color = [UIColor sc_colorForPieChart:[account.balanceInfo.currency.typeCode unsignedIntegerValue]].CGColor;

    CGContextSetFillColorWithColor(context, color);

    if(_dataModel.chartList.count == 4) {

    switch (idx) {

        case 0: {

            rect = CGRectMake(48, 45, 16, -9);

            UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3.0];

            [bezierPath fill];

            CGContextFillRect(context, rect);

            return NO;

            break;

        }

        case 1: {

            NSLog(@"%f, %f", legend.position.x, legend.position.y);

            rect = CGRectMake(133, 45, 16, -9);

            UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3.0];

            [bezierPath1 fill];

            CGContextFillRect(context, rect);

            return NO;

            break;

        }

        case 2: {

            NSLog(@"%f, %f", legend.position.x, legend.position.y);

            rect = CGRectMake(224, 45, 16, -9);

            UIBezierPath *bezierPath2 = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3.0];

            [bezierPath2 fill];

            CGContextFillRect(context, rect);

            return NO;

            break;

        }

        case 3: {

            NSLog(@"%f, %f", legend.position.x, legend.position.y);

            rect = CGRectMake(315, 45, 16, -9);

            UIBezierPath *bezierPath3 = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3.0];

            [bezierPath3 fill];

            CGContextFillRect(context, rect);

            return NO;

            break;

        }

        default:

            return NO;

            break;

    }

    }

    return NO;

    


My current result is:


Screenshot 2019-07-16 at 17.41.37.png

This is nice if I test with some data, but when I use some other data I get this:

Screenshot 2019-07-16 at 17.42.18.png

Is there any possible way to get the center of the displaced legend at some index's middle so it could always be drawn at the middle of the legend


Reply all
Reply to author
Forward
0 new messages