ios - 移位的核心图 x 轴标签

标签 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;
    } 

最佳答案

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

x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];

关于ios - 移位的核心图 x 轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285303/

相关文章:

ios - 如果一部 iphone 有两个同时传输 ibeacon 信号的应用程序,它们会相互竞争吗?

objective-c - UIImageView 子类中的自定义初始值设定项 - 是否有必要覆盖 initWithImage 或 initWithFrame 初始值设定项?

ios - 使用 CTMessageCenter 发送短信 (iOS 7)

ios - Core-Plot DataLabel Z 排序

ios - 有没有好的 iPhone 图表库?

iphone - 如何在 Core Plot 中使用自定义标签添加刻度线?

ios - 是否可以在未经 iOS 许可的情况下获取用户大致位置?

ios - 调用 `FIRApp.configure()` 时首次运行应用程序崩溃

iOS 图表删除条形图条形图上的值

iphone - 在 Iphone 中邀请 facebook 好友