使用coreplot库创建的日期图上的记录略有移位

时间:2015-12-31 05:12:46

标签: ios xamarin core-plot

以下是必要的代码段,

X轴标签格式化程序,

NSDateFormatter dateFormatter = new NSDateFormatter();
dateFormatter.DateFormat = "dd/MM";
var timeFormatter = new CPTTimeFormatter(dateFormatter);

timeFormatter.ReferenceDate = NSDate.FromTimeIntervalSinceNow(0);
x.LabelFormatter = timeFormatter;

委托获取记录的方法,

public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, nuint index)
    {
        if (forFieldEnum == CPTPlotField.ScatterPlotFieldX)
            return new NSNumber((index + 1) * 86400);

        Debug.WriteLine("{0}", Data[(int)index].Rate);
        return Data[(int)index].Rate;
    }

请参阅附件截图,了解结果。您可以看到标记未与X轴对齐。第一个数据点应显示在“01/01”上,但它显示在它之前。所有其他要点都一样。

如果有人希望查看代码的任何其他部分,请告诉我。我只需要指导或线索可能导致这一记录转移的原因。我已经查看了coreplot中提供的示例代码,但没有得到任何线索。

编辑:

范围如下,

 plotSpace.XRange = new CPTPlotRange(NSNumber.FromDouble(0).NSDecimalValue, NSNumber.FromDouble(86400 * 9).NSDecimalValue);
 plotSpace.YRange = new CPTPlotRange(NSNumber.FromDouble(-1).NSDecimalValue, NSNumber.FromDouble(9).NSDecimalValue);

也尝试了,

var space = graph.DefaultPlotSpace as CPTXYPlotSpace;
space.ScaleToFitPlots(new CPTPlot [] { dataSourceLinePlot });

修改:Graph setup code

enter image description here

1 个答案:

答案 0 :(得分:0)

问题在于时间格式化程序的ReferenceDate。它正在使用当前日期和时间进行初始化,因此偏移量将在一天中变化,具体取决于设置代码的运行时间。有几种方法可以使NSDate对象与固定的时间相对应。最直接的方法是使用NSDateComponents

一些Core Plot示例应用程序,包括" Date Plot" Plot Gallery 应用程序中的演示,使用此技术生成参考日期。

此外,自动轴标签政策不适用于日期。它会选择落在" nice"刻度之间的秒数,但这与偶数天数不对应。您应该使用固定间隔策略(默认值)或允许您直接提供刻度位置的策略之一。