0
votes

I created a chart with Core Plot and used the following method in order to scale the plot:

    [plotSpace scaleToFitPlots:[graph allPlots]

The chart shows up perfectly when the data starts with a 0 value enter image description here

But if the data contains just higher values than zero then the labels of the x-axis get shifted.
enter image description here

(The data used by the chart above has just values between 23 and 107)

What do I have to do that the labels don't shift?

Here is the code:

    - (void)viewDidLoad {
        [super viewDidLoad];

        CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
        self.chartView.hostedGraph = graph;
        CGRect rect = [self.chartView frame];
        CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] initWithFrame:rect];
        scatterPlot.dataSource = self;
        [scatterPlot setAreaBaseValue: [NSNumber numberWithInt:0]];
        [graph addPlot:scatterPlot];

        CPTTheme *theme = [CPTTheme themeNamed:kCPTSlateTheme];
        [graph applyTheme:theme];
        graph.plotAreaFrame.masksToBorder   = NO;
        graph.paddingLeft                   = 0.0f;
        graph.paddingTop                    = 0.0f;
        graph.paddingRight                  = 0.0f;
        graph.paddingBottom                 = 0.0f;
        graph.plotAreaFrame.paddingTop      = 10.0;
        graph.plotAreaFrame.paddingRight    = 10.0;
        graph.plotAreaFrame.paddingBottom   = 20.0;
        graph.plotAreaFrame.paddingLeft     = 50.0;


        // Y axis configuration
        CPTXYAxisSet *axisSet= (CPTXYAxisSet *)graph.axisSet;
        CPTXYAxis *y         = axisSet.yAxis;
        y.labelingPolicy     = CPTAxisLabelingPolicyAutomatic;


        // X axis configuration
        CPTXYAxis *x            = axisSet.xAxis;
        x.labelingPolicy        =  CPTAxisLabelingPolicyAutomatic;
        x.minorTicksPerInterval = 4;


        // X axis label formatter
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        [NSTimeZone resetSystemTimeZone];
        [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
        CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
        timeFormatter.referenceDate = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
        x.labelFormatter = timeFormatter;

        [graph.defaultPlotSpace scaleToFitPlots:[graph allPlots]];
    }



    -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
        return self.data.count;
    }

    - (id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx{

        MyData *d =  self.data[idx];
        if (fieldEnum == CPTScatterPlotFieldX){
            NSNumber *seconds = [self secondsFromDate: d.nsDate];
            return seconds;
        }else if (fieldEnum == CPTScatterPlotFieldY){
            return d.in;
        }
        return nil;
    } 
1

1 Answers

1
votes

You can use the axisConstraints to lock the axis to the edge of the plot area.

x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];