2012/08/31

C#.Net使用zedgraph套件繪製圓餅圖

畫圓餅圖必須要去呼叫到GraphPane類別的AddPieSlices方法





其方法架構如下,我們可以透過PieItem類別去建構圓餅圖的數值以及文字,或者可以宣告一個double型態的陣列以及一個string型態的陣列,去建構圓餅圖

以下是我自己實做的一個Demo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace 圓餅圖
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            drawPieSlices(zedGraphControl1);
            setSize();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            setSize();
        }

        private void setSize()
        {
            zedGraphControl1.Location = new Point(10, 10);
            zedGraphControl1.Size = new Size(ClientRectangle.Width - 50,
                           ClientRectangle.Height - 50);
        }

        private void drawPieSlices(ZedGraphControl zc)
        {
            GraphPane gp = new GraphPane();
            zc.GraphPane = gp;

            PieItem[] Pa = new PieItem[] { 
                new PieItem(1111, "test1"), 
                new PieItem(2222, "test1"),
                new PieItem(3333, "test3"),
                new PieItem(7777, "test4")
            };
           
            foreach (PieItem p in Pa)
            {
                gp.AddPieSlices(new double[] { p.Value }, new string[] { p.Label.Text });
            }
            gp.AxisChange();


        }

    }
}



參考文章:
http://zedgraph.sourceforge.net/samples.html