【问题标题】:How to update plot using Oxyplot in Windows Form如何在 Windows 窗体中使用 Oxyplot 更新绘图
【发布时间】:2018-02-17 06:44:08
【问题描述】:

我正在做一个项目,我从板上读取一些串行数据,并尝试在图表上显示它。 到目前为止,我已经设法在我的应用程序中实现了 Oxyplot。

但我很困惑如何更新来自串行端口的每个新数据的绘图?

这是我的简化版代码

using OxyPlot;


namespace Motor
{

public partial class Form1 : Form
{ 


    public Form1()
    {
        InitializeComponent();
        ComPort.DataReceived += new 
            System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived_1);
        plot1.Model = GridLinesHorizontal();

    }

    public static PlotModel GridLinesHorizontal()
    {
        var plotModel = new PlotModel();
        plotModel.Title = "Horizontal";
        var linearAxis1 = new LinearAxis();

        linearAxis1.MajorGridlineStyle = LineStyle.Solid;
        linearAxis1.MinorGridlineStyle = LineStyle.Dot;
        linearAxis1.Maximum = 5;
        linearAxis1.Minimum = -5;
        plotModel.Axes.Add(linearAxis1);

        return plotModel;

    }


    private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
    {

        InputData = ComPort.ReadLine();

        if (InputData != String.Empty)
        {
            this.BeginInvoke(new SetTextCallback(SetText), new object[] { InputData });
        }
    }

    private void SetText(string text)
    {
        dVal = double.Parse(text, CultureInfo.InvariantCulture); // convert to double
        ///// HERE I WANT TO UPDATE THE PLOT with dval
    }

}
}

【问题讨论】:

    标签: c# oxyplot


    【解决方案1】:

    不确定这是否是最好的方法,但这样的方法应该可行:

    using OxyPlot;
    using OxyPlot.Axes;
    using OxyPlot.Series;
    
    namespace Motor
    {
        public partial class Form1 : Form
        { 
            public Form1()
            {
               InitializeComponent();
               ComPort.DataReceived += new
                      System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived_1);
               plot1.Model = GridLinesHorizontal();
               //create new LineSeries and add it to the PlotView
               Line1 = new LineSeries
               {
                   Title = "Test Series",
                   Color = OxyColors.Red,
                   TextColor = OxyColors.Red,
                   BrokenLineColor = OxyColors.Red
               };
               plot1.Model.Series.Add(Line1);
    
           }
    
           LineSeries Line1; // declare Line1 as global
    
           public static PlotModel GridLinesHorizontal()
           {
               var plotModel = new PlotModel();
               plotModel.Title = "Horizontal";
               var linearAxis1 = new LinearAxis();
    
               linearAxis1.MajorGridlineStyle = LineStyle.Solid;
               linearAxis1.MinorGridlineStyle = LineStyle.Dot;
               linearAxis1.Maximum = 5;
               linearAxis1.Minimum = -5;
               plotModel.Axes.Add(linearAxis1);
    
               return plotModel;
    
           }
    
           private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
           {
               InputData = ComPort.ReadLine();
    
               if (InputData != String.Empty)
               {
                   this.BeginInvoke(new SetTextCallback(SetText), new object[] { InputData });
               }
           }
           int plotIndex = 0;
           private void SetText(string text)
           {
               dVal = double.Parse(text, CultureInfo.InvariantCulture); // convert to double
               ///// plotIndex is the x value of the new point, not sure if OxyPlot offers an auto increment option
               Line1.Points.Add(new DataPoint(plotIndex, dVal));
               plotIndex++;
               plot1.Invalidate();
           }
        }
    }
    

    【讨论】:

    • 感谢您的分享,关于 plotindex,我会尝试您的建议,我猜您的意思是 x 轴。将来我计划通过计时器或类似的东西来增加它。现在只想熟悉一下oxy
    • 是的,plotIndex 是 x 轴上的新点位置。像这样,它会自动将每个新数据值移动到旧数据值的右侧。您还可以使用 plotIndex 变量设置 x 轴的视图范围,以便它始终显示最新数据。
    • 谢谢,它现在运行良好,同时我注意到 oxyplot 有一些不同的版本。是否可以让图表专注于新数据并以 1 毫秒为基数?
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    相关资源
    最近更新 更多