澳门永利娱乐总站都好难找.

改 Legend 属性就能够了,只要简单加二个 item 就行了.

http://www.dotblogs.com.tw/suehilary/archive/2011/10/22/45430.aspx

坐标则是 ChartArea 控制


都好难找.

MS Chart Control 學習手記(一) – 直條圖&橫條圖

 

20120%/22 09:53 | 閱讀數 : 1075
| 作者要推薦 | Add
Comment

| 文章分類 : ASP.NET
C#

MSChart |
訂閱

客戶希望得以針對資料庫裡面包车型客车資料產生出曲線圖、直條圖、橫條圖、立體直條圖、立體橫條圖、圓餅圖、立體圓餅圖

剛聽到真是快肺痈,畢竟沒做過,感覺又頗麻煩…

不過問了弹指间G哥,發現原來微軟收購了一家圖表元件集团(Dundas)

並釋出了一個通通免費的圖表元件「Microsoft Chart Controls 」

稍稍看了一下,作用頗強…且官網還能够下載超多範例(這裡)

 

使用前必須安裝組件,小编是选择VS二〇〇八,忘了當初有沒有裝(半個月前的事…)

Microsoft Chart Controls for Microsoft .NET Framework
3.5

Microsoft Chart Controls for Microsoft .NET Framework 3.5
語言套件

Microsoft Chart Controls Add-on for Microsoft Visual Studio
2008

(裝完後能够在工具箱拖曳使用)

Microsoft Chart Controls for .NET Framework
Documentation

(安裝及使用說明文件)

 

MSChart – 使用直條圖&恆條圖

01 using System.Web.UI.DataVisualization.Charting;
02 using System.Drawing;
03  
04 namespace Chart.AJAX
05 {
06     public partial class Export_AJAX : System.Web.UI.Page
07     {
08         void CreateChart()
09         {
10             string[] xValues = { "數值1", "數值2" };
11             string[] titleArr = {"活動1", "活動2"};
12             int[] yValues = {269000, 94};
13             int[] yValues2 = {120300, 116};
14  
15             //ChartAreas,Series,Legends 基本設定--------------------------------------------------
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"); //圖例集合
21  
22             //設定 Chart
23             Chart1.Width = 700;
24             Chart1.Height = 400;
25             Title title = new Title();
26             title.Text = "長條圖";
27             title.Alignment = ContentAlignment.MiddleCenter;
28             title.Font = new System.Drawing.Font("Trebuchet MS", 14F, FontStyle.Bold);
29             Chart1.Titles.Add(title);
30  
31             //設定 ChartArea----------------------------------------------------------------------
32             Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true; //3D效果
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; //隱藏 X2 標示
41             Chart1.ChartAreas["ChartArea1"].AxisY2.Enabled = AxisEnabled.False; //隱藏 Y2 標示
42             Chart1.ChartAreas["ChartArea1"].AxisY2.MajorGrid.Enabled = false;   //隱藏 Y2 軸線
43             //Y 軸線顏色
44             Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.FromArgb(150, 150, 150);
45             //X 軸線顏色
46             Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.FromArgb(150, 150, 150);
47             Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "#,###";
48             //Chart1.ChartAreas["ChartArea1"].AxisY2.Maximum = 160;
49             //Chart1.ChartAreas["ChartArea1"].AxisY2.Interval = 20;
50  
51             //設定 Legends------------------------------------------------------------------------               
52             Chart1.Legends["Legends1"].DockedToChartArea = "ChartArea1"; //顯示在圖表內
53             //Chart1.Legends["Legends1"].Docking = Docking.Bottom; //自訂顯示位置
54             Chart1.Legends["Legends1"].BackColor = Color.FromArgb(235, 235, 235); //背景色
55             //斜線背景
56             Chart1.Legends["Legends1"].BackHatchStyle = ChartHatchStyle.DarkDownwardDiagonal;
57             Chart1.Legends["Legends1"].BorderWidth = 1;
58             Chart1.Legends["Legends1"].BorderColor = Color.FromArgb(200, 200, 200);
59  
60             //設定 Series-----------------------------------------------------------------------
61             Chart1.Series["Series1"].ChartType = SeriesChartType.Column; //直條圖
62             //Chart1.Series["Series1"].ChartType = SeriesChartType.Bar; //橫條圖
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; //Label 範圍大小
68             Chart1.Series["Series1"].LabelForeColor = Color.FromArgb(0, 90, 255); //字體顏色
69             //字體設定
70             Chart1.Series["Series1"].Font = new System.Drawing.Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold);
71             //Label 背景色
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; // Show data points labels
75  
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; //Label 範圍大小
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);
87  
88             //Data Table
89             string output = "..."; //output data table
90             Label label = new Label();
91             label.Text = output;
92             Page.Controls.Add(label);
93         }
94     }
95 }

 

跑出來的圖大致就長這樣,个中 塞里es1 是藍色的,Series2 是橘色

澳门永利娱乐总站 1

 

直條圖跟橫條圖其實只要改變 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 是一樣的道理

 

            

參考資料:

Samples Environment for Microsoft Chart Controls
 http://archive.msdn.microsoft.com/mschart


http://www.dotblogs.com.tw/joo3360/archive/2010/10/05/18127.aspx

MS Chart 針對多個Series以及自訂X軸Group樣式

這次接到客戶必要說,要顯示各機台狀況以及顯示機台與站點間X軸Group的顯示,而每個機台又必須區分顏色出來。

沒寫過,所以研究了弹指间MS Chart怎么着運用。

然後發現ChartType必須使用SeriesChartType.StackedColumn,才能達到一個X軸刻度來重疊Series來顯示(這個作者找好久,果然不熟MS
Chart,物件太多了)

下圖是這次運用到作法萨姆ple Chart。

澳门永利娱乐总站 2 

Step.1 必須先動態建立好Series至Chart物件中

01 //先動態產生Series
02 for (int i = 0; i < 8; i++)
03 {
04     Series series = new Series("DATA" + i.ToString());
05     series.Color = Color.FromArgb(rnd.Next() % (255 - 0 + 1) + 0,
06         rnd.Next() % (255 - 0 + 1) + 0,
07         rnd.Next() % (255 - 0 + 1) + 0); //亂數定義Series顏色
08  
09     series.ChartType = SeriesChartType.StackedColumn;
10     this.Chart1.Series.Add(series);
11 }

Step.2 再將各個Series参预資訊。

*注意:因為假設一筆資料都要區分分裂顏色必須各自填入各自的Series。

01 //填入Series資訊       
02         for (int i = 0; i < Chart1.Series.Count; i++)
03         {
04             for (int j = 0; j < Chart1.Series.Count; j++)
05             {
06                 if (i == j)
07                 {
08                     Chart1.Series[i].Points.AddXY(i.ToString(), i * 5);
09                 }
10                 else
11                 {
12                     Chart1.Series[i].Points.AddY(0); //如果不要產生該Point資訊時,就先資訊設為0
13                 }
14             }
15         }

Step.3 再動態加作客製化的X軸。

 

 

1 //X軸作Group設定
2         Chart1.ChartAreas[0].AxisX.CustomLabels.Add(0.5, 4.5, "A", 1, LabelMarkStyle.LineSideMark);
3         Chart1.ChartAreas[0].AxisX.CustomLabels.Add(4.5, 8.5, "B", 1, LabelMarkStyle.LineSideMark);

 

 

Step.4 参预Legend,顯示右方Series資訊列

 

1 //設定Legend
2         Legend legend = new Legend("TEST Legend");
3         legend.Docking = Docking.Right;
4         legend.Alignment = System.Drawing.StringAlignment.Center;
5         Chart1.Legends.Add(legend);

 

 

如上就完结上列Sample Chart

相关文章