码迷,mamicode.com
首页 > 其他好文 > 详细

动态chart Demo

时间:2019-04-22 22:56:50      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:hid   class   dem   存储   model   eval   nts   img   .data   

技术图片
  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 using System.Windows.Forms.DataVisualization.Charting;
 10 using CommonLib;
 11 
 12 namespace ChartDemo
 13 {
 14     public partial class Form1 : Form
 15     {
 16 
 17         /*  涉及知识点:
 18        
 19              Chart 控件,功能强大,可以绘制柱状图,折线图,波形图,饼状图,大大简化了对图的开发与定制。
 20              Chart控件的相关概念:
 21 
 22              ChartArea,表示图表区域,一个Chart可以绘制多个ChartArea,重叠在一起。
 23              Series ,表示数据序列,每个ChartArea可以有多个数据线。即,Series属于ChartArea.
 24              AxisX,AxisY,表示主坐标轴,每一个ChartArea都有对应的坐标轴,包括主坐标轴,辅坐标轴
 25              Queue集合,表示先进先出的集合。
 26              主要有两个方法:
 27 
 28              Dequeue() 表示移除并返回位于 System.Collections.Generic.Queue<T> 开始处的对象。
 29              Enqueue() 表示将对象添加到 System.Collections.Generic.Queue<T> 的结尾处。
 30              Timer ,定时器,定时之行相应的功能,更新数据,刷新图表。        
 31          
 32           */
 33 
 34         private Queue<double> dataQueue = new Queue<double>(100);
 35         private int curValue = 0;
 36         private int num = 5;//每次删除增加几个点
 37 
 38         public Form1()
 39         {
 40             Logger.Instance.WriteLog("Start InitializeComponent Form1");
 41             InitializeComponent();
 42             Logger.Instance.WriteLog("End InitializeComponent Form1");
 43         }
 44         
 45         /// <summary>
 46         /// 初始化图表
 47         /// </summary>
 48         private void InitChart() 
 49         {
 50                 //定义图表区域
 51                 this.chart1.ChartAreas.Clear();
 52                 ChartArea chartArea1 = new ChartArea("C1");
 53                 ChartArea chartArea12 = new ChartArea("C2");
 54                 this.chart1.ChartAreas.Add(chartArea1);
 55                 this.chart1.ChartAreas.Add(chartArea12);
 56 
 57                 //定义存储和显示点的容器
 58                 this.chart1.Series.Clear();
 59                 Series series1 = new Series("S1");
 60                 series1.ChartArea = "C1";
 61                 this.chart1.Series.Add(series1);
 62 
 63                 //设置图表显示样式
 64                 this.chart1.ChartAreas[0].AxisX.Minimum = 0;
 65                 this.chart1.ChartAreas[0].AxisX.Maximum = 100;
 66                 this.chart1.ChartAreas[0].AxisX.Interval = 5;
 67                 this.chart1.ChartAreas[0].AxisY.Minimum = 0;
 68                 this.chart1.ChartAreas[0].AxisY.Maximum = 100;
 69                 this.chart1.ChartAreas[0].AxisY.Interval = 10;
 70                 this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Blue ;
 71                 this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Blue;
 72                 //设置标题
 73 
 74                 this.chart1.Titles.Clear();
 75                 this.chart1.Titles.Add("S01");
 76                 this.chart1.Titles[0].Text = "随机数显示";
 77                 this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
 78                 this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
 79 
 80                 //设置图表显示样式
 81                 this.chart1.Series[0].Color = Color.Red;         
 82             
 83             if (Rb_Brokenline.Checked)
 84             {
 85                 this.chart1.Titles[0].Text =string.Format( "随机数 {0} 显示",Rb_Brokenline.Text);
 86                 this.chart1.Series[0].ChartType = SeriesChartType.Line;
 87             }
 88             if (Rb_Waveform.Checked) {
 89                 this.chart1.Titles[0].Text = string.Format("随机数 {0} 显示", Rb_Waveform.Text);
 90                 this.chart1.Series[0].ChartType = SeriesChartType.Spline;
 91             }
 92             this.chart1.Series[0].Points.Clear();
 93         }
 94         
 95         //更新队列中的值
 96         private void UpdateQueueValue()
 97         {
 98             
 99             if (dataQueue.Count > 100) {
100                 //先出列
101                 for (int i = 0; i < num; i++)
102                 {
103                     dataQueue.Dequeue();
104                 }
105             }
106             if (Rb_Brokenline.Checked)
107             {
108                 Random r = new Random();
109                 for (int i = 0; i < num; i++)
110                 {
111                     dataQueue.Enqueue(r.Next(0, 100));
112                 }
113             }
114             if (Rb_Waveform.Checked) {
115                 for (int i = 0; i < num; i++)
116                 {
117                     //对curValue只取[0,360]之间的值
118                     curValue = curValue % 360;
119                     //对得到的正玄值,放大50倍,并上移50
120                     dataQueue.Enqueue((50*Math.Sin(curValue*Math.PI / 180))+50);
121                     curValue=curValue+10;
122                 }
123             }
124         }
125 
126 
127         /// <summary>
128         /// 初始化事件
129         /// </summary>
130         /// <param name="sender"></param>
131         /// <param name="e"></param>
132         private void Btn_Init_Click(object sender, EventArgs e)
133         {     
134             InitChart();
135         }
136 
137         /// <summary>
138         /// 开始事件
139         /// </summary>
140         /// <param name="sender"></param>
141         /// <param name="e"></param>
142         private void Btn_Start_Click(object sender, EventArgs e)
143         {
144             this.timer1.Start();
145         }
146 
147 
148          /// <summary>
149         /// 停止事件
150         /// </summary>
151         /// <param name="sender"></param>
152         /// <param name="e"></param>
153         private void Btn_Stop_Click(object sender, EventArgs e)
154         {
155             this.timer1.Stop();       
156         }
157 
158 
159         /// <summary>
160         /// 定时器事件
161         /// </summary>
162         /// <param name="sender"></param>
163         /// <param name="e"></param>
164         private void timer1_Tick_1(object sender, EventArgs e)
165         {
166             UpdateQueueValue();
167             this.chart1.Series[0].Points.Clear();
168             for (int i = 0; i < dataQueue.Count; i++)
169             {
170                 this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
171                 //add
172                 //this.chart1.Series[1].Points.AddXY((i + 1), dataQueue.ElementAt(i)+1);
173             }
174 
175         }
176 
177     }
178 }
View Code

 

动态chart Demo

标签:hid   class   dem   存储   model   eval   nts   img   .data   

原文地址:https://www.cnblogs.com/fengbaobao/p/10753334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!