차트의 Bar의 색상을 변경합니다.

 

using System;
using System.Data;
using System.Drawing;
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 SideBySideBarSeriesView();

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.BoundDataChanged += chart_BoundDataChanged;

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

        private void chart_BoundDataChanged(object sender, EventArgs e)
        {
            ChartControl chart = (ChartControl)sender;

            for (int i = 0; i < chart.Series.Count; i++)
            {
                BarSeriesView view = (BarSeriesView)chart.Series[i].View;
                view.Color = Color.Blue;
                view.FillStyle.FillMode = FillMode.Gradient;
                ((GradientFillOptionsBase)view.FillStyle.Options).Color2 = Color.Beige;
            }
        }

        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"  , "Section3", 40 });
            table.Rows.Add(new object[] { "Feb"  , "Section1", 20 });
            table.Rows.Add(new object[] { "Feb"  , "Section2", 30 });
            table.Rows.Add(new object[] { "Feb"  , "Section3", 80 });
            table.Rows.Add(new object[] { "March", "Section1", 30 });
            table.Rows.Add(new object[] { "March", "Section2", 40 });
            table.Rows.Add(new object[] { "March", "Section3", 100 });

            return table;
        }
    }
}

 

차트에 spline, line, 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 SideBySideBarSeriesView();

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.SeriesNameTemplate.BeginText = "Month : ";

            chart.BoundDataChanged += chart_BoundDataChanged;

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

        private void chart_BoundDataChanged(object sender, EventArgs e)
        {
            ChartControl chart = (ChartControl)sender;

            Series series = chart.GetSeriesByName("Month : Feb");

            if(series != null)
                series.ChangeView(ViewType.Line);

            Series march = chart.GetSeriesByName("Month : March");

            if(march != null)
                march.ChangeView(ViewType.Spline);
        }

        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"  , "Section3", 40 });
            table.Rows.Add(new object[] { "Feb"  , "Section1", 20 });
            table.Rows.Add(new object[] { "Feb"  , "Section2", 30 });
            table.Rows.Add(new object[] { "Feb"  , "Section3", 80 });
            table.Rows.Add(new object[] { "March", "Section1", 30 });
            table.Rows.Add(new object[] { "March", "Section2", 40 });
            table.Rows.Add(new object[] { "March", "Section3", 100 });

            return table;
        }
    }
}

 

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;
        }
    }
}

Bar 생성하기 입니다.

 

Bar 생성하기 예제

MainForm.cs 입니다.

using System.Windows.Forms;

using DevExpress.XtraBars;

namespace WindowsFormsApp2
{
    /// <summary>
    /// 메인폼 클래스 입니다.
    /// </summary>
    public partial class MainForm : Form
    {
        // Constructor (Public)

        #region MainForm() - 메인폼 생성자 입니다.

        /// <summary>
        /// 메인폼 생성자 입니다.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region Bar를 생성합니다.

            BarManager barManager = new BarManager();
            barManager.Form = this;

            barManager.BeginUpdate();

            Bar bar1 = new Bar(barManager, "My MainMenu");
            Bar bar2 = new Bar(barManager, "My Bar");

            bar1.DockStyle = BarDockStyle.Top;
            bar2.DockStyle = BarDockStyle.Top;

            bar1.DockRow = 0;

            barManager.MainMenu = bar1;

            BarSubItem subMenuFile = new BarSubItem(barManager, "File");
            BarSubItem subMenuEdit = new BarSubItem(barManager, "Edit");
            BarSubItem subMenuView = new BarSubItem(barManager, "View");

            BarButtonItem buttonOpen       = new BarButtonItem(barManager, "Open"  );
            BarButtonItem buttonExit       = new BarButtonItem(barManager, "Exit"  );
            BarButtonItem buttonCopy       = new BarButtonItem(barManager, "Copy"  );
            BarButtonItem buttonCut        = new BarButtonItem(barManager, "Cut"   );
            BarButtonItem buttonViewOutput = new BarButtonItem(barManager, "Output");

            subMenuFile.AddItems(new BarItem[] { buttonOpen, buttonExit });
            subMenuFile.AddItems(new BarItem[] { buttonCopy, buttonCut  });
            subMenuView.AddItem (buttonViewOutput);

            bar1.AddItems(new BarItem[] { subMenuFile, subMenuEdit, subMenuView });

            bar2.AddItem(buttonViewOutput);

            barManager.EndUpdate();

            #endregion

            #region 이벤트 동작입니다.

            barManager.ItemClick += barManager_ItemClick;

            #endregion
        }

        #endregion

        #region barManager_ItemClick(sender, e) - Bar 아이템 클릭시 동작합니다.
        /// <summary>
        /// Bar 아이템 클릭시 동작합니다.
        /// </summary>
        /// <param name="sender">이벤트 발생자 입니다.</param>
        /// <param name="e">이벤트 인자 입니다.</param>
        private void barManager_ItemClick(object sender, ItemClickEventArgs e)
        {
            BarSubItem subMenu = e.Item as BarSubItem;

            if(subMenu != null) return;

            MessageBox.Show("Item " + e.Item.Caption + " has been clicked");
        }

        #endregion
    }
}

 

+ Recent posts