如何使用 Livecharts 使 x 轴从 0 开始并有 2 秒的步长,而不是盯着程序启动的第二秒?

How to make the x Axis to start on 0 and have a step of 2 seconds, instead of staring on a second the program started, using Livecharts?

我正在使用 sample code from live charts

代码隐藏

        private double _axisMax;
        private double _axisMin;
       

        public Plotter()
        {
            var mapper = Mappers.Xy<MeasureModel>()
                .X(model => model.DateTime.Ticks)   //use DateTime.Ticks as X
                .Y(model => model.Value);           //use the value property as Y
            //lets save the mapper globally.
            Charting.For<MeasureModel>(mapper);
            //the values property will store our values array
            ChartValues = new ChartValues<MeasureModel>();
            //lets set how to display the X Labels
            DateTimeFormatter = value => new DateTime((long)value).ToString("ss");
            //AxisStep forces the distance between each separator in the X axis
            AxisStep = TimeSpan.FromSeconds(5).Ticks;
            //AxisUnit forces lets the axis know that we are plotting seconds
            //this is not always necessary, but it can prevent wrong labeling
            AxisUnit = TimeSpan.TicksPerSecond;
            SetAxisLimits(DateTime.Now);
            //The next code simulates data changes every 300 ms
            IsReading = false;
            DataContext = this;
        }

        public ChartValues<MeasureModel> ChartValues { get; set; }
        public Func<double, string> DateTimeFormatter { get; set; }
        public double AxisStep { get; set; }
        public double AxisUnit { get; set; }

        public double AxisMax
        {
            get => _axisMax;
            set
            {
                _axisMax = value;
                OnPropertyChanged("AxisMax");
            }
        }
        public double AxisMin
        {
            get => _axisMin;
            set
            {
                _axisMin = value;
                OnPropertyChanged("AxisMin");
            }
        }

        public bool IsReading { get; set; }

       

        private void SetAxisLimits(DateTime now)
        {
            AxisMax = now.Ticks + TimeSpan.FromSeconds(1).Ticks; // lets force the axis to be 1 second ahead
            AxisMin = now.Ticks - TimeSpan.FromSeconds(20).Ticks; // and 20 seconds behind
        }

实际插入坐标的内部方法。我有:

所以

    var now = DateTime.Now;
    ChartValues.Add(new MeasureModel
    {
        DateTime = now,
        Value = SomeFunction()
    });
    SetAxisLimits(now);

xaml

             <wpf:CartesianChart Grid.Row="0" 
                                AnimationsSpeed="0:0:0.9" 
                                Hoverable="False" 
                                DataTooltip="{x:Null}">
                <wpf:CartesianChart.Series>
                    <wpf:LineSeries 
                        Name="MyChart" Values="{Binding ChartValues}" 
                                    PointGeometry="{x:Null}" 
                                    LineSmoothness="2"
                                    StrokeThickness="3" 
                                    Stroke="#F34336"
                                    Fill="Transparent"/>
                </wpf:CartesianChart.Series>
                <wpf:CartesianChart.AxisX>
                    <wpf:Axis LabelFormatter="{Binding DateTimeFormatter}" 
                              MaxValue="{Binding AxisMax}" 
                              MinValue="{Binding AxisMin}"
                              Unit="{Binding AxisUnit}">
                        <wpf:Axis.Separator>
                            <wpf:Separator Step="{Binding AxisStep}" />
                        </wpf:Axis.Separator>
                    </wpf:Axis>
                </wpf:CartesianChart.AxisX>
                <wpf:CartesianChart.AxisY>
                    <wpf:Axis MinValue="-20"
                              MaxValue="20">

                    </wpf:Axis>
                </wpf:CartesianChart.AxisY>
            </wpf:CartesianChart>

但是如何更改代码以便我的图表显示 x Axis label0 开始,然后转到 0,1,2,...100,...300 等等?而不是在第二个程序开始时开始?

我想完成类似的事情:

也许我可以使用其他库,如 oxyplot 之类的,对此有什么建议吗?

我认为最直接的方法是保存第一个数据点 DateTime 值。然后您可以计算每个其他数据点的相对时间。使用这些相对值,您可以 "make" 您的图表从 0.

开始

要让数据移出图表,如果时间继续,您应该将相对时间值提供给 SetAxisLimits(TimeSpan) 函数。数据类型必须更改为 TimeSpan.

像这样:

var now = DateTime.Now;
if(ChartValues.Count == 0)
    start = now; // save first timestamp to reference the following datapoints
var time = now.Subtract(start)
ChartValues.Add(new MeasureModel
{
    Time = time,
    Value = SomeFunction()
});
SetAxisLimits(time);