簡體   English   中英

如何在 Windows 窗體中使用 Oxyplot 更新繪圖

[英]How to update plot using Oxyplot in Windows Form

我正在做一個項目,我從板上讀取一些串行數據,並嘗試在圖表上顯示它。 到目前為止,我已經設法在我的應用程序中實現了 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
    }

}
}

不確定這是否是最好的方法,但這樣的事情應該可行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM