LinearAxis StringFormat显示DateTime?

时间:2016-03-31 01:04:51

标签: c# oxyplot

目前,我的数据显示在LineSeries中,两个轴都是数字。每个点的X值是非常小的时间间隔。我需要以自定义方式显示此值。如果可能,我想避免使用DateTimeAxis数据。我可以以特定方式格式化LinearAxis吗?

1 个答案:

答案 0 :(得分:1)

这似乎对我有用。我希望我不会在这里误解,回答感觉太简单了。

//Start point of the data (arbitrary in this example)
var startOfData = DateTime.Now.Subtract(TimeSpan.FromDays(1));

//https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
var formatString = "m.ss.fffff";

Func<double, string> labelFormatter = 
    milliseconds => 
        startOfData
            .Add(TimeSpan.FromMilliseconds(milliseconds))
            .ToString(formatString);

示例用法:

var pm = new PlotModel();
pm.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = AxisPosition.Left });
pm.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = AxisPosition.Bottom, LabelFormatter = labelFormatter });

Random random = new Random(Guid.NewGuid().GetHashCode());

var datapoints = 
    Enumerable.Range(1, 100)
        .Select(milliseconds => new DataPoint(milliseconds, random.Next(0, 10)));

var lineSeries = new OxyPlot.Series.LineSeries();
lineSeries.Points.AddRange(datapoints);

pm.Series.Add(lineSeries);

Show(pm);

这里有完整的linqpad脚本 https://github.com/kennethito/StackOverflowReferences/blob/master/Oxyplot-CustomAxis/oxyplot.linq

enter image description here