첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ ChartHelper.cs

using System;
using System.Collections.Generic;
using System.Drawing;

using Arction.WinForms.Charting;
using Arction.WinForms.Charting.Axes;
using Arction.WinForms.Charting.Views.ViewXY;

namespace TestProject
{
    /// <summary>
    /// 차트 헬퍼
    /// </summary>
    public static class ChartHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 단일 색상 리스트
        /// </summary>
        public static List<Color> SolidColorList = new List<Color>
        {
            Color.FromArgb(187, 28 , 0 ),
            Color.FromArgb(222, 54 , 11),
            Color.FromArgb(199, 86 , 10),
            Color.FromArgb(255, 160, 0 ),
            Color.FromArgb(255, 124, 0 )
        };

        /// <summary>
        /// 반투명 색상 리스트
        /// </summary>
        public static List<Color> TranslucentColorList = new List<Color>
        {
            Color.FromArgb(180, SolidColorList[0]),
            Color.FromArgb(180, SolidColorList[1]),
            Color.FromArgb(180, SolidColorList[2]),
            Color.FromArgb(180, SolidColorList[3]),
            Color.FromArgb(180, SolidColorList[4])
        };

        #endregion

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

        #region Field

        /// <summary>
        /// 복구 정보
        /// </summary>
        private static RestoreInfo _restoreInfo;

        /// <summary>
        /// 배경 색상
        /// </summary>
        private static Color _backgroundColor = Color.FromArgb(255, 30, 30, 30);

        /// <summary>
        /// 제목 색상
        /// </summary>
        private static Color _titleColor = Color.FromArgb(255, 249, 202, 3);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 리스트 데이터 지우기 - ClearListData<TItem>(sourceList)

        /// <summary>
        /// 리스트 데이터 지우기
        /// </summary>
        /// <typeparam name="TItem">항목 타입</typeparam>
        /// <param name="sourceList">소스 리스트</param>
        public static void ClearListData<TItem>(List<TItem> sourceList) where TItem : IDisposable
        {
            if(sourceList == null)
            {
                return;
            }

            while(sourceList.Count > 0)
            {
                int lastIndex = sourceList.Count - 1;

                TItem item = sourceList[lastIndex];

                sourceList.RemoveAt(lastIndex);

                if(item != null)
                {
                    (item as IDisposable).Dispose();
                }
            }
        }

        #endregion

        #region 다크/플랫 스타일 설정하기 - SetDarkFlatStyle(chart, minorTickOption)

        /// <summary>
        /// 다크/플랫 스타일 설정하기
        /// </summary>
        /// <param name="chart">차트</param>
        /// <param name="minorTickOption">작은 눈금 옵션</param>
        public static void SetDarkFlatStyle(LightningChartUltimate chart, MinorTickOption minorTickOption = MinorTickOption.None)
        {
            _restoreInfo = new RestoreInfo(chart);

            chart.Background.Color        = _backgroundColor;
            chart.Background.GradientFill = GradientFill.Solid;

            chart.ViewXY.GraphBackground.Color        = Color.FromArgb(255, 20, 20, 20);
            chart.ViewXY.GraphBackground.GradientFill = GradientFill.Solid;

            chart.Title.Color          = _titleColor;
            chart.Title.MouseHighlight = MouseOverHighlight.None;

            bool showXMinotTick = false;
            bool showYMinorTick = false;

            if(minorTickOption == MinorTickOption.Both)
            {
                showXMinotTick = showYMinorTick = true;
            }
            else if(minorTickOption == MinorTickOption.OnlyX)
            {
                showXMinotTick = true;
            }
            else if(minorTickOption == MinorTickOption.OnlyY)
            {
                showYMinorTick = true;
            }

            foreach(AxisY axisY in chart.ViewXY.YAxes)
            {
                axisY.Title.Color          = _titleColor;
                axisY.Title.MouseHighlight = MouseOverHighlight.None;

                axisY.MajorGrid.Color   = Color.FromArgb(35, 255, 255, 255);
                axisY.MajorGrid.Pattern = LinePattern.Solid;

                axisY.MinorDivTickStyle.Visible = showYMinorTick;
            }

            foreach(AxisX axisX in chart.ViewXY.XAxes)
            {
                axisX.Title.Color          = _titleColor;
                axisX.Title.MouseHighlight = MouseOverHighlight.None;

                axisX.MajorGrid.Color   = Color.FromArgb(35, 255, 255, 255);
                axisX.MajorGrid.Pattern = LinePattern.Solid;

                axisX.MinorDivTickStyle.Visible = showXMinotTick;
            }

            if(chart.ViewXY.LegendBoxes != null)
            {
                foreach(LegendBoxXY legendBox in chart.ViewXY.LegendBoxes)
                {
                    legendBox.Shadow.Visible = false;
                }
            }
        }

        #endregion
        #region 다크 플랫 스타일 취소하기 - CancelDarkFlatStyle(chart)

        /// <summary>
        /// 다크 플랫 스타일 취소하기
        /// </summary>
        /// <param name="chart">차트</param>
        public static void CancelDarkFlatStyle(LightningChartUltimate chart)
        {
            if(_restoreInfo != null)
            {
                if(chart != null)
                {
                    chart.BeginUpdate();

                    chart.Background.Color        = _restoreInfo.ChartColor;
                    chart.Background.GradientFill = _restoreInfo.ChartGradientFill;

                    chart.ViewXY.GraphBackground.Color        = _restoreInfo.ViewColor;
                    chart.ViewXY.GraphBackground.GradientFill = _restoreInfo.viewGradientFill;

                    foreach(var yAxis in chart.ViewXY.YAxes)
                    {
                        yAxis.MajorGrid.Color   = _restoreInfo.GridColor;
                        yAxis.MajorGrid.Pattern = _restoreInfo.GridLinePattern;

                        yAxis.MinorDivTickStyle.Visible = true;
                    }

                    foreach(var xAxis in chart.ViewXY.XAxes)
                    {
                        xAxis.MajorGrid.Color   = _restoreInfo.GridColor;
                        xAxis.MajorGrid.Pattern = _restoreInfo.GridLinePattern;

                        xAxis.MinorDivTickStyle.Visible = true;
                    }

                    chart.EndUpdate();
                }
            }
        }

        #endregion
    }
}

 

▶ MainForm.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using Arction.WinForms.Charting;
using Arction.WinForms.Charting.Axes;
using Arction.WinForms.Charting.SeriesXY;
using Arction.WinForms.Charting.Views.ViewXY;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 차트
        /// </summary>
        private LightningChartUltimate chart = null;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            InitializeChart();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 뷰 XY 확대/축소시 처리하기 - ViewXY_Zoomed(sender, e)

        /// <summary>
        /// 뷰 XY 확대/축소시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void ViewXY_Zoomed(object sender, ZoomedXYEventArgs e)
        {
            ViewXY viewXY = sender as ViewXY;
            AxisX axisX   = viewXY.XAxes[0];

            double minimum = 0;
            double maximum = 1000;

            double currentMinimum = axisX.Minimum;
            double currentMaximum = axisX.Maximum;

            if(minimum > currentMinimum || maximum < currentMaximum)
            {
                if(minimum > currentMinimum)
                {
                    currentMinimum = minimum;
                }

                if(maximum < currentMaximum)
                {
                    currentMaximum = maximum;
                }

                viewXY.OwnerChart.BeginUpdate();

                axisX.SetRange(currentMinimum, currentMaximum);

                viewXY.OwnerChart.EndUpdate();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 차트 포인트 리스트 구하기 - GetChartPointList(random, pointCount, minimumAmplitude, maximumAmplitude)

        /// <summary>
        /// 차트 포인트 리스트 구하기
        /// </summary>
        /// <param name="random">난수기</param>
        /// <param name="pointCount">포인트 수</param>
        /// <param name="minimumAmplitude">최소 크기</param>
        /// <param name="maximumAmplitude">최대 크기</param>
        /// <returns>차트 포인트 리스트</returns>
        private List<ChartPoint> GetChartPointList(Random random, int pointCount, double minimumAmplitude, double maximumAmplitude)
        {
            double stepX = 1000 / (pointCount - 1);

            double previousY = minimumAmplitude;

            List<ChartPoint> list = new List<ChartPoint>();

            for(int i = 0; i < pointCount; i++)
            {
                ChartPoint point = new ChartPoint();

                point.X = (double)i * stepX;

                double y = previousY + (random.NextDouble() - 0.5) * maximumAmplitude / 5;

                if(y < minimumAmplitude / 2)
                {
                    y = minimumAmplitude;
                }

                if(y > maximumAmplitude * 1.5)
                {
                    y = maximumAmplitude;
                }

                point.Y = y;

                list.Add(point);

                previousY = y;
            }

            return list;
        }

        #endregion
        #region 시리즈 데이터 리스트 구하기 - GetSeriesDataList(pointCount)

        /// <summary>
        /// 시리즈 데이터 리스트 구하기
        /// </summary>
        /// <param name="pointCount">포인트 수</param>
        /// <returns>시리즈 데이터 리스트</returns>
        private List<SeriesData> GetSeriesDataList(int pointCount)
        {
            Random random = new Random();

            List<SeriesData> list = new List<SeriesData>();

            SeriesData data;

            data = new SeriesData();

            data.ChartPointList = GetChartPointList(random, pointCount, 25000.0, 50000.0);

            list.Add(data);

            data = new SeriesData();

            data.ChartPointList = GetChartPointList(random, pointCount, 18000.0, 30000.0);

            list.Add(data);

            data = new SeriesData();

            data.ChartPointList = GetChartPointList(random, pointCount, 5000.0, 15000.0);

            list.Add(data);

            return list;
        }

        #endregion
        #region 차트 데이터 설정하기 - SetChartData(viewXY, axisX, axisY, seriesDataList)

        /// <summary>
        /// 차트 데이터 설정하기
        /// </summary>
        /// <param name="viewXY">뷰 XY</param>
        /// <param name="axisX">X축</param>
        /// <param name="axisY">Y축</param>
        /// <param name="seriesDataList">시리즈 데이터 리스트</param>
        private void SetChartData(ViewXY viewXY, AxisX axisX, AxisY axisY, List<SeriesData> seriesDataList)
        {
            for(int i = 0; i < 3; i++)
            {
                AreaSeries series = new AreaSeries(viewXY, axisX, axisY);

                series.Fill.Color             = ChartHelper.TranslucentColorList[i];
                series.Fill.GradientFill      = GradientFill.Solid;
                series.Fill.GradientDirection = 90;
                series.LineStyle.Color        = ChartHelper.SolidColorList[4];
                series.LineStyle.Width        = 2f;
                series.MouseHighlight         = MouseOverHighlight.None;
                series.MouseInteraction       = false;
                series.Title.Text             = "시리즈 " + (i + 1).ToString();

                SeriesData seriesData = seriesDataList[i];

                List<ChartPoint> pointList = seriesData.ChartPointList;

                AreaSeriesPoint[] pointArray = new AreaSeriesPoint[pointList.Count];

                for(int j = 0; j < pointList.Count; j++)
                {
                    pointArray[j].X = pointList[j].X;
                    pointArray[j].Y = pointList[j].Y;
                }

                series.AddValues(pointArray);

                viewXY.AreaSeries.Add(series);
            }
        }

        #endregion
        #region 차트 초기화하기 - InitializeChart()

        /// <summary>
        /// 차트 초기화하기
        /// </summary>
        private void InitializeChart()
        {
            this.chart = new LightningChartUltimate();
            
            this.chart.BeginUpdate();
            
            this.chart.Name   = "chart";
            this.chart.Parent = this;
            this.chart.Dock   = DockStyle.Fill;

            this.chart.Title.Font = Font;
            this.chart.Title.Text = "영역 차트";

            ViewXY viewXY = this.chart.ViewXY;

            viewXY.ZoomPanOptions.MouseWheelZooming = MouseWheelZooming.Horizontal;
            viewXY.ZoomPanOptions.PanDirection      = PanDirection.Horizontal;

            viewXY.Zoomed += ViewXY_Zoomed;
            
            AxisX axisX = this.chart.ViewXY.XAxes[0];

            axisX.Title.Font = Font;
            axisX.Title.Text = "X축";
            axisX.ValueType  = AxisValueType.Number;
            axisX.ScrollMode = XAxisScrollMode.None;

            AxisY axisY = this.chart.ViewXY.YAxes[0];

            axisY.Title.Font     = Font;
            axisY.Title.Text     = "Y축";
            axisY.PanningEnabled = false;

            ChartHelper.SetDarkFlatStyle(this.chart);

            List<SeriesData> seriesDataList = GetSeriesDataList(500);

            SetChartData(viewXY, axisX, axisY, seriesDataList);

            viewXY.ZoomToFit();

            LegendBoxXY legendBoxXY = this.chart.ViewXY.LegendBoxes[0];

            legendBoxXY.Position = LegendBoxPositionXY.SegmentBottomRight;

            this.chart.EndUpdate();
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요