C# 绘制曲线的ScottPlot.NET
ScottPlot 是一个免费的开源 .NET 绘图库,可以轻松交互式地显示大型数据集。折线图、条形图、饼图、散点图等只需几行代码即可创建。
ScottPlot 4.1 Demo 官方的demo是最好的学习例程
解压后的文件清单如下,
双击 ScottPlot.Demo.WinForms.exe,查看不同demo的效果
查找demo对应的源代码点击如下的连接到ScottPlot 官网
再点击Github 跳转至 GitHub - ScottPlot/ScottPlot: Interactive plotting library for .NET
例如我要查找 DataLogger的源码
在Visual Studio 2017中利用Nuget包管理下载ScottPlot,如下图
在使用ScottPlot.NET之前需要了解VS基本的配置需求,不同版本的ScottPlot所需的.NET framework版本也不一样
如上图我准备使用V4最高版本V4.174,根据依赖需求,我准备切换.NETFramework4.7.2,如何从.NETFramework4切换
到.NETFramework4.7.2,这里报错是因为缺少app.config的配置信息。
添加如下内容到app.config文件中
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /> 5 </startup> 6 </configuration>
参考demo的源码移植到自己的程序里
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 11 namespace CMXM 12 { 13 public partial class Debug : Form 14 { 15 16 readonly System.Windows.Forms.Timer AddNewDataTimer = new Timer() { Interval = 10, Enabled = true }; 17 readonly System.Windows.Forms.Timer UpdatePlotTimer = new Timer() { Interval = 50, Enabled = true }; 18 19 readonly ScottPlot.Plottable.DataLogger Logger; 20 21 readonly Random Rand = new Random(); 22 23 double LastPointValue = 0; 24 25 26 public Debug() 27 { 28 InitializeComponent(); 29 30 Logger = formsPlot1.Plot.AddDataLogger(label: "trace"); 31 32 AddRandomWalkData(1000); 33 btnFull_Click(null, EventArgs.Empty); 34 cbView_CheckedChanged(null, EventArgs.Empty); 35 36 AddNewDataTimer.Tick += (s, e) => AddRandomWalkData(10); 37 UpdatePlotTimer.Tick += UpdatePlotTimer_Tick; 38 } 39 40 private void AddRandomWalkData(int count) 41 { 42 for (int i = 0; i < count; i++) 43 { 44 LastPointValue = LastPointValue + Rand.NextDouble() - .5; 45 Logger.Add(Logger.Count, LastPointValue); 46 } 47 } 48 49 private void UpdatePlotTimer_Tick(object sender, EventArgs e) 50 { 51 if (Logger.Count == Logger.CountOnLastRender) 52 return; 53 54 formsPlot1.Refresh(); 55 56 this.metroTextBoxInfo.Text = $"DataLogger Demo ({Logger.Count:N0} points)"; 57 } 58 59 private void cbView_CheckedChanged(object sender, EventArgs e) 60 { 61 Logger.ManageAxisLimits = cbEnableViewManagement.Checked; 62 63 // disable mouse interaction if axis limits are managed automatically 64 formsPlot1.Configuration.Pan = !cbEnableViewManagement.Checked; 65 formsPlot1.Configuration.Zoom = !cbEnableViewManagement.Checked; 66 } 67 68 private void btnFull_Click(object sender, EventArgs e) 69 { 70 Logger.ViewFull(); 71 formsPlot1.Plot.Title("Full"); 72 formsPlot1.Refresh(); 73 } 74 75 private void btnJump_Click(object sender, EventArgs e) 76 { 77 Logger.ViewJump(); 78 formsPlot1.Plot.Title("Jump"); 79 formsPlot1.Refresh(); 80 } 81 82 private void btnSlide_Click(object sender, EventArgs e) 83 { 84 Logger.ViewSlide(); 85 formsPlot1.Plot.Title("Slide"); 86 formsPlot1.Refresh(); 87 } 88 89 private void btnClear_Click(object sender, EventArgs e) 90 { 91 Logger.Clear(); 92 formsPlot1.Refresh(); 93 } 94 95 private void chk_showLegend_CheckedChanged(object sender, EventArgs e) 96 { 97 formsPlot1.Plot.Legend(chk_showLegend.Checked); 98 formsPlot1.Refresh(); 99 } 100 } 101 }
运行效果