OxyPlot在WinForm中的应用

Plot

1、简介(从GitHub上复制)

Getting started

  1. Use the NuGet package manager to add a reference to OxyPlot (see details below if you want to use pre-release packages)
  2. Add a PlotView to your user interface
  3. Create a PlotModel in your code
  4. Bind the PlotModel to the Model property of your PlotView

Examples

You can find examples in the /Source/Examples folder in the code repository.

NuGet packages

The latest pre-release packages are pushed by AppVeyor CI to myget.org To install these packages, set the myget.org package source https://www.myget.org/F/oxyplot and remember the "-pre" flag.

The stable release packages will be pushed to nuget.org. Note that we have currently have a lot of old (v2015.*) and pre-release packages on this feed, this will be cleaned up as soon as we release v1.0.

PackageTargetsDependencies
OxyPlot.Core.NET Standard 1.0 
OxyPlot.Wpf.NET 4.5.2 
OxyPlot.WindowsForms.NET 4.5.2 
OxyPlot.WindowsUniversal Windows 10.0 
OxyPlot.GtkSharp.NET 4.5.2GTK# 2
OxyPlot.GtkSharp3.NET 4.5.2GTK# 3
OxyPlot.Xamarin.AndroidMonoAndroid 
OxyPlot.Xamarin.iOSMonoTouch and iOS10 
OxyPlot.Xamarin.MacMac20 
OxyPlot.Xamarin.FormsMonoTouch, iOS10, MonoAndroid, WP8 
OxyPlot.Xwt.NET 4.5.2 
OxyPlot.SharpDX.Wpf.NET 4.5.2 
OxyPlot.Avalonia.NET 4.5.2 
OxyPlot.OpenXML.NET 4.5.2 
OxyPlot.Pdf.NET 4.5.2PdfSharp
OxyPlot.Contrib.NET Standard 1.0 
OxyPlot.ExampleLibrary.NET Standard 1.0 

2、如何使用

在GitHub上面有一些简单的示例可以参考。但是在实际应用中并非如此简单,本示例主要介绍如何在winform中完成一个能够实时绘制动态曲线的目标

2.1、创建项目

创建项目名为OxyPlotWinform的winform工程,在项目的nuget搜索添加OxyPlot.WindowsForms库。

2.2、添加PlotView

在Form1中添加一个容器panel,设置为在父容器中停靠,在panel1中添加一个plotview,设置Dock为Fill。

2.3 添加代码

添加变量:

private DateTimeAxis _dateAxis;//X轴
private LinearAxis _valueAxis;//Y轴
        
private PlotModel _myPlotModel;
private Random rand = new Random();//用来生成随机数

添加代码:

        private void Form1_Load(object sender, EventArgs e)
        {
            //定义model
            _myPlotModel = new PlotModel()
            {
                Title = "Temp & Humi",
                LegendTitle =  "Legend",
                LegendOrientation = LegendOrientation.Horizontal,
                LegendPlacement =  LegendPlacement.Inside,
                LegendPosition =  LegendPosition.TopRight,
                LegendBackground = OxyColor.FromAColor(200,OxyColors.Beige),
                LegendBorder = OxyColors.Black
            };
            //X轴
            _dateAxis = new DateTimeAxis()
            {
                MajorGridlineStyle =  LineStyle.Solid,
                MinorGridlineStyle =  LineStyle.Dot,
                IntervalLength =  80,
                IsZoomEnabled = false,
                IsPanEnabled =  false
            };
            _myPlotModel.Axes.Add(_dateAxis);

            //Y轴
            _valueAxis = new LinearAxis()
            {
                MajorGridlineStyle =  LineStyle.Solid,
                MinorGridlineStyle = LineStyle.Dot,
                IntervalLength = 80,
                Angle = 60,
                IsZoomEnabled = false,
                IsPanEnabled = false,
                Maximum = 100,
                Minimum = -1
            };
            _myPlotModel.Axes.Add(_valueAxis);

            //添加标注线,温度上下限和湿度上下限
            var lineTempMaxAnnotation = new OxyPlot.Annotations.LineAnnotation()
            {
                Type = LineAnnotationType.Horizontal,
                Color = OxyColors.Red,
                LineStyle = LineStyle.Solid,
                Y = 10,
                Text = "Temp MAX:10"
            };
            _myPlotModel.Annotations.Add(lineTempMaxAnnotation);

            var lineTempMinAnnotation = new LineAnnotation()
            {
                Type = LineAnnotationType.Horizontal,
                Y = 30,
                Text = "Temp Min:30",
                Color = OxyColors.Red,
                LineStyle = LineStyle.Solid
            };
            _myPlotModel.Annotations.Add(lineTempMinAnnotation);

            var lineHumiMaxAnnotation = new OxyPlot.Annotations.LineAnnotation()
            {
                Type = LineAnnotationType.Horizontal,
                Color = OxyColors.Red,
                LineStyle = LineStyle.Solid,
                //lineMaxAnnotation.MaximumX = 0.8;
                Y = 75,
                Text = "Humi MAX:75"
            };
            _myPlotModel.Annotations.Add(lineHumiMaxAnnotation);

            var lineHumiMinAnnotation = new LineAnnotation()
            {
                Type = LineAnnotationType.Horizontal,
                Y = 35,
                Text = "Humi Min:35",
                Color = OxyColors.Red,
                LineStyle = LineStyle.Solid
            };
            _myPlotModel.Annotations.Add(lineHumiMinAnnotation);

            //添加两条曲线
            var series = new LineSeries()
            {
                Color = OxyColors.Green,
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = OxyColors.DarkGreen,
                MarkerType = MarkerType.Diamond,
                Title = "Temp",
                Smooth = true
            };
            _myPlotModel.Series.Add(series);
            series = new LineSeries()
            {
                Color = OxyColors.Blue,
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = OxyColors.BlueViolet,
                MarkerType = MarkerType.Star,
                Title = "Humi",
                Smooth = true
            };
            _myPlotModel.Series.Add(series);


            plotView1.Model = _myPlotModel;

            Task.Factory.StartNew(() =>
            {
                while (true)
                {

                    var date = DateTime.Now;
                    _myPlotModel.Axes[0].Maximum = DateTimeAxis.ToDouble(date.AddSeconds(1));

                    var lineSer = plotView1.Model.Series[0] as LineSeries;
                    lineSer.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), rand.Next(100, 300)/10.0));
                    if (lineSer.Points.Count > 100)
                    {
                        lineSer.Points.RemoveAt(0);
                    }

                    lineSer = plotView1.Model.Series[1] as LineSeries;
                    lineSer.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), rand.Next(350, 750)/10.0));
                    if (lineSer.Points.Count > 100)
                    {
                        lineSer.Points.RemoveAt(0);
                    }

                    _myPlotModel.InvalidatePlot(true);

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

2.4、程序运行

项目地址:

1、https://gitee.com/sesametech-group/OxyPlotWinform

2、https://github.com/mzy666888/OxyPlotWinform

参考文献:

1、https://blog.csdn.net/mytinaonly/article/details/79926290


©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页