1
votes

Problem: I can't get Core Plot (1.1) to plot automatic labels for my x-axis when using autoscaling ([plotSpace scaleToFitPlots:[graph allPlots]).

What I have tried: I changed the values for the offsets and paddings, but this did not change the result. However, when turning autoscale off (not using [plotSpace scaleToFitPlots:[graph allPlots]]and setting the y scale automatically, the automatic labeling of the x-axis works.

Question: Is there a bug in Core Plot or what did I do wrong? I would appreciate any help!

Thank you!

This is how I have set up my chart:

CPTBarPlot *barPlot = [CPTBarPlot 
   tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlot.baseValue       = CPTDecimalFromInt(0);
barPlot.barOffset       = CPTDecimalFromFloat(0.0f); // CPTDecimalFromFloat(0.5f);
barPlot.barWidth        = CPTDecimalFromFloat(0.4f);
barPlot.barCornerRadius = 4;
barPlot.labelOffset = 5;
barPlot.dataSource      = self;
barPlot.delegate        = self;
 
graph = [[CPTXYGraph alloc]initWithFrame:self.view.bounds];
   self.hostView.hostedGraph = graph;
    
graph.paddingLeft   = 40.0f;
graph.paddingTop    = 30.0f;
graph.paddingRight  = 30.0f;
graph.paddingBottom = 50.0f;
        
[graph addPlot:barPlot];
        
graph.plotAreaFrame.masksToBorder = NO;
graph.plotAreaFrame.cornerRadius  = 0.0f;
graph.plotAreaFrame.borderLineStyle = borderLineStyle;
    
double xAxisStart = 0;
CPTXYAxisSet *xyAxisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *xAxis = xyAxisSet.xAxis;
CPTMutableLineStyle *lineStyle = [xAxis.axisLineStyle mutableCopy];
lineStyle.lineCap = kCGLineCapButt;
xAxis.axisLineStyle = lineStyle;
xAxis.majorTickLength = 10;
xAxis.orthogonalCoordinateDecimal   = CPTDecimalFromDouble(yAxisStart);
xAxis.paddingBottom = 5;
        
xyAxisSet.delegate = self;
xAxis.delegate = self;
xAxis.labelOffset = 0;
         
xAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;

[plotSpace scaleToFitPlots:[graph allPlots]];
        
CPTMutablePlotRange *yRange = plotSpace.yRange.mutableCopy;
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.3)];
plotSpace.yRange = yRange;
NSInteger xLength = CPTDecimalIntegerValue(plotSpace.xRange.length) + 1;
        
plotSpace.xRange = [CPTPlotRange
plotRangeWithLocation:CPTDecimalFromDouble(xAxisStart) 
   length:CPTDecimalFromDouble(xLength)] ;

Edit

Eric's answer has helped. The x-labels are now plotted, but at the wrong y-position (offset). How can I have them plotted right below the x-axis (please refer to the screenshots below):enter image description hereenter image description here).

Thank you!

2

2 Answers

4
votes

Set the orthogonalCoordinateDecimal for the axes after scaling the plot space. Just a guess, but the the yAxisStart value used to position the x-axis might be outside the scaled range.

0
votes

I had the same issue and patched it by adding another plot with lineWidth:0.0 and clearColor at (x,y) values of (-1,-1). scaleToFit includes this plot and shows my labels and axis.