page contents

C#实现chart控件动态曲线绘制

本文讲述了C#实现chart控件动态曲线绘制!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-06-0oaLUYpX62a2a32eb7cd4.png

本文讲述了C#实现chart控件动态曲线绘制!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

实验室要做一个动态曲线绘制,网上方法很多,但是缺乏完整代码和效果图的整合,往往总是缺少其一,因此整理如下,方便大家编程,节约时间。
思路:新建一个队列,利用timer控件,动态的往队列中加入数据,每次触发事件,就相当于将队列中的值全部重新画一遍。

我的目的是做四个点的动态监测,所以代码重复了四次,其实应该用4个线程来做,思路就显得较为清晰了,这也是可以改进的地方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
public partial class 界面_Xtratabcontrol版本_ : Form
    {
        private Queue<double> dataQueue1 = new Queue<double>(100); //30个就清空一次
        private Queue<double> dataQueue2 = new Queue<double>(100); //30个就清空一次
        private Queue<double> dataQueue3 = new Queue<double>(100); //30个就清空一次
        private Queue<double> dataQueue4 = new Queue<double>(100); //30个就清空一次
        private int stress1 = 0;//设置一个压力值全局变量
        private int stress2 = 0;//设置一个压力值全局变量
        private int stress3 = 0;//设置一个压力值全局变量
        private int stress4 = 0;//设置一个压力值全局变量
        string monthNow = "";
        string monthNext = "";
        string currentTime = "";
        bool isRefresh = false;
        public 界面_Xtratabcontrol版本_()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false; //设置不自动显示数据库中未绑定的列
            //设置隔行背景色
            this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
        }
 
        private void btnInit_Click(object sender, EventArgs e)
        {
            InitChart1();
            InitChart2();
            InitChart3();
            InitChart4();
        }
 
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.timer1.Start();
        }
 
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                UpdateDate(); //根据当前时间取下一个数据,同时给month赋值
                dataQueue1.Enqueue(stress1); //就是这,不断往里面加数据。
                dataQueue2.Enqueue(stress2);
                dataQueue3.Enqueue(stress3);
                dataQueue4.Enqueue(stress4);
                if (isRefresh)
                {
                    //刷新界面
                    isRefresh = false;
                    InitChart1();
                    InitChart2();
                    InitChart3();
                    InitChart4();
                    dataQueue1.Enqueue(stress1);
                    dataQueue2.Enqueue(stress2);
                    dataQueue3.Enqueue(stress3);
                    dataQueue4.Enqueue(stress4);
                }
                this.chart1.Series[0].Points.Clear();
                this.chart2.Series[0].Points.Clear();
                this.chart3.Series[0].Points.Clear();
                this.chart4.Series[0].Points.Clear();
                for (int i = 0; i < dataQueue1.Count; i++)
                {
                    this.chart1.Series[0].Points.AddXY((i + 1), dataQueue1.ElementAt(i)); 相当于每次都是重新画一遍
                }
                for (int i = 0; i < dataQueue2.Count; i++)
                {
                    this.chart2.Series[0].Points.AddXY((i + 1), dataQueue2.ElementAt(i)); 相当于每次都是重新画一遍
                }
                for (int i = 0; i < dataQueue3.Count; i++)
                {
                    this.chart3.Series[0].Points.AddXY((i + 1), dataQueue3.ElementAt(i)); 相当于每次都是重新画一遍
                }
                for (int i = 0; i < dataQueue4.Count; i++)
                {
                    this.chart4.Series[0].Points.AddXY((i + 1), dataQueue4.ElementAt(i)); 相当于每次都是重新画一遍
                }
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void InitChart1()
        {
            try
            {
                //定义图表区域
                this.chart1.ChartAreas.Clear();
                ChartArea chartArea1 = new ChartArea("C1");
                this.chart1.ChartAreas.Add(chartArea1);
                //this.chart1.Dock = DockStyle.Fill;
                //定义存储和显示点的容器
                this.chart1.Series.Clear();
                Series series1 = new Series("S1");
                series1.ChartArea = "C1";
                this.chart1.Series.Add(series1);
                //设置图表显示样式
                this.chart1.ChartAreas[0].AxisY.Minimum = 30000;
                this.chart1.ChartAreas[0].AxisY.Maximum = 50000;
                this.chart1.ChartAreas[0].AxisX.Minimum = 1;
                this.chart1.ChartAreas[0].AxisX.Maximum = 31;
                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 = "1号监测点";
                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;
                if (rb1.Checked)
                {
                    //this.chart1.Titles[0].Text = string.Format("动态 {0} 显示", rb1.Text);
                    this.chart1.Titles[0].Text = string.Format("1号监测点");
                    this.chart1.Series[0].ChartType = SeriesChartType.Line;
                }
                if (rb2.Checked)
                {
                    this.chart1.Titles[0].Text = string.Format("动态 {0} 显示", rb1.Text);
                    this.chart1.Series[0].ChartType = SeriesChartType.Spline;
                }
                this.chart1.Series[0].Points.Clear();
                //DBEngine.ConnectDB("orcl", "dt", "6312");
                dataQueue1.Clear();//清空队列中所有数据
            }
            catch (Exception ex)
            {
 
            }
        }
 
        private void InitChart2()
        {
            try
            {
                //定义图表区域
                this.chart2.ChartAreas.Clear();
                ChartArea chartArea2 = new ChartArea("C2");
                this.chart2.ChartAreas.Add(chartArea2);
                //this.chart1.Dock = DockStyle.Fill;
                //定义存储和显示点的容器
                this.chart2.Series.Clear();
                Series series2 = new Series("S2");
                series2.ChartArea = "C2";
                this.chart2.Series.Add(series2);
                //设置图表显示样式
                this.chart2.ChartAreas[0].AxisY.Minimum = 30000;
                this.chart2.ChartAreas[0].AxisY.Maximum = 50000;
                this.chart2.ChartAreas[0].AxisX.Minimum = 1;
                this.chart2.ChartAreas[0].AxisX.Maximum = 31;
                this.chart2.ChartAreas[0].AxisX.Interval = 1;
                this.chart2.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
                this.chart2.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
                //设置标题
                this.chart2.Titles.Clear();
                this.chart2.Titles.Add("S02");
                this.chart2.Titles[0].Text = "动态折线图显示";
                this.chart2.Titles[0].ForeColor = Color.RoyalBlue;
                this.chart2.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); //标题字体
                //设置图表显示样式
                this.chart2.Series[0].Color = Color.Red;
                if (rb1.Checked)
                {
                    //this.chart2.Titles[0].Text = string.Format("动态 {0} 显示", rb1.Text);
                    this.chart2.Titles[0].Text = string.Format("2号监测点");
                    this.chart2.Series[0].ChartType = SeriesChartType.Line;
                }
                if (rb2.Checked)
                {
                    this.chart2.Titles[0].Text = string.Format("动态 {0} 显示", rb1.Text);
                    this.chart2.Series[0].ChartType = SeriesChartType.Spline;
                }
                this.chart2.Series[0].Points.Clear();
                //DBEngine.ConnectDB("orcl", "dt", "6312");
                dataQueue2.Clear();//清空队列中所有数据
            }
            catch (Exception ex)
            {
 
            }
        }
        private void InitChart3()
        {
            try
            {
                //定义图表区域
                this.chart3.ChartAreas.Clear();
                ChartArea chartArea3 = new ChartArea("C3");
                this.chart3.ChartAreas.Add(chartArea3);
                //this.chart1.Dock = DockStyle.Fill;
                //定义存储和显示点的容器
                this.chart3.Series.Clear();
                Series series3 = new Series("S3");
                series3.ChartArea = "C3";
                this.chart3.Series.Add(series3);
                //设置图表显示样式
                this.chart3.ChartAreas[0].AxisY.Minimum = 30000;
                this.chart3.ChartAreas[0].AxisY.Maximum = 50000;
                this.chart3.ChartAreas[0].AxisX.Minimum = 1;
                this.chart3.ChartAreas[0].AxisX.Maximum = 31;
                this.chart3.ChartAreas[0].AxisX.Interval = 1;
                this.chart3.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
                this.chart3.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
                //设置标题
                this.chart3.Titles.Clear();
                this.chart3.Titles.Add("S03");
                this.chart3.Titles[0].Text = "动态折线图显示";
                this.chart3.Titles[0].ForeColor = Color.RoyalBlue;
                this.chart3.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); //标题字体
                //设置图表显示样式
                this.chart3.Series[0].Color = Color.Red;
                if (rb1.Checked)
                {
                    //this.chart3.Titles[0].Text = string.Format("动态 {0} 显示", rb1.Text);
                    this.chart3.Titles[0].Text = string.Format("3号监测点");
                    this.chart3.Series[0].ChartType = SeriesChartType.Line;
                }
                if (rb2.Checked)
<
  • 发表于 2022-06-10 09:49
  • 阅读 ( 442 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章