做一些软件时需要输出图表(折线图等),本文介绍的就是vs拥有的chart控件
当然还有其他单独的控件等,这里就不进行其他的描述了,毕竟自带的就是香
折线图如下(波形图在这里就不进行展示了)
在图表启动时一点要先进行初始化操作,操作指令如下
InitChart();
开始和停止指令如下
this.timer1.Start();//开始
this.timer1.Stop();//停止
设置图表的各种参数代码如下,可根据自身需要进行修改
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 = 5;
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 = "我也不知道";
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.Blue;
if (rb1.Checked)
{
this.chart1.Titles[0].Text = string.Format("这是个假图 {0} 显示", rb1.Text);
this.chart1.Series[0].ChartType = SeriesChartType.Line;
this.chart1.Series[0].ChartType = SeriesChartType.FastLine;
}
if (rb2.Checked)
{
this.chart1.Titles[0].Text = string.Format("这也不是真的", rb2.Text);
this.chart1.Series[0].ChartType = SeriesChartType.Spline;
this.chart1.Series[0].ChartType = SeriesChartType.FastLine;
}
this.chart1.Series[0].Points.Clear();
}
对chart数据的更新代码如下
private void UpdateQueueValue()//更新数据
{
if (dataQueue.Count > 100)
{
//先出列
for (int i = 0; i < num; i++)
{
dataQueue.Dequeue();
}
}
if (rb1.Checked)
{
Random r = new Random();
for (int i = 0; i < num; i++)
{
dataQueue.Enqueue(r.Next(0, 100));
}
}
if (rb2.Checked)
{
for (int i = 0; i < num; i++)
{
//对curValue只取[0,360]之间的值
curValue = curValue % 360;
//对得到的正玄值,放大50倍,并上移50
dataQueue.Enqueue((50 * Math.Sin(curValue * Math.PI / 180)) + 50);
curValue = curValue + 10;
}
}
}
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));
}
}
需要用到的几个变量如下
private Queue<double> dataQueue = new Queue<double>(100);
private int curValue = 0;
private int num = 5;//每次删除增加几个点
代码可以直接使用,后期将会将写一篇Y轴自适应的和生成多线的文章敬请期待