移位核心绘图x轴标签

时间:2015-03-26 17:42:26

标签: ios objective-c core-plot

我使用Core Plot创建了一个图表,并使用以下方法来缩放图:

    [plotSpace scaleToFitPlots:[graph allPlots]

当数据以0值开始时,图表会完美显示 enter image description here

但是如果数据只包含高于零的值,则x轴的标签会发生偏移 enter image description here

(上图中使用的数据只有23到107之间的值)

标签不会移动,我该怎么做?

以下是代码:

    - (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 个答案:

答案 0 :(得分:1)

您可以使用axisConstraints将轴锁定到绘图区域的边缘。

x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];