.NET chart 毫秒级坐标轴

微软的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



  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值