2011/10/24 17:57 | 閱讀數 : 7333 | 我要推薦 | 5 Comments | 文章分類 : ASP.NET C# MSChart| 訂閱
繼上一篇的直條圖
這次要筆記的是圓餅圖(Pie)
01usingSystem.Web.UI.DataVisualization.Charting;
02usingSystem.Drawing;
03
04namespaceChart.AJAX
05{
06publicpartialclassExport_AJAX : System.Web.UI.Page
07{
08voidCreateChart()
09{
10string[] xValues = { "0-20", "20-30", "30-40", "40-50", "50-60", "> 60", "unknow"};
11int[] yValues = {5, 18, 45, 17, 2, 1, 162 };
12
13//ChartAreas,Series,Legends 基本設定-------------------------------------------------
14Chart Chart1 = newChart();
15Chart1.ChartAreas.Add("ChartArea1"); //圖表區域集合
16Chart1.Legends.Add("Legends1"); //圖例集合說明
17Chart1.Series.Add("Series1"); //數據序列集合
18
19//設定 Chart-------------------------------------------------------------------------
20Chart1.Width = 770;
21Chart1.Height = 400;
22Title title = newTitle();
23title.Text = titleStr;
24title.Alignment = ContentAlignment.MiddleCenter;
25title.Font = newSystem.Drawing.Font("Trebuchet MS", 14F, FontStyle.Bold);
26Chart1.Titles.Add(title);
27
28//設定 ChartArea1--------------------------------------------------------------------
29Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = is3D;
30Chart1.ChartAreas[0].AxisX.Interval = 1;
31
32//設定 Legends-------------------------------------------------------------------------              
33//Chart1.Legends["Legends1"].DockedToChartArea = "ChartArea1"; //顯示在圖表內
34//Chart1.Legends["Legends1"].Docking = Docking.Bottom; //自訂顯示位置
35//背景色
36Chart1.Legends["Legends1"].BackColor = Color.FromArgb(235, 235, 235);
37//斜線背景
38Chart1.Legends["Legends1"].BackHatchStyle = ChartHatchStyle.DarkDownwardDiagonal;
39Chart1.Legends["Legends1"].BorderWidth = 1;
40Chart1.Legends["Legends1"].BorderColor = Color.FromArgb(200, 200, 200);
41
42//設定 Series1-----------------------------------------------------------------------
43Chart1.Series["Series1"].ChartType = SeriesChartType.Pie;
44//Chart1.Series["Series1"].ChartType = SeriesChartType.Doughnut;
45Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);
46Chart1.Series["Series1"].LegendText = "#VALX:    [ #PERCENT{P1} ]"; //X軸 + 百分比
47Chart1.Series["Series1"].Label = "#VALX\n#PERCENT{P1}"; //X軸 + 百分比
48//Chart1.Series["Series1"].LabelForeColor = Color.FromArgb(0, 90, 255); //字體顏色
49//字體設定
50Chart1.Series["Series1"].Font = newSystem.Drawing.Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold);
51Chart1.Series["Series1"].Points.FindMaxByValue().LabelForeColor = Color.Red;
52//Chart1.Series["Series1"].Points.FindMaxByValue().Color = Color.Red;
53//Chart1.Series["Series1"].Points.FindMaxByValue()["Exploded"] = "true";
54Chart1.Series["Series1"].BorderColor = Color.FromArgb(255, 101, 101, 101);
55
56//Chart1.Series["Series1"]["DoughnutRadius"] = "80"; // ChartType為Doughnut時,Set Doughnut hole size
57//Chart1.Series["Series1"]["PieLabelStyle"] = "Inside"; //數值顯示在圓餅內
58Chart1.Series["Series1"]["PieLabelStyle"] = "Outside"; //數值顯示在圓餅外
59//Chart1.Series["Series1"]["PieLabelStyle"] = "Disabled"; //不顯示數值
60//設定圓餅效果,除 Default 外其他效果3D不適用
61Chart1.Series["Series1"]["PieDrawingStyle"] = "Default";
62//Chart1.Series["Series1"]["PieDrawingStyle"] = "SoftEdge";
63//Chart1.Series["Series1"]["PieDrawingStyle"] = "Concave";
64
65//Random rnd = new Random();  //亂數產生區塊顏色
66//foreach (DataPoint point in Chart1.Series["Series1"].Points)
67//{
68//    //pie 顏色
69//    point.Color = Color.FromArgb(150, rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
70//}
71Page.Controls.Add(Chart1);
72}
73}
74}


畫出來的圓餅圖就像這樣
20111024183044727.jpg