http://www.dotblogs.com.tw/suehilary/archive/2011/10/22/45430.aspx
--------------------------------------------------
客戶希望可以針對資料庫裡面的資料產生出曲線圖、直條圖、橫條圖、立體直條圖、立體橫條圖、圓餅圖、立體圓餅圖
剛聽到真是快吐血,畢竟沒做過,感覺又頗麻煩...
不過問了一下G哥,發現原來微軟收購了一家圖表元件公司(Dundas)
並釋出了一個完全免費的圖表元件「Microsoft Chart Controls 」
稍微看了一下,功能頗強...且官網還可以下載超多範例(
這裡)
使用前必須安裝組件,我是使用VS2010,忘了當初有沒有裝(半個月前的事...)
MSChart - 使用直條圖&恆條圖
01 |
using System.Web.UI.DataVisualization.Charting; |
06 |
public partial class Export_AJAX : System.Web.UI.Page |
10 |
string [] xValues = { "數值1" , "數值2" }; |
11 |
string [] titleArr = { "活動1" , "活動2" }; |
12 |
int [] yValues = {269000, 94}; |
13 |
int [] yValues2 = {120300, 116}; |
16 |
Chart Chart1 = new Chart(); |
17 |
Chart1.ChartAreas.Add( "ChartArea1" ); |
18 |
Chart1.Series.Add( "Series1" ); |
19 |
Chart1.Series.Add( "Series2" ); |
20 |
Chart1.Legends.Add( "Legends1" ); |
25 |
Title title = new Title(); |
27 |
title.Alignment = ContentAlignment.MiddleCenter; |
28 |
title.Font = new System.Drawing.Font( "Trebuchet MS" , 14F, FontStyle.Bold); |
29 |
Chart1.Titles.Add(title); |
32 |
Chart1.ChartAreas[ "ChartArea1" ].Area3DStyle.Enable3D = true ; |
33 |
Chart1.ChartAreas[ "ChartArea1" ].Area3DStyle.IsClustered = true ; |
34 |
Chart1.ChartAreas[ "ChartArea1" ].Area3DStyle.Rotation = 40; |
35 |
Chart1.ChartAreas[ "ChartArea1" ].Area3DStyle.Inclination = 50; |
36 |
Chart1.ChartAreas[ "ChartArea1" ].Area3DStyle.PointDepth = 30; |
37 |
Chart1.ChartAreas[ "ChartArea1" ].Area3DStyle.WallWidth = 0; |
38 |
Chart1.ChartAreas[ "ChartArea1" ].Area3DStyle.LightStyle = LightStyle.Realistic; |
39 |
Chart1.ChartAreas[ "ChartArea1" ].BackColor = Color.FromArgb(240, 240, 240); |
40 |
Chart1.ChartAreas[ "ChartArea1" ].AxisX2.Enabled = AxisEnabled.False; |
41 |
Chart1.ChartAreas[ "ChartArea1" ].AxisY2.Enabled = AxisEnabled.False; |
42 |
Chart1.ChartAreas[ "ChartArea1" ].AxisY2.MajorGrid.Enabled = false ; |
44 |
Chart1.ChartAreas[ "ChartArea1" ].AxisY.MajorGrid.LineColor = Color.FromArgb(150, 150, 150); |
46 |
Chart1.ChartAreas[ "ChartArea1" ].AxisX.MajorGrid.LineColor = Color.FromArgb(150, 150, 150); |
47 |
Chart1.ChartAreas[ "ChartArea1" ].AxisY.LabelStyle.Format = "#,###" ; |
52 |
Chart1.Legends[ "Legends1" ].DockedToChartArea = "ChartArea1" ; |
54 |
Chart1.Legends[ "Legends1" ].BackColor = Color.FromArgb(235, 235, 235); |
56 |
Chart1.Legends[ "Legends1" ].BackHatchStyle = ChartHatchStyle.DarkDownwardDiagonal; |
57 |
Chart1.Legends[ "Legends1" ].BorderWidth = 1; |
58 |
Chart1.Legends[ "Legends1" ].BorderColor = Color.FromArgb(200, 200, 200); |
61 |
Chart1.Series[ "Series1" ].ChartType = SeriesChartType.Column; |
63 |
Chart1.Series[ "Series1" ].Points.DataBindXY(xValues, yValues); |
64 |
Chart1.Series[ "Series1" ].Legend = "Legends1" ; |
65 |
Chart1.Series[ "Series1" ].LegendText = titleArr[0]; |
66 |
Chart1.Series[ "Series1" ].LabelFormat = "#,###" ; |
67 |
Chart1.Series[ "Series1" ].MarkerSize = 8; |
68 |
Chart1.Series[ "Series1" ].LabelForeColor = Color.FromArgb(0, 90, 255); |
70 |
Chart1.Series[ "Series1" ].Font = new System.Drawing.Font( "Trebuchet MS" , 10, System.Drawing.FontStyle.Bold); |
72 |
Chart1.Series[ "Series1" ].LabelBackColor = Color.FromArgb(150, 255, 255, 255); |
73 |
Chart1.Series[ "Series1" ].Color = Color.FromArgb(240, 65, 140, 240); |
74 |
Chart1.Series[ "Series1" ].IsValueShownAsLabel = true ; |
76 |
Chart1.Series[ "Series2" ].Points.DataBindXY(xValues, yValues2); |
77 |
Chart1.Series[ "Series2" ].Legend = "Legends1" ; |
78 |
Chart1.Series[ "Series2" ].LegendText = titleArr[1]; |
79 |
Chart1.Series[ "Series2" ].LabelFormat = "#,###" ; |
80 |
Chart1.Series[ "Series2" ].MarkerSize = 8; |
81 |
Chart1.Series[ "Series2" ].LabelForeColor = Color.FromArgb(255, 103, 0); |
82 |
Chart1.Series[ "Series2" ].Font = new System.Drawing.Font( "Trebuchet MS" , 10, FontStyle.Bold); |
83 |
Chart1.Series[ "Series2" ].LabelBackColor = Color.FromArgb(150, 255, 255, 255); |
84 |
Chart1.Series[ "Series2" ].Color = Color.FromArgb(240, 252, 180, 65); |
85 |
Chart1.Series[ "Series2" ].IsValueShownAsLabel = true ; |
86 |
Page.Controls.Add(Chart1); |
89 |
string output = "..." ; |
90 |
Label label = new Label(); |
92 |
Page.Controls.Add(label); |
跑出來的圖差不多就長這樣,其中 Series1 是藍色的,Series2 是橘色
直條圖跟橫條圖其實只要改變 SeriesChartType.Column 或 SeriesChartType.Bar 就好了,其他東西都不用做更動
只是裡面有一個 Chart1.Series["Series1"].LabelFormat = "#,###"; //金錢格式
改成橫條圖時會出錯,可能設定的格式不編準之類的?有時間在摸索
另外筆記一下:
可以看到裡面有這些宣告
1 |
Chart Chart1 = new Chart(); |
2 |
Chart1.ChartAreas.Add( "ChartArea1" ); |
3 |
Chart1.Series.Add( "Series1" ); |
4 |
Chart1.Series.Add( "Series2" ); |
5 |
Chart1.Legends.Add( "Legends1" ); |
很多範例都可以看到 Chart1.Series["Default"]
一開始接觸看不太懂,自己在 .cs 新增了 Chart1.Series.Add("Series1");
然後設確定用 Chart1.Series["Default"] ...,導致一直出現找不到 Default 之類的錯誤
因為範例大多都先在 .aspx 拉好,順便設定好,所以就亂用
後來才發現原來 Default 是自己命名的...
所以 Chart1.Series.Add("Series1"); ,就必須 Chart1.Series["Series1"]....
還有 Chart1.Series["Series1"].ChartType ... 等於 Chart1.Series[0].ChartType ...
一開始宣告一定要命名,之後要用 Chart1.Series["第n個Series"] 還是 Chart1.Series[n] 都可以
當然 ChartAreas、Legends 是一樣的道理