2015/01/11

C#.Net 使用Chart繪製長條圖


程式碼:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Series[] _series = null;
        private readonly double[] _y = new double[] { 100, 57, 93, 26, 77, 88 };
        private readonly Color[] _colors = new Color[] { Color.Peru, Color.PowderBlue, Color.RosyBrown, 
            Color.Salmon, Color.Sienna, Color.SlateBlue };
        private readonly String[] _users = new String[] { "小王", "小風", "小明", "小姿", "小玉", "小蟹" };


        private void Form1_Load(object sender, EventArgs e)
        {



            chart1.Titles.Add("長條圖");
            int _length = _y.Length;
            _series = new Series[_length];
            for (int index = 0; index < _length; index++)
            {
                _series[index] = new Series();
                _series[index].Color = _colors[index];
                _series[index].ChartType = SeriesChartType.Column;
                _series[index].Name = _users[index];
                _series[index].IsValueShownAsLabel = true;
                _series[index].Points.Add(_y[index]);
                chart1.Series.Add(_series[index]);
            }
            

        }
    }
}


執行結果: