OxyPlot的动态曲线图表

前台

  <oxy:PlotView Model="{Binding DynamicCurve}" /> 

 Model中

public PlotModel DynamicCurve{ get; set; }

public void PlotViewModel()
        {
            DynamicCurve = new PlotModel();
            //定义x轴 double属性的轴
            OxyPlot.Axes.LinearAxis bottomAxis = new OxyPlot.Axes.LinearAxis()
            {
                Position = AxisPosition.Bottom,
                //IsAxisVisible = false,//X轴不显示
                //Title = "X轴",//显示标题内容
                //TitlePosition = 1,//显示标题位置
                //TitleColor = OxyColor.Parse("#d3d3d3"),//显示标题位置
                IsZoomEnabled = false,//坐标轴缩放关闭
                IsPanEnabled = false,//图表缩放功能关闭
                MajorGridlineStyle = LineStyle.Solid,
                MinorGridlineStyle = LineStyle.Dot,
            };
            //定义x轴,日期属性的轴
             OxyPlot.Axes.DateTimeAxis bottomAxis = new OxyPlot.Axes.DateTimeAxis()
             {
                Position = AxisPosition.Bottom,
                //IsAxisVisible = false,//X轴不显示
                Title = "日期",//显示标题内容
                //TitlePosition = 1,//显示标题位置
                //TitleColor = OxyColor.Parse("#d3d3d3"),//显示标题位置
                IsZoomEnabled = false,//坐标轴缩放关闭
                IsPanEnabled = false,//图表缩放功能关闭
                //MajorGridlineStyle = LineStyle.Solid,
                //MinorGridlineStyle = LineStyle.Dot,
                StringFormat = "HH:mm:ss",
                MinorIntervalType = DateTimeIntervalType.Minutes,

                IntervalType = DateTimeIntervalType.Minutes,
              };



            //定义y轴
            OxyPlot.Axes.LinearAxis leftAxis = new OxyPlot.Axes.LinearAxis()
            {
                Position = AxisPosition.Left,
                Minimum = -5,
                Maximum = 5,
                //Title = "Y轴",//显示标题内容
                //TitlePosition = 1,//显示标题位置
                //TitleColor = OxyColor.Parse("#d3d3d3"),//显示标题位置
                IsZoomEnabled = false,//坐标轴缩放关闭
                IsPanEnabled = false,//图表缩放功能关闭

                MajorGridlineStyle = LineStyle.Solid,
                MinorGridlineStyle = LineStyle.Dot,
            };
           

            DynamicCurve.Axes.Add(bottomAxis);
            DynamicCurve.Axes.Add(leftAxis);

            var rd = new Random();
            ////添加曲线
            var series = new LineSeries()
            {
                Color = OxyColors.Green,
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = OxyColors.DarkGreen,
                MarkerType = MarkerType.Diamond,
                Title = "Temp"

            };
            DynamicCurve.Series.Add(series);
            int i = 0;
            Task.Factory.StartNew(() =>
            {
                while (true)
                { 
                    DynamicCurve.Axes[0].Maximum = 5+i;
                    series.Points.Add(new DataPoint(i, rand.Next(-5, 5)));
                //  series.Points.Add(DateTimeAxis.CreateDataPoint(CreateDate, data)); //x轴为日期的
                    if (series.Points.Count > 100)
                    {
                        series.Points.RemoveAt(0);
                    }

                    DynamicCurve.InvalidatePlot(true);

                    Thread.Sleep(1000);
                    i++;
                }
            });


        }

 

posted @ 2021-03-15 17:26  Yhzwei  阅读(1810)  评论(0编辑  收藏  举报