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();
        private readonly double[] _y = new double[] { 77, 35, 131, 55, 77, 66 };
        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("圓餅圖");
            _series.ChartType = SeriesChartType.Pie;
            _series.IsValueShownAsLabel = true;
            _series.Points.DataBindXY(_users, _y);
            chart1.Series.Add(_series);

        }
    }
}

執行結果: