库的下载
项目的引用右键,选择“管理NuGet程序包”,搜索OxyPlot,安装OxyPlot.WindowsForms
或者直接引用OxyPlot.dll,和OxyPlot.WIndowsForms.dll文件,在此处下载。
HelloWorld
首先在Form上创建一个PlotView控件,暂时无法直接拖入。
可以在InitializeComponent下调用InitPlot();
private void InitPlot() { this.plot1 = new OxyPlot.WindowsForms.PlotView(); this.SuspendLayout(); this.plot1.Dock = System.Windows.Forms.DockStyle.Fill; this.plot1.Location = new System.Drawing.Point(0, 0); this.plot1.Name = "plot1"; this.plot1.TabIndex = 0; this.plot1.Margin = new System.Windows.Forms.Padding(0); this.Controls.Add(this.plot1); this.ResumeLayout(); }
注意:如果是在Form的一个Panel下添加PlotView,
可以这样
this.panel1.Controls.Add(this.plot1);
然后创建Model,整体代码如下
public Form1() { InitializeComponent(); InitPlot(); var myModel = new PlotModel { Title = "Example 1" }; myModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)")); this.plot1.Model = myModel; }
改变背景色,添加曲线
var myModel = new PlotModel { Title = "Example 1" ,Background=OxyColors.White}; myModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)")); myModel.Series.Add(new FunctionSeries(Math.Sin, -10, 10, 0.1, "sin(x)")); myModel.Series.Add(new FunctionSeries(t => 5 * Math.Cos(t), t => 5 * Math.Sin(t), 0, 2 * Math.PI, 0.1, "cos(t),sin(t)")); this.plot1.Model = myModel;
一个实时更新曲线的例子
来源https://blog.csdn.net/weixin_42930928/article/details/81706540
public partial class Form1 : Form { private PlotView plot1; public Form1() { InitializeComponent(); InitPlot(); } private void InitPlot() { this.plot1 = new OxyPlot.WindowsForms.PlotView(); this.SuspendLayout(); this.plot1.Dock = System.Windows.Forms.DockStyle.Fill; this.plot1.Location = new System.Drawing.Point(0, 0); this.plot1.Name = "plot1"; this.plot1.TabIndex = 0; this.plot1.Margin = new System.Windows.Forms.Padding(0); this.panel1.Controls.Add(this.plot1); this.ResumeLayout(); } private PlotModel _myPlotModel; private DateTimeAxis _dateAxis; private LinearAxis _valueAxis; private Random rand = new Random(); private void Form1_Load(object sender, EventArgs e) { _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", }; _myPlotModel.Series.Add(series); series = new LineSeries() { Color = OxyColors.Blue, StrokeThickness = 2, MarkerSize = 3, MarkerStroke = OxyColors.BlueViolet, MarkerType = MarkerType.Star, Title = "Humi", }; _myPlotModel.Series.Add(series); plot1.Model = _myPlotModel; Task.Factory.StartNew(() => { while (true) { var date = DateTime.Now; _myPlotModel.Axes[0].Maximum = DateTimeAxis.ToDouble(date.AddSeconds(1)); var lineSer = plot1.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 = plot1.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); } }); } }