.NET chart 毫秒级坐标轴

来源:互联网 发布:婚礼沙画软件 编辑:程序博客网 时间:2024/05/29 09:15

微软的chart控件,功能强大,也非常方便好用。

这里说一下,如何用时间做X轴,坐标时间间隔可以精确到ms。

直接上代码(在VS2013 上通过):

在form1中拖入chart控件,name为chart1

拖入一个timer控件,name为timer1; 两个button;

界面如下图:


上图横坐标是取系统时间的    分:秒.毫秒

代码如下:

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;   //与chart相关的引用


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private DateTime minValue, maxValue;    //横坐标最小和最大值

        private Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            minValue = DateTime.Now;          //x轴最小刻度
            maxValue = minValue.AddSeconds(1); //X轴最大刻度,比最小刻度大1秒
            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss.fff";         //毫秒格式: hh:mm:ss.fff ,后面几个f则保留几位毫秒小数,此时要注意轴的最大值和最小值不要差太大
            chart1.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Milliseconds;
            chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 200;               //坐标值间隔200 ms
            chart1.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = false;   //防止X轴坐标跳跃
            chart1.ChartAreas[0].AxisX.MajorGrid.IntervalType = DateTimeIntervalType.Milliseconds;
            chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 200;

            chart1.ChartAreas[0].AxisX.Minimum = minValue.ToOADate();
            chart1.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate();
            chart1.Series.Clear();

            Series newSeries = new Series("Series1");
            newSeries.ChartType = SeriesChartType.Line;
            newSeries.BorderWidth = 1;
            newSeries.Color = Color.FromArgb(0, 0, 255);
            newSeries.XValueType = ChartValueType.DateTime;
            chart1.Series.Add(newSeries);
            timer1.Interval = 200;    
        }

        public void AddNewPoint(DateTime timeStamp, System.Windows.Forms.DataVisualization.Charting.Series ptSeries)
        {
            // Add new data point to its series.
            ptSeries.Points.AddXY(timeStamp.ToOADate(), rand.Next(5, 20));

            // remove all points from the source series older than 1 seconds.
            double removeBefore = timeStamp.AddSeconds((double)(1) * (-1)).ToOADate();

            //remove oldest values to maintain a constant number of data points
            while (ptSeries.Points[0].XValue < removeBefore)
            {
                ptSeries.Points.RemoveAt(0);
            }

            chart1.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
            chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddSeconds(1).ToOADate();

            chart1.Invalidate();
        }


        //添加时间数据和对应列的值 
        public void AddData()
        {
            DateTime timeStamp = DateTime.Now;

            AddNewPoint(timeStamp, chart1.Series[0]);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            AddData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

    }
}

以上代码下载地址:http://download.csdn.net/detail/flyingqd/9623405



0 0
原创粉丝点击