1. 程式人生 > >C#使用Ms Chart 實現餅圖

C#使用Ms Chart 實現餅圖


      //餅圖   dt資料結構為 columndata(資料)  columnname(文字) 這兩列
        private void ViewChart(DataTable _dt, string _title)
        {
            this.ct_coll.Series.Clear();
            this.ct_coll.Legends.Clear();

            this.ct_coll.Series.Add(new Series("Data"));
            this.ct_coll.Legends.Add(new Legend("Stores")); //右邊標籤列
            
            this.ct_coll.Series["Data"].ChartType = SeriesChartType.Pie;
            this.ct_coll.Series["Data"]["PieLabelStyle"] = "Inside";//Inside 數值顯示在圓餅內 Outside 數值顯示在圓餅外 Disabled 不顯示數值

            this.ct_coll.Series["Data"]["PieLineColor"] = "Black";

            
            //this.ct_coll.Series["Data"].IsValueShownAsLabel = true;
            //this.ct_coll.Series["Data"].IsVisibleInLegend = true;
            //this.ct_coll.Series["Data"].ShadowOffset = 1;//陰影偏移量

            this.ct_coll.Series["Data"].ToolTip = "#VAL{D} 人";//滑鼠移動到上面顯示的文字
            this.ct_coll.Series["Data"].BackSecondaryColor = Color.DarkCyan;
            this.ct_coll.Series["Data"].BorderColor = Color.DarkOliveGreen;
            this.ct_coll.Series["Data"].LabelBackColor = Color.Transparent;

            foreach (DataRow dr in _dt.Rows)
            {
                int ptIdx = this.ct_coll.Series["Data"].Points.AddY(Convert.ToDouble(dr["columndata"].ToString()));
                    DataPoint pt = this.ct_coll.Series["Data"].Points[ptIdx];
                    pt.LegendText = dr["columnname"].ToString() + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 人" + " ]";//右邊標籤列顯示的文字
                    pt.Label = dr["columnname"].ToString() + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 人"+" ]"; //圓餅外顯示的資訊
                
                  //  pt.LabelToolTip = "#PERCENT{P2}";
                    //pt.LabelBorderColor = Color.Red;//文字背景色 
            }
           
           // this.ct_coll.Series["Data"].Label = "#PERCENT{P2}"; //
            this.ct_coll.Series["Data"].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            this.ct_coll.Series["Data"].Legend = "Stores"; //右邊標籤列顯示資訊
            this.ct_coll.Legends["Stores"].Alignment = StringAlignment.Center;
            this.ct_coll.Legends["Stores"].HeaderSeparator = System.Windows.Forms.DataVisualization.Charting.LegendSeparatorStyle.ThickLine;


            this.ct_coll.Titles[0].Text = _title;
            this.ct_coll.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
            this.ct_coll.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;

            int int_count = _dt.AsEnumerable().Select(t => t.Field<int>("columndata")).Sum();
            this.ct_coll.Titles[1].Text = "總人數: " + int_count.ToString("0.00") + " 人";

            /*
             #VALX      顯示當前圖例的X軸的對應文字(或資料)
  #VAL, #VALY,  顯示當前圖例的Y軸的對應文字(或資料)
  #VALY2, #VALY3, 顯示當前圖例的輔助Y軸的對應文字(或資料)
  #SER:      顯示當前圖例的名稱
  #LABEL       顯示當前圖例的標籤文字
  #INDEX      顯示當前圖例的索引
  #PERCENT       顯示當前圖例的所佔的百分比
  #TOTAL      總數量
  #LEGENDTEXT      圖例文字
             */
        }



效果圖: