2010年4月10日

[C#] - 使用zedgraph做圓餅圖

要製作圓餅圖有現成的套件可以用

在.Net Framework 3.5也有收納一些Chart的控制項.

今天要介紹的是在.Net Framework 2.0底下使用的.

首先要先下載一個外掛套件zedgraph

,在之前的文章有介紹過哩!!Phate - 畫圖表的套件ZedGraph

今天要介紹的是圓餅圖的繪製:



















Code如下:
private void CreateChart()
        {
            zgc.GraphPane.CurveList.Clear();
            GraphPane myPane = zgc.GraphPane;

            // Set the GraphPane title
            myPane.Title.Text = "圓餅圖";
            //myPane.Title.FontSpec.IsItalic = false;
            myPane.Title.FontSpec.Size = 24f;
            myPane.Title.FontSpec.Family = "新細明體";

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.Goldenrod, 45.0f);
            // No fill for the chart background
            myPane.Chart.Fill.Type = FillType.None;

            // Set the legend to an arbitrary location
            myPane.Legend.Position = LegendPos.Float;
            myPane.Legend.Location = new Location(0f, 0f, CoordType.PaneFraction,
                           AlignH.Left, AlignV.Top);
            myPane.Legend.FontSpec.Size = 15f;
            myPane.Legend.IsHStack = false;

            PieItem[] s0 = myPane.AddPieSlices(ChartCost.ToArray(), ChartCategory.ToArray());//增加項目
            int i=0;
            while(i.LabelType = PieLabelType.Name_Value_Percent;
                s0[i>.LabelDetail.FontSpec.Size = 15f;//設定圓餅圖旁邊的label字體大小(預設值為10)
                i++;
            }
            zgc.AxisChange();
            zgc.Refresh();
        }

其中:
PieItem[] s0 = myPane.AddPieSlices(ChartNumber.ToArray(), Chartlabel.ToArray());

這行的目的是要把數值陣列與標籤字串陣列放入.

當然也有單筆設定的指令,如下:
PieItem s=myPane.AddPieSlice(chartnumber,Color.Red,0,chartlabel);

參考資料:

http://zedgraph.org/wiki/index.php?title=Pie_Chart_Demo
http://zedgraph.sourceforge.net/documentation/default.html

沒有留言:

張貼留言