2015/01/28

C#.Net Chart scrollbar move

有些人會希望在畫曲線時,能立即看到最新的曲線
那我們知道在國小畫曲線時,只有X跟Y軸
所以聰明的你應該知道要去改變到X跟Y軸的位置
//如果你要改變X軸位置則可以去修改ScaleView的Position
chart.ChartAreas[0].AxisX.ScaleView.Position = number;

//Y軸則是
chart.ChartAreas[0].AxisY.ScaleView.Position = number;

下方範例則是按一次按鈕,就移動30個x點

XML:
namespace Sample
{
    partial class Form1
    {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 設計工具產生的程式碼

        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
        /// 修改這個方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
            this.SuspendLayout();
            // 
            // chart1
            // 
            chartArea2.Name = "ChartArea1";
            this.chart1.ChartAreas.Add(chartArea2);
            legend2.Name = "Legend1";
            this.chart1.Legends.Add(legend2);
            this.chart1.Location = new System.Drawing.Point(12, 1);
            this.chart1.Name = "chart1";
            this.chart1.Size = new System.Drawing.Size(523, 393);
            this.chart1.TabIndex = 0;
            this.chart1.Text = "chart1";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 401);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Move";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(547, 443);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.chart1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
        private System.Windows.Forms.Button button1;


    }
}



Code:
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
 
namespace Sample
{
    public partial class Form1 : Form
    {
        private Thread thread;
        //標題 最大數值
        private Series series1 = new Series("第一條線");

        private Chart chart;
        private int index = 0;
        protected double MaxPosition { get { return chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum; } }
        protected double position { get { return chart.ChartAreas[0].AxisX.ScaleView.Position; } }

        public Form1()
        {
            InitializeComponent();
            chart = chart1;
            //將序列新增到圖上
            chart1.Series.Add(series1);

            series1.ChartType = SeriesChartType.Line;
            //讓使用者可以選取想看得範圍
            chart1.ChartAreas[0].CursorY.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;

            //設定選取捲軸
            chart1.ChartAreas[0].CursorX.AutoScroll = true;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(0, 30);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //標題
            this.chart1.Titles.Add("標題");
            thread = new Thread(new ThreadStart(Run));
            thread.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((position + 30) < MaxPosition)
                chart.ChartAreas[0].AxisX.ScaleView.Position += 30;
            else
                chart.ChartAreas[0].AxisX.ScaleView.Position = 0;
        }

        private void Run()
        {
            chart.Invoke(new MethodInvoker(delegate()
            {
                while (index < 100)
                {
                    for (int i = 1; i <= 30; i++)
                        series1.Points.AddY(index * i);

                    index++;
                }
            }));
        }

    }
}

執行結果: