Search code examples
iosobjective-ccore-plot

Plot area padding to prevent symbols from being truncated. (Core Plot)


In Core Plot for iOS, is there a way to add some sort of padding to a plot area, as opposed to a plot area frame? When using plot symbols, the symbols can be truncated if they fall on an axis line or boundary of the frame. Admittedly, I understand why this is the case when plotting a non-zero sized circle as a data symbol, but I'm curious if there's a way to add some padding to prevent this.

Or maybe there's a way to do with the axis ranges, but if so I haven't figured out the magic combination other than creating axis ranges that are "magically" large enough to account for the size of these symbols.

In the attached screenshot, the x-axis represents a week. Any data point for the first day will be truncated because it's getting drawn right on the y-axis. Ideally, I'd like this data point drawn just "a bit inside". (Which really means I want the x-axis range to increase by just enough to take this in to account.)

Likewise for the y-axis. A value of '0' plots right on the x-axis.

I don't really want the axis to show a range wider than required (i.e. I don't want the y-axis to go -1, for example.)

Thank you.

enter image description here


Solution

  • Here's some sample code from the "Control Chart" demo in the Plot Gallery example app:

    // Adjust visible ranges so plot symbols along the edges are not clipped
    CPTMutablePlotRange *xRange = [[plotSpace.xRange mutableCopy] autorelease];
    CPTMutablePlotRange *yRange = [[plotSpace.yRange mutableCopy] autorelease];
    
    x.orthogonalCoordinateDecimal = yRange.location;
    y.orthogonalCoordinateDecimal = xRange.location;
    
    x.visibleRange = xRange;
    y.visibleRange = yRange;
    
    x.gridLinesRange = yRange;
    y.gridLinesRange = xRange;
    
    [xRange expandRangeByFactor:CPTDecimalFromDouble(1.05)];
    [yRange expandRangeByFactor:CPTDecimalFromDouble(1.05)];
    plotSpace.xRange = xRange;
    plotSpace.yRange = yRange;
    

    You can adjust the expansion factor as needed based on the size of the graph and plot symbols.