简体   繁体   English

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

[英]How do i get X Axis to start from 0 and update each second instead of using the current time(DateTime)?

I'm trying to create like a measuring system that can measure a value on Y axis and time on X axis with LiveCharts. 我正在尝试创建一个测量系统,可以使用LiveCharts测量Y轴上的值和X轴上的时间。

I've used Constant Changes as a base to create my graph but I want my X Axis to start from 0 seconds instead of the current time like they do in the example. 我使用Constant Changes作为基础来创建我的图形,但我希望我的X轴从0秒开始,而不是像在示例中那样从当前时间开始。 I've tried 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? 我已经尝试过如何使x轴从0开始并且步骤为2秒,而不是使用Livecharts盯着程序启动的第二步? but I couldn't get it to work with my program. 但我无法让它与我的程序一起工作。 I want to do the same thing they did in thread linked above but can't get it to work. 我想做他们在上面链接的线程中所做的相同的事情,但无法让它工作。

My Output/ How it looks 我的输出/它的外观

Code Behind: 代码背后:

        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; 
        }

ValuerandomizerFortest Class: ValuerandomizerFortest类:

        public DateTime DateTime { get; set; }
        public TimeSpan Time { get; set; }
        public double Valuefordate { get; set; }



        public double AxisStep { get; set; }

        public double AxisUnit { get; set; }


           private double _axisMax;

           private double _axisMin;


        public double AxisMax
        {

            get
            {
                return _axisMax
            }

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

XAML: XAML:

<Window x:Class="WPFDemo.Views.GraphViewer"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFDemo.Views"
        mc:Ignorable="d"
        Title="GraphViewer" Height="700" Width="950">
    <Grid >
        <Grid Margin="759,10,10,588.4" RenderTransformOrigin="0.51,0.526">
            <Button Content="Random"
                Margin="6"
                Padding="6"
                Command="{Binding randomer}">

            </Button>

        </Grid>
        <!--<Grid Height="80" Width="200" Margin="50, -20,600,500">
            -->
        <!--<ComboBox ItemsSource="{Binding list}" Margin="-32,-10,32,20">
                -->
        <!--<ComboBox.ItemTemplate>
                    -->
        <!--<DataTemplate>
                        <TextBlock Text="{Binding Name}">

                        </TextBlock>
                    </DataTemplate>-->
        <!--
                </ComboBox.ItemTemplate>-->
        <!--
            </ComboBox>-->

        <!--<Button Content="Updater"
                    Command="{Binding ClickCommand}">


            </Button>-->
        <!--

        </Grid>-->
        <Grid Height="500" Width="940" Margin="4,100,0,28.4">



            <lvc:CartesianChart Grid.Row="1" AnimationsSpeed="0:0:0.3" Hoverable="False" DataTooltip="{x:Null}">
                <lvc:CartesianChart.Series>
                    <lvc:LineSeries Values="{Binding ChartValues}" 
                                PointGeometry="{x:Null}" 
                                LineSmoothness="1"
                                StrokeThickness="6" 
                                Stroke="#F34336"
                                Fill="Transparent"/>
                </lvc:CartesianChart.Series>
                <lvc:CartesianChart.AxisX>
                    <lvc:Axis x:Name="XAxis" LabelFormatter="{Binding DateTimeFormatter}" 
                          MaxValue="{Binding AxisMax}" 
                          MinValue="{Binding AxisMin}"
                          Unit="{Binding AxisUnit}">
                        <lvc:Axis.Separator>
                            <lvc:Separator Step="{Binding AxisStep}" />
                        </lvc:Axis.Separator>
                    </lvc:Axis>
                </lvc:CartesianChart.AxisX>
            </lvc:CartesianChart>

        </Grid>
    </Grid>
</Window>

See comments inline: 请参阅内联评论:

    private long startTimeTicks;
    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;

        var currentTime = DateTime.Now;
        startTimeTicks = currentTime.Ticks; // store start time
        SetAxisLimits(currentTime);
    }

    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 - new TimeSpan(startTimeTicks),
                Valuefordate = _trend
            });

            SetAxisLimits(now);

            //lets only use the last 150 values
            if (ChartValues.Count > 150)
            {
                ChartValues.RemoveAt(0);
            }
        }
    }

    public void SetAxisLimits(DateTime now)
    {
        long offsetTicks = now.Ticks - startTimeTicks; // compute offset ticks from program start (at call from init() this calculation will be equal to 0)
        AxisMin = Math.Max(offsetTicks - TimeSpan.FromSeconds(5).Ticks, 0);
        AxisMax = AxisMin  + TimeSpan.FromSeconds(6).Ticks; // Set max according to min
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 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? 如何从 X 轴上的日期时间值获取像素 Position? - How to get Pixel Position from DateTime Value on X-Axis? 如何在不使用系统时间的情况下在C#中获取当前本地DateTime - How do you get current local DateTime in c# without using system time 即使从更新调用,如何使用 StartCoroutine 使协程每次仅启动一次,如何淡入/淡出屏幕? - How can I fade in/out the screen using StartCoroutine making the Coroutine to start each time only once even if calling from Update? 如何调试与时间相关的应用程序? (使用当前的DateTime变量而不是DateTime.Now) - How to debug an application that is time-dependent? (using a current DateTime variable instead of DateTime.Now) 如何在C#中使用ZedGraph从X轴的PointPair获取日期时间? - How to get date time from PointPair in X axis using ZedGraph in C#? 如何从Windows Phone 7中的DateTime中减去秒? - How do I subtract a second from a DateTime in Windows Phone 7? 我如何获得时间在Unity中使用StreamWriter和DateTime保存到文件(我在使用c#)? - How do I get the time to save to a file using StreamWriter and DateTime in Unity (I'm using c#)? 为什么我没有获得X轴的DateTime值 - why don't I get the DateTime value of the X Axis 如何从C#中的预编译器获取当前的DateTime? - How can I get the current DateTime from the Precompiler in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM