【问题标题】:Core Plot not displaying labels (but it displays the graph and axes fine)核心图不显示标签(但它可以很好地显示图形和轴)
【发布时间】:2015-09-13 12:16:14
【问题描述】:

我正在尝试在我的 OS X 应用程序上绘制图表,并且我已经成功绘制了图表和坐标轴:

数据(看趋势)和 y 轴刻度似乎正常。但如上所示,两个轴上都没有标签,X 轴上也没有刻度。

这是我的代码:

graph = [[CPTXYGraph alloc] initWithFrame:self.mainGraph.bounds];
graph.plotAreaFrame.paddingLeft = 50;
graph.plotAreaFrame.paddingBottom = 60;
graph.plotAreaFrame.paddingTop = 10;
graph.plotAreaFrame.paddingRight = 16;
self.mainGraph.hostedGraph = graph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace*)graph.defaultPlotSpace;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterNoStyle;
NSTimeInterval oneDay = 60 * 60 * 24;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.multiplier = @(oneDay);

NSDate *refDate = [graphDates firstObject];
timeFormatter.referenceDate = refDate;
CPTXYAxisSet *axisSet = (id)graph.axisSet;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor grayColor];
lineStyle.lineWidth = 1.0f;

axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
axisSet.yAxis.title = @"Followers";
axisSet.yAxis.titleOffset = 0;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromInt(1000);
axisSet.yAxis.minorTicksPerInterval = 9;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 4;
axisSet.yAxis.majorTickLength = 10;
axisSet.yAxis.tickDirection = CPTSignNone;
axisSet.yAxis.labelFormatter = numberFormatter;

axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
axisSet.xAxis.majorIntervalLength = CPTDecimalFromInt(oneDay * 7);
axisSet.xAxis.minorTicksPerInterval = 6;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.labelFormatter = timeFormatter;
axisSet.xAxis.labelRotation = M_PI / 4;
axisSet.xAxis.majorTickLength = 10;

我也尝试分配标签策略,但无济于事(我能得到的最好的结果就是也丢失了 Y 轴刻度)。

在数据刷新时(包括初始加载),在这段代码之后:

graphMax = followers * 1.1;
graphMin = 0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace*)graph.defaultPlotSpace;
NSTimeInterval intervalSpan = [[graphDates lastObject] timeIntervalSinceReferenceDate] - [[graphDates firstObject] timeIntervalSinceReferenceDate];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(intervalSpan)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(graphMin) length:CPTDecimalFromInt(graphMax)];

graphDates 按排序顺序保存最小和最大日期,就我而言,这分别是几天前和现在。

我的图表和我的绘图区域框架在所有边都有很多填充,所以这似乎不是常见的填充问题。还有什么导致标签不显示的原因?

【问题讨论】:

  • numberFormattertimeFormatter 是什么?它们在代码的这一点上是非空的吗?什么是情节空间的xRange?什么是oneDay
  • @EricSkroch 我已经更新了 cide。
  • 情节空间的xRange是什么?你使用的是什么版本的 OS X? El Capitan beta 上的标签存在问题(请参阅issue 210)。
  • @EricSkroch 我已经用范围更新了问题。哎呀,确切地说,我在 El Capitan,虽然它不是测试版,但它是 GM 种子。但大概就是这样!不过,我不太确定 xRange 刻度线的绘制。
  • 没有看到您的数据很难说,但似乎intervalSpan 小于oneDay。这会将第一个刻度线放在屏幕外的 x 轴上。

标签: macos core-plot osx-elcapitan


【解决方案1】:

该问题是 El Capitan 和 Core Plot 特有的,框架已解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多