asp.net|餅狀圖|建立|動態|柱狀圖 做web項目開發,難免會遇到動態建立柱狀圖或餅型圖。現在用.net技術實現動態建立它們。由於本身比較喜歡足球,更是魯能的球迷,所以就用了魯能的主場球迷人數作為例子,請不要太驚奇! 一個人在西安的山東人希望魯能可以提前奪冠!!呵呵,扯遠了……
動態建立柱狀圖與餅狀圖的前台HTML代碼如下:
<body> <form id="Form1" method="post" runat="server"> <table width="517" border="0" height="255"> <tr> <td align="middle"><img src="ChartText.aspx"></td> </tr> <tr> <td height="20" align="middle">ASP.NET 中動態建立圖形範例</td> </tr> </table> </form> </body>程式運行後的效果如圖:後台代碼如下:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Drawing.Imaging; //添加 Imaging 的引用。 namespace WebApplication1{ ///<summary> /// ChartText 的摘要說明。 /// 作者:Shadow ///</summary> public class ChartText : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { Bitmap objBitMap = new Bitmap(400, 240); Graphics objGraphics; objGraphics = Graphics.FromImage(objBitMap); objGraphics.Clear(Color.White); int[] arrValues = {40000,32000,24000,30000,36000,28000}; string[] arrValueNames = newstring[]{"第一次","第二次","第三次","第四次","第五次","第六次"}; objGraphics.DrawString(" 山東魯能主場球迷人數統計(人)", new Font("宋體", 16), Brushes.Blue, new PointF(5, 5)); PointF symbolLeg = new PointF(335, 20); PointF descLeg = new PointF(360, 16); //畫出說明部分的圖形 for (int i = 0; i < arrValueNames.Length; i++) { objGraphics.FillRectangle(new SolidBrush(GetColor(i)), symbolLeg.X, symbolLeg.Y, 20, 10); objGraphics.DrawRectangle(Pens.Black, symbolLeg.X, symbolLeg.Y, 20, 10); objGraphics.DrawString(arrValueNames[i].ToString(), new Font("宋體", 10), Brushes.Black, descLeg); symbolLeg.Y += 15; descLeg.Y += 15; } float TotalValues = 0; for (int i = 0; i <= arrValues.Length - 1; i++) { TotalValues += arrValues[i]; } //繪出矩形圖。 float Rectangleheight = 0; PointF recLeg = new PointF(12,200-arrValues[0]/TotalValues*300); for (int i = 0; i < arrValues.Length; i++) { Rectangleheight = arrValues[i] / TotalValues *300; objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 35) + 15, 200 - Rectangleheight, 20, Rectangleheight + 50); objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 - Rectangleheight, 20, Rectangleheight + 50); recLeg.Y=200-Rectangleheight-14; objGraphics.DrawString(arrValues[i].ToString(), new Font("宋體", 10), Brushes.Blue, recLeg); recLeg.X+=35; } //繪出圓形圖。 float sglCurrentAngle = 0; float sglTotalAngle = 0; for (int i = 0; i < arrValues.Length; i++) { sglCurrentAngle = arrValues[i] / TotalValues * 360; objGraphics.FillPie(new SolidBrush(GetColor(i)), 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle); objGraphics.DrawPie(Pens.Black, 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle); sglTotalAngle += sglCurrentAngle; } objBitMap.Save(Response.OutputStream, ImageFormat.Gif); } #region Web Form設計器產生的程式碼 override protected void OnInit(EventArgs e) { // // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。 // InitializeComponent(); base.OnInit(e); } ///<summary> /// 設計器支援所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 ///</summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion //定義顏色。 private Color GetColor(int itemIndex) { Color objColor; if (itemIndex == 0) { objColor = Color.Maroon; } elseif (itemIndex == 1) { objColor = Color.Red; } elseif (itemIndex == 2) { objColor = Color.Gray; } elseif (itemIndex == 3) { objColor = Color.Blue; } elseif (itemIndex == 4) { objColor = Color.Orange; } elseif (itemIndex == 5) { objColor = Color.Cyan; } elseif (itemIndex == 6) { objColor = Color.Bisque; } elseif (itemIndex == 7) { objColor = Color.Maroon; } elseif (itemIndex == 8) { objColor = Color.Maroon; } else { objColor = Color.Blue; } return objColor; } }}