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= new Series[3];
        private readonly double[] _y = new double[] { 77, 35, 131 };
        private readonly Color[] _colors = new Color[] { Color.Peru, Color.PowderBlue, Color.RosyBrown };
        private readonly String[] _users = new String[] { "小王", "小風", "小明" };


        private void Form1_Load(object sender, EventArgs e)
        {

            chart1.Titles.Add("直線圖");

            int _length = _users.Length;

            for (int index = 0; index < _length; index++)
            {
                _series[index] = new Series(_users[index]);
                _series[index].ChartType = SeriesChartType.Column;
                _series[index].Color = _colors[index];
                _series[index].IsValueShownAsLabel = true;
            }


            for (int index = 0; index < 6; index++)
            {
                _series[0].Points.AddXY(index, _y[0] - index);
                _series[1].Points.AddXY(index, _y[1] - index);
                _series[2].Points.AddXY(index, _y[2] - index);
            }

            foreach (Series s in _series)
                chart1.Series.Add(s);
        }

    }
}



執行結果:


x軸只顯示一條,只要將資料都加入到一個序列內即可
而x軸顯示多條,則需要使用多個序列存放資料

沒有留言:

張貼留言