CorePlot drawing a legend swatch in the middle of the legend text

12 views
Skip to first unread message

Laurynas Letkauskas

unread,
Jul 16, 2019, 6:06:20 PM7/16/19
to coreplot-discuss
Hey, I am banging my head into the wall, I can't get the swatch to be drawed at the center-top of the legends text, I am implementing this method:

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

}


Why would I need to draw a swatch? Well, because I need the swatch to be drawn at the top of the legend, and by default the legend can be aligned only left to right. As I manually draw, I always get the swatched placed in different places depending on the device, attributed text, etc.. If there is any solution for drawing the swatch in the middle and top of the text, I would be very delighted...

Screenshot 2019-07-16 at 17.42.18.png


Screenshot 2019-07-16 at 17.41.37.png

Here are some photos that represent my problem, I can't understand how this happens, but they both have the same amount of legends that are drawn almost at the same position, so why do the swatches differ?


Also, here is how I configure the legend:


    [_graph addPlot:_pieChart];

    CPTLegend *legend = [CPTLegend legendWithGraph:_graph];

    legend.delegate = self;

    legend.numberOfRows = 1;

    legend.numberOfColumns = 4;

    legend.columnMargin = 1.0;

    _graph.legend = legend;

    [_graph.legend setHidden:YES];

    _graph.legendAnchor = CPTRectAnchorBottom;

    _graph.legendDisplacement = CGPointMake(-10, 0);



Eric

unread,
Jul 17, 2019, 7:51:19 PM7/17/19
to coreplot-discuss
I just pushed an update to the release-2.3 branch that adds CPTLegendSwatchLayoutTop and CPTLegendSwatchLayoutBottom options to place the swatches above or below the titles, respectively. If all you want is a rectangular swatch, you don't need custom drawing code any more.

Eric 

Laurynas Letkauskas

unread,
Jul 18, 2019, 1:58:00 AM7/18/19
to coreplot-discuss
That is a life-saver, but my podfile doesn't seem to see the update

Eric

unread,
Jul 18, 2019, 6:47:08 AM7/18/19
to coreplot-discuss
I did a quick test and this pod file entry worked for me:

pod 'CorePlot', :git => 'https://github.com/core-plot/core-plot.git', :branch => 'release-2.3'
Reply all
Reply to author
Forward
0 new messages