Search code examples
cocoacore-plot

Coreplot Legend drawing outside of plot frame


I have chart for OS X app that can be resized with the window. I expected when the width was reduced enough the legend would be truncated or clipped. However, it is spilling outside of the plot area as shown below. Ideally, I would like the legend to truncate or at least clip the contents. how can this be done?

My legend setup is as follows

- (void)configureLegend
{
    // 1 - Get graph instance
    CPTGraph *graph = self.graphHostingView.hostedGraph;
    // 2 - Create legend
    CPTLegend *theLegend;
    if (!theLegend) {
        theLegend = [CPTLegend legendWithGraph:graph];
    }

    //Configure Text
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor colorWithComponentRed:0.612f green:0.612f blue:0.612f alpha:1.00f];
    textStyle.fontName = @"HelveticaNeue";
    textStyle.fontSize = 12.0f;
    theLegend.textStyle = textStyle;

    // 3 - Configure legend
    theLegend.numberOfColumns = 1;
    theLegend.fill = nil;
    theLegend.borderLineStyle = nil;
    theLegend.swatchSize = CGSizeMake(10.0, 10.0);
    theLegend.swatchCornerRadius = 5.0f;
    // 4 - Add legend to graph
    graph.legend = theLegend;
    graph.legendAnchor = CPTRectAnchorLeft;
    CGFloat viewWidth = self.graphHostingView.bounds.size.width;
    CGFloat legendPadding = (viewWidth * 0.3) + self.pieChart.pieRadius + (viewWidth * 0.05);
    graph.legendDisplacement = CGPointMake(legendPadding, 0.0);
}

Legend Not Clipping


Solution

  • Make sure the graph is masking its sublayers. Use masksToBounds to clip to the outside of the border line or masksToBorder to clip to the inside edge of the border.