■ ChartControl 클래스 : 실시간 차트 만들기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

using DevExpress.Utils;

using DevExpress.XtraCharts;

using DevExpress.XtraEditors;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : XtraForm

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 타이머

        /// </summary>

        private Timer timer;

 

        /// <summary>

        /// 난수기

        /// </summary>

        private Random random = new Random();

 

        /// <summary>

        /// 시리즈 배열

        /// </summary>

        private Series[] seriesArray = new Series[2];

 

        /// <summary>

        /// 인덱스

        /// </summary>

        private int index = 0;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 차트 컨트롤을 설정한다.

 

            this.seriesArray[0] = new Series("시리즈1", ViewType.Line);

            this.seriesArray[1] = new Series("시리즈2", ViewType.Line);

            

            this.chartControl.Series.Add(this.seriesArray[0]);

            this.chartControl.Series.Add(this.seriesArray[1]);

            

            this.chartControl.CrosshairEnabled = DefaultBoolean.False;

            

            XYDiagram diagram = (XYDiagram)this.chartControl.Diagram;

            

            diagram.AxisY.WholeRange.Auto             = false;

            diagram.AxisY.WholeRange.MinValue         = -100;

            diagram.AxisY.WholeRange.MaxValue         = 200;

            diagram.AxisX.WholeRange.SideMarginsValue = 0;

            

            ConstantLine constantLine = new ConstantLine();

            

            constantLine.Color        = Color.LightYellow;

            constantLine.AxisValue    = 0;

            constantLine.ShowInLegend = false;

 

            diagram.AxisY.ConstantLines.Add(constantLine);

            

            diagram.EnableAxisXScrolling = false;

            diagram.EnableAxisXZooming   = false;

 

            #endregion

            #region 타이머를 설정한다.

 

            this.timer = new Timer();

 

            this.timer.Interval = 300;

 

            #endregion

            #region 이벤트를 설정한다.

 

            Load            += Form_Load;

            this.timer.Tick += timer_Tick;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            this.timer.Start();

        }

 

        #endregion

        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void timer_Tick(object sender, EventArgs e)

        {

            if(this.seriesArray[0].Points.Count > 10)

            {

                this.seriesArray[0].Points.RemoveAt(0);

                this.seriesArray[1].Points.RemoveAt(0);

            }

            

            this.seriesArray[0].Points.Add(new SeriesPoint(this.index, this.random.Next(-100, 200)));

            this.seriesArray[1].Points.Add(new SeriesPoint(this.index, this.random.Next( -50, 100)));

 

            this.index++;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요