我如何让 X 轴从 0 开始并每秒更新而不是使用当前时间(DateTime)?

我正在尝试创建一个测量系统,可以使用 LiveCharts 测量 Y 轴上的值和 X 轴上的时间。

我使用Constant Changes作为基础来创建我的图表,但我希望我的 X 轴从 0 秒开始,而不是像示例中那样从当前时间开始。我已经尝试过如何使用 Livecharts 使 x 轴从 0 开始并有 2 秒的步长,而不是盯着程序启动的第二秒?但我无法让它与我的程序一起工作。我想做他们在上面链接的线程中做的同样的事情,但无法让它工作。

我的输出/它看起来如何

代码隐藏:

        public void init()

        {

            var mapper = Mappers.Xy<ValueRandomizerForTest>().X(model => 

             model.DateTime.Ticks).Y(model => model.Valuefordate);

            Charting.For<ValueRandomizerForTest>(mapper);

            ChartValues = new ChartValues<ValueRandomizerForTest>();

            DateTimeFormatter = value => new 

                 DateTime((long)value).ToString("ss");

            AxisStep = TimeSpan.FromSeconds(1).Ticks;


            AxisUnit = TimeSpan.TicksPerSecond;

            SetAxisLimits(DateTime.Now);



        }



public void read()

        {



            var r = new Random();


            while (isreading)

            {

                Thread.Sleep(550);

                var now = DateTime.Now;

                var test = now.Second;



                _trend = r.Next(1, 100);


                if(ChartValues.Count == 0)

                {



                }


                ChartValues.Add(new ValueRandomizerForTest

                {

                    DateTime = now,

                    Valuefordate = _trend

                });



                SetAxisLimits(now);


                //lets only use the last 150 values

                if (ChartValues.Count > 150)

                {

                    ChartValues.RemoveAt(0);

                }

            }




        }

        public void SetAxisLimits(DateTime now)

        {


            AxisMax = now.Ticks + TimeSpan.FromSeconds(1).Ticks; //Axis is moving 1 second ahead

            AxisMin = now.Ticks - TimeSpan.FromSeconds(5).Ticks; 

        }


ITMISS
浏览 109回答 1
1回答

吃鸡游戏

见内联评论:&nbsp; &nbsp; private long startTimeTicks;&nbsp; &nbsp; public void init()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var mapper = Mappers.Xy<ValueRandomizerForTest>().X(model =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model.DateTime.Ticks).Y(model => model.Valuefordate);&nbsp; &nbsp; &nbsp; &nbsp; Charting.For<ValueRandomizerForTest>(mapper);&nbsp; &nbsp; &nbsp; &nbsp; ChartValues = new ChartValues<ValueRandomizerForTest>();&nbsp; &nbsp; &nbsp; &nbsp; DateTimeFormatter = value => new&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DateTime((long)value).ToString("ss");&nbsp; &nbsp; &nbsp; &nbsp; AxisStep = TimeSpan.FromSeconds(1).Ticks;&nbsp; &nbsp; &nbsp; &nbsp; AxisUnit = TimeSpan.TicksPerSecond;&nbsp; &nbsp; &nbsp; &nbsp; var currentTime = DateTime.Now;&nbsp; &nbsp; &nbsp; &nbsp; startTimeTicks = currentTime.Ticks; // store start time&nbsp; &nbsp; &nbsp; &nbsp; SetAxisLimits(currentTime);&nbsp; &nbsp; }&nbsp; &nbsp; public void read()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var r = new Random();&nbsp; &nbsp; &nbsp; &nbsp; while (isreading)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(550);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var now = DateTime.Now;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var test = now.Second;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _trend = r.Next(1, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ChartValues.Count == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChartValues.Add(new ValueRandomizerForTest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTime = now - new TimeSpan(startTimeTicks),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Valuefordate = _trend&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetAxisLimits(now);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //lets only use the last 150 values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ChartValues.Count > 150)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChartValues.RemoveAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void SetAxisLimits(DateTime now)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; long offsetTicks = now.Ticks - startTimeTicks; // compute offset ticks from program start (at call from init() this calculation will be equal to 0)&nbsp; &nbsp; &nbsp; &nbsp; AxisMin = Math.Max(offsetTicks - TimeSpan.FromSeconds(5).Ticks, 0);&nbsp; &nbsp; &nbsp; &nbsp; AxisMax = AxisMin&nbsp; + TimeSpan.FromSeconds(6).Ticks; // Set max according to min&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP