C#话曲线图
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApp6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //自定义函数 private Queue<double> dataQueue = new Queue<double>(100); private int curValue = 0; private int num = 1;//每次删除增加几个点 /// <summary> /// 初始化图表 /// </summary> private void InitChart() { //定义图表区域 this.chart1.ChartAreas.Clear(); ChartArea chartArea1 = new ChartArea("C1"); this.chart1.ChartAreas.Add(chartArea1); //定义存储和显示点的容器 this.chart1.Series.Clear(); Series series1 = new Series("S1"); series1.ChartArea = "C1"; this.chart1.Series.Add(series1); //设置图表显示样式 this.chart1.ChartAreas[0].AxisY.Minimum = 0; this.chart1.ChartAreas[0].AxisY.Maximum = 100; this.chart1.ChartAreas[0].AxisX.Interval = 1; this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver; this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver; //设置标题 this.chart1.Titles.Clear(); this.chart1.Titles.Add("S01"); this.chart1.Titles[0].Text = "XXX显示"; this.chart1.Titles[0].ForeColor = Color.RoyalBlue; this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); //设置图表显示样式 this.chart1.Series[0].Color = Color.Red; this.chart1.Series[0].ChartType = SeriesChartType.Line; this.chart1.Series[0].Points.Clear(); } private void UpdateQueueValue() { if (dataQueue.Count > 100) { //先出列 for (int i = 0; i < num; i++) { dataQueue.Dequeue(); } } Random r = new Random(); for (int i = 0; i < num; i++) { dataQueue.Enqueue(r.Next(0, 100)); } } private void Form1_Load(object sender, EventArgs e) { InitChart(); this.timer1.Start(); } private void timer1_Tick_1(object sender, EventArgs e) { UpdateQueueValue(); this.chart1.Series[0].Points.Clear(); for (int i = 0; i < dataQueue.Count; i++) { this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i)); } } } }