본문 바로가기
C#/DevExpress

DevExpress / Winform Bar 차트를 생성합니다

by HyunS_ 2019. 9. 29.
728x90

Bar 차트를 생성합니다

 

using System;
using System.Data;
using System.Windows.Forms;

using DevExpress.XtraCharts;

namespace Dev_Sample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            Load += mainForm_Load;
        }

        /// <summary>
        /// 메인폼 로드시 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void mainForm_Load(object sender, EventArgs e)
        {
            ChartControl chart = new ChartControl();

            chart.DataSource = CreateChartData();

            chart.SeriesDataMember = "Month";
            chart.SeriesTemplate.ArgumentDataMember = "Section";
            chart.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Value" });

            chart.SeriesTemplate.View = new StackedBarSeriesView();

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }

        private DataTable CreateChartData()
        { 
            DataTable table = new DataTable("Table");

            table.Columns.Add("Month", typeof(string));
            table.Columns.Add("Section", typeof(string));
            table.Columns.Add("Value", typeof(Int32));

            table.Rows.Add(new object[] { "Jan", "Section1", 10 });
            table.Rows.Add(new object[] { "Jan", "Section2", 20 });
            table.Rows.Add(new object[] { "Jan", "Section1", 20 });
            table.Rows.Add(new object[] { "Jan", "Section2", 30 });
            table.Rows.Add(new object[] { "Jan", "Section1", 15 });
            table.Rows.Add(new object[] { "Jan", "Section2", 25 });

            return table;
        }
    }
}

728x90

댓글