Csharp通过Chart类实现实时显示曲线并可截图(c#winform)
一、引入命名空间
using System.Windows.Forms.DataVisualization.Charting;
二、定义曲线
public Chart Curve = new Chart();
三、初始化曲线
this.Curve.ChartAreas.Clear();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea("C1");
this.Curve.ChartAreas.Add(chartArea1);
this.Curve.Series.Clear();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series("实时温度");
series1.ChartArea = "C1";
this.Curve.Series.Add(series1);
System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series("设定温度");
series2.ChartArea = "C1";
this.Curve.Series.Add(series2);
System.Windows.Forms.DataVisualization.Charting.Legend legendPV = new System.Windows.Forms.DataVisualization.Charting.Legend();
legendPV.Alignment = System.Drawing.StringAlignment.Near;
legendPV.BackColor = System.Drawing.Color.Black;
legendPV.DockedToChartArea = "ChartArea1";
legendPV.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
legendPV.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold);
legendPV.IsTextAutoFit = true;
legendPV.LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Column;
legendPV.Name = "S1";
Curve.Legends.Add(legendPV.Name);
System.Windows.Forms.DataVisualization.Charting.Legend legendSV = new System.Windows.Forms.DataVisualization.Charting.Legend();
legendSV.Alignment = System.Drawing.StringAlignment.Near;
legendSV.BackColor = System.Drawing.Color.Black;
legendSV.DockedToChartArea = "ChartArea1";
legendSV.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
legendSV.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold);
legendSV.IsTextAutoFit = true;
legendSV.LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Column;
legendSV.Name = "S2";
Curve.Legends.Add(legendSV.Name);
四、显示曲线
Random ran = new Random();
int n = ran.Next(100,1000);
int m = ran.Next(100,1000);
AddDataPVToCurrChart(m);
AddDataSVToCurrChart(n);
五、源程序下载
源程序下载地址:Csharp通过Chart类实现实时显示曲线并可截图(c#winform)