ASP.NET動態建立柱狀圖與餅狀圖

來源:互聯網
上載者:User
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;          }      }} 


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。