基本信息
源码名称:Chart曲线随机运行时间轴缩放(DataVisualization.Charting)
源码大小:0.06M
文件格式:.rar
开发语言:C#
更新时间:2020-07-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

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 Chart1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            InitChart();
            chart1.MouseWheel  = new MouseEventHandler(myMouseWheel);

        }
        private void InitChart()
        {

            DateTime time = DateTime.Now;
            Series series = chart1.Series[0];
            series.ChartType = SeriesChartType.Spline;


            #region chart1  
            chart1.Series[0].IsValueShownAsLabel = true;//让点集0在图像上显示数值
            chart1.Series[0].SmartLabelStyle.Enabled = true;
            chart1.Series[0].XValueType = ChartValueType.DateTime;//坐标轴type改为时间

            //去掉滚动条的按钮
            chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.None;
            chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
            chart1.ChartAreas[0].AxisX.ScrollBar.Size = 20;
            chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Seconds;
            chart1.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Seconds;
            chart1.ChartAreas[0].AxisX.ScaleView.Size = 20;
            chart1.ChartAreas[0].AxisX.ScaleView.MinSize = 15;
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1;
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Seconds;
            chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;
            //这个interval可以用来修改显示间隔
            chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("00:00:01").Second;
            chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
            chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
            chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 1;
            chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
            //显示格式为时:分:秒
            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
            chart1.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
            chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.LightGray;
            #endregion


            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();

            //DateTime time = DateTime.Now;
            //Series series = chart1.Series[0];
            //series.ChartType = SeriesChartType.Spline;
            //chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
            //chart1.ChartAreas[0].AxisY.Interval = 5;
            //chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
            //chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
            //chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
            //chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
            //chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
            //chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            //chart1.ChartAreas[0].CursorX.Interval = 0;
            //chart1.ChartAreas[0].CursorX.IntervalOffset = 0;
            //chart1.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Minutes;
            //chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            //chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;

            //timer1.Enabled = true;
            //timer1.Interval = 1000;
            //timer1.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.chart1.ChartAreas[0].AxisX.ScaleView.Scroll(ScrollType.Last);
            string now = DateTime.Now.ToLongTimeString();
            DateTime time = DateTime.Parse(now);
            this.chart1.Series[0].Points.AddXY(time, DateTime.Now.Second);
            Random ra = new Random();
            Series series = chart1.Series[0];
            series.Points.AddXY(DateTime.Now, ra.Next(1, 100));
        }

        private int IntervalNow = 0;
        private string[] deltas = new string[] {"00:00:01",
                                                "00:00:10",
                                                "00:00:30",
                                                "00:01:00",
                                                "00:05:00",
                                                "00:10:00",
                                                "00:30:00",
                                                "00:45:00",
                                                "01:00:00"};

        public DateTime X_minValue { get; private set; }

        void myMouseWheel(object sender, MouseEventArgs e)

        {


            if (e.Delta > 0)
            {//放大
                if (((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size >= 0.5)
                    ((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size = ((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size * 0.5;

                if (Convert.ToInt32(((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size) >= 20)
                {
                    --IntervalNow;
                    if (IntervalNow < 0)
                        IntervalNow = 0;

                    ((Chart)(sender)).ChartAreas[0].AxisX.Interval = DateTime.Parse(deltas[IntervalNow]).Second;

                }
                else
                {
                    IntervalNow = 0;
                    ((Chart)(sender)).ChartAreas[0].AxisX.Interval = DateTime.Parse(deltas[IntervalNow]).Second;
                }

            }
            else
            {
                if (((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size <= 100000)
                    ((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size = ((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size * 2;

                if (((Chart)(sender)).ChartAreas[0].AxisX.ScaleView.Size >= 20)
                {
                      IntervalNow;
                    if (IntervalNow >= 9)
                        IntervalNow = 8;

                    ((Chart)(sender)).ChartAreas[0].AxisX.Interval = DateTime.Parse(deltas[IntervalNow]).Second;
                }
                else
                {
                    IntervalNow = 0;
                    ((Chart)(sender)).ChartAreas[0].AxisX.Interval = DateTime.Parse(deltas[IntervalNow]).Second;
                }

            }
        }
    }
}