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