您的位置:首页 > 编程语言 > C#

C# chart控件实时动态显示数据

2014-08-04 18:51 811 查看
这里介绍了一个最简单的实时显示数据的完整示例,

本文参考了 使用MSChart实时动态显示折线图 ,谢谢原作者的分享。

平台:VS2013,C#windows程序。

源代码下载:http://download.csdn.net/detail/lj22377/7713939

1、首先,将chart控件添加到窗口。

2、设置chart - series 主要t属性:

2.1 IsXValueIndexed = true;

2.2 XValueType = Time;

3、编写初始化函数:

private void InitChart()
{
DateTime time = DateTime.Now;
chartTimer.Interval = 1000;
chartTimer.Tick += chartTimer_Tick;
chartDemo.DoubleClick += chartDemo_DoubleClick;

Series series = chartDemo.Series[0];
series.ChartType = SeriesChartType.Spline;

chartDemo.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
chartDemo.ChartAreas[0].AxisX.ScaleView.Size = 5;
chartDemo.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chartDemo.ChartAreas[0].AxisX.ScrollBar.Enabled = true;

chartTimer.Start();

}
4、编写timer事件:

void chartTimer_Tick(object sender, EventArgs e)
{
Random ra = new Random();
Series series = chartDemo.Series[0];
series.Points.AddXY(DateTime.Now, ra.Next(1, 10));
chartDemo.ChartAreas[0].AxisX.ScaleView.Position = series.Points.Count - 5;
//throw new NotImplementedException();
}
5、编写chart双击事件。(因为滑动条可以隐藏,隐藏之后不知道怎么恢复,所以就编写了这个双击事件,以恢复滑动条)

void chartDemo_DoubleClick(object sender, EventArgs e)
{
chartDemo.ChartAreas[0].AxisX.ScaleView.Size = 5;
chartDemo.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chartDemo.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
//throw new NotImplementedException();
}


6、完整代码如下:

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 ChartRealTimeShow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitChart();
}

System.Windows.Forms.Timer chartTimer = new System.Windows.Forms.Timer();

private void InitChart() { DateTime time = DateTime.Now; chartTimer.Interval = 1000; chartTimer.Tick += chartTimer_Tick; chartDemo.DoubleClick += chartDemo_DoubleClick; Series series = chartDemo.Series[0]; series.ChartType = SeriesChartType.Spline; chartDemo.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss"; chartDemo.ChartAreas[0].AxisX.ScaleView.Size = 5; chartDemo.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true; chartDemo.ChartAreas[0].AxisX.ScrollBar.Enabled = true; chartTimer.Start(); }

void chartDemo_DoubleClick(object sender, EventArgs e) { chartDemo.ChartAreas[0].AxisX.ScaleView.Size = 5; chartDemo.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true; chartDemo.ChartAreas[0].AxisX.ScrollBar.Enabled = true; //throw new NotImplementedException(); }

void chartTimer_Tick(object sender, EventArgs e) { Random ra = new Random(); Series series = chartDemo.Series[0]; series.Points.AddXY(DateTime.Now, ra.Next(1, 10)); chartDemo.ChartAreas[0].AxisX.ScaleView.Position = series.Points.Count - 5; //throw new NotImplementedException(); }

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐