【问题标题】:Core Plot shows wrong date on x-Axis核心图在 x 轴上显示错误的日期
【发布时间】:2014-04-22 21:48:30
【问题描述】:

我在 x 轴上显示日期范围时遇到问题。此范围包含日期作为时间戳值:1398172101、1398176156、1398181026、1398196536 等。下面的代码显示了我如何尝试将日期格式化为人类可读的

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"hh:mm dd/MM/yy"];
 CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
 timeFormatter.referenceDate = [NSDate dateWithTimeIntervalSince1970:0.0];
 x.labelFormatter = timeFormatter;

我不确定我是否为timeFormatter.referenceDate 设置了正确的值。我在 stackoverflow 上发现了一个问题,它描述了相同的问题,但正确的答案对我不起作用。它告诉将[NSDate dateWithTimeIntervalSince1970:0.0] 分配给timeFormatter.referenceDate

我做错了什么?谢谢

【问题讨论】:

  • 时间戳的零 (0) 点是什么?您已将其设置为 1970 年 1 月 1 日。
  • @EricSkroch 我的每个时间戳值都是使用 [[NSDate date] timeIntervalSince1970] 生成的
  • Core Plot 代码看起来没问题。问题是什么?您是否收到格式错误的标签?偏离正确的日期和时间?
  • 日期在标签上的格式很好。问题是日期错误,例如: 03:00 03/01/70 。看年份:2070,时间和日期的其余部分也是错误的

标签: ios objective-c ios7 core-plot


【解决方案1】:

确保绘图空间的xRange 设置正确。例如,要创建一个包含问题中列出的值的范围,请执行以下操作:

NSTimeInterval minTime = 1398172101;
NSTimeInterval maxTime = 1398196536;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(minTime)
                                                length:CPTDecimalFromDouble(maxTime - minTime)];

【讨论】:

  • 你是对的。 xRange 属性的值错误。您的代码运行良好。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
相关资源
最近更新 更多