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


  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
完整的服务端及客户端调用程序,在win7+ vs2015环境运行通过. 一、说明 1、创建winfrom应用程序;(或者是控制台项目) 2、在项目添加一个WCF服务,并实现服务; 3、在需要启动WebService服务的地方启动该服务即可; 二、代码如下: 1、新建一个WCF服务——定义服务接口    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]     public interface ICalculator     {         [OperationContract]         double Add(double n1, double n2);     } 2、新建一个WCF服务——实现服务 public class CalculatorService : ICalculator     {         public double Add(double n1, double n2)         {             return n1 + n2;         }     } 3、添加完WcF服务后会在应用程序配置文件有入下节点                             <!--TestServer.ICalculator服务定义的接口,根据自己定义进行修改-->                                                                   <baseAddresses> <!--这个是要发布的服务地址,可以进行修改-->                                   </baseAddresses>                   4、在要启动服务的地方启动服务监听   public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { try { //打开服务创建监听,并开始监听消息 ServiceHost serviceHost = new ServiceHost(typeof(Service1));//需要using System.ServiceModel; serviceHost.Open(); label1.Text = "服务启动正常"; } catch (Exception ex) { label1.Text = ex.Message; } } 5、下面可以在客户端通过上面的服务地址”http://xxx.xxx.xxx.xx:8733/test/Service1/“对服务进行调用 到这步就实现在控制台实现webService的发布。
OxyPlot是一个用于创建数据可视化的开源库,适用于多个平台和框架,包括WinForms。它提供了一个易于使用的API和丰富的功能,可以用于绘制各种类型的图表和图形。 使用OxyPlot库在WinForms应用程序创建图表非常简单。首先,我们需要在项目添加对OxyPlotOxyPlot.WinForms的引用。然后,我们可以在需要绘制图表的窗体添加一个PlotView控件。PlotView控件是一个承载OxyPlot图表的容器。 使用OxyPlot绘制图表的基本步骤包括以下几个方面: 1. 创建一个PlotModel对象,它代表了我们要绘制的图表。 2. 向PlotModel对象添加数据系列,即要绘制的数据。 3. 配置图表的样式,例如设置坐标轴、添加标题等。 4. 将PlotModel对象设置给PlotView控件的Model属性,使其显示对应的图表。 在创建数据系列时,OxyPlot支持多种常见的图表类型,包括柱状图、折线图、散点图等。可以根据需要选择合适的数据系列类型,并设置相应的数据值和样式。 除了基本的图表功能,OxyPlot还提供了许多高级特性,例如支持缩放和滚动、数据标注、图例、动画等。这些功能可以帮助我们更好地展示和分析数据。 总体而言,OxyPlot是一个功能强大且易于使用的数据可视化库,适用于WinForms平台。无论是简单的静态图表还是复杂的交互式图形,它都有很好的灵活性和扩展性,能够满足各种数据可视化的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值