在Asp.net页面中实现数据饼图

Posted on 2005-10-19 09:56  飞鼠  阅读(121)  评论(0)    收藏  举报

  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;
 //下面程序中使用的ImageFormat?所在的命名空?
 using System . Drawing . Imaging ;
 //下面程序中使用到?于?据?方面的?所在的命名空?
 using System . Data . OleDb ;
 
 
 namespace vbtocs
 {
     /**//// <summary>
     /// WebForm1 的摘要?明。
     /// </summary>
     public class WebForm2 : System.Web.UI.Page
     {
         private void Page_Load(object sender, System.EventArgs e)
         {
//             // 在此?放置用?代?以初始化?面
//             string sRouter = "c:\\db2.mdb" ;
//
//             //?得?前Access?据?在服?器端的??路?
//             string strCon = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + sRouter ;

    string strCon = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = d:\\vbtocs\\db2.mdb" ;

            //?建一??据??接
             OleDbConnection myConn = new OleDbConnection ( strCon ) ;
             string strCom = " SELECT YF ,SL FROM MonthSale ORDER BY YF" ;
             myConn.Open ( ) ;
             OleDbCommand myCommand = new OleDbCommand ( strCom , myConn ) ;
            OleDbDataReader myOleDbDataReader = myCommand.ExecuteReader ( ) ;
             //?建OleDbDataReader?例,并以此?例??取?据?中各????据
 
             int [ ] iXiaoSH = new int [ 12 ] ;
             //定?一???,用以存放??据?中?取的?售?据
 
             string [ ] sMoth = new string [ 12 ] ;
             //定?一???,用以存放??据?中?取的?售月份
 
             int iIndex = 0 ;
             while ( myOleDbDataReader.Read ( ) )
             {
                 iXiaoSH [ iIndex ] = myOleDbDataReader.GetInt32 ( 1 ) ;
                 sMoth [ iIndex ] = myOleDbDataReader.GetInt32 ( 0 ) . ToString() + "月" ;
                 iIndex++ ;
             }
             //?取Table01?据表中的各??据,并存放在先前定?的二???中
 
             myConn . Close ( ) ;
             myOleDbDataReader . Close ( ) ;
 
             Bitmap bm = new Bitmap ( 600 , 300 ) ;
             //?建一??度?600,???300的Bitmap?例
 
             Graphics g ;
             g = Graphics.FromImage ( bm ) ;
             g . Clear ( Color . Snow ) ;
             g . DrawString ( " 佳通??科技(江西)公司2005年度?售情?一?表" , new Font ( "宋体" , 16 ) , Brushes . Black , new Point ( 5 , 5 ) ) ;
             //在???面的指定位置,以指定的字体、指定的?色?制指定的字符串。即??表??
 
             //以下代?是是???01中的右上部?域
             //以上是在?01中?下面?制定位
             Point myRec = new Point ( 515 , 30 ) ;
             Point myDec = new Point ( 540 , 30 ) ;
             Point myTxt = new Point ( 565 , 30 ) ;
             g . DrawString ( "?位:万件" , new Font ( "宋体" , 9 ) , Brushes . Black , new Point ( 515 , 12 ) ) ;
             for ( int i = 0 ; i < sMoth.Length ; i++ )
             {
                 g . FillRectangle ( new SolidBrush ( GetColor ( i ) ) , myRec . X , myRec . Y , 20 , 10 ) ;
                 //填充小方?
 
                 g . DrawRectangle ( Pens.Black , myRec . X , myRec . Y , 20 , 10 ) ;
                 //?制小方?
 
                 g . DrawString ( sMoth [ i ] . ToString ( ) , new Font ( "宋体", 9 ) , Brushes . Black , myDec ) ;
                 //?制小方?右?的文字
 
                 g . DrawString ( iXiaoSH[i].ToString (), new Font ( "宋体", 9 ) , Brushes . Black , myTxt ) ;
                 myRec . Y += 15 ;
                 myDec . Y += 15 ;
                 myTxt . Y += 15 ;
             }
 
             //以下代?是根据??据?中得到的?值大小,?制扇型,并以相?色彩填充扇型,//?而构成?01中的Pie?
             int iTatal = 0 ;
             float fCurrentAngle = 0 ;
             float fStartAngle = 0;
             for ( int i = 0 ; i < iXiaoSH . Length ; i++ )
             {
                 iTatal = iTatal + iXiaoSH [ i ] ;
             }
             for ( int i = 0 ; i < iXiaoSH . Length ; i++ )
             {
                //以下代?是?得要?制扇型的?始角度
                if ( i == iXiaoSH . Length - 1 )
                {
                    fCurrentAngle = 360- fStartAngle ;
                }
                else
                {
                    int iTemp = iXiaoSH [ i ] ;
                    fCurrentAngle = ( iTemp * 360 ) / iTatal ;
                }
                //根据???制扇型

                g.DrawPie ( Pens.Black , 100 , 40 , 250 , 250 , fStartAngle , fCurrentAngle ) ;

                //以指定色彩填充?制的扇型

                g.FillPie ( new SolidBrush ( GetColor ( i ) ) , 100 , 40 , 250 , 250 , fStartAngle , fCurrentAngle ) ;
                fStartAngle += fCurrentAngle ;
            }

            //?出?片的?框
            Pen p = new Pen ( Color.Black , 2 ) ;
            g . DrawRectangle ( p , 1 , 1 , 598 , 298 ) ;

            //向客?端?出?据流,并以此?据流形成Jpeg?片
            bm.Save ( Response . OutputStream , ImageFormat . Jpeg ) ;
        }

       // Web 窗体??器生成的代?#region Web 窗体??器生成的代?
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: ??用是 ASP.NET Web 窗体??器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /**//// <summary>
        /// ??器支持所需的方法 - 不要使用代???器修改
        /// 此方法的?容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /**//// <summary>
        /// ?取?色
        /// </summary>
        /// <param name="itemIndex">??的索引</param>
        /// <returns></returns>
        private Color GetColor ( int itemIndex )
        {
             Color MyColor ;
             int i = itemIndex ;
            switch (i)
             {
                  case 0 :
                   MyColor = Color.Green;
                   return MyColor;
                  case 1 :
                   MyColor = Color.Red;
                    return MyColor;
                  case 2:
                   MyColor = Color.Yellow;
                   return MyColor;
                  case 3 :
                   MyColor = Color.Blue;
                   return MyColor;
                  case 4 :
                   MyColor = Color.Orange;
                   return MyColor;
                 case 5 :
                   MyColor = Color.Aqua;
                   return MyColor;
                  case 6:
                   MyColor = Color.SkyBlue;
                   return MyColor;
                  case 7:
                   MyColor = Color.DeepPink;
                    return MyColor;
                 case 8:
                   MyColor = Color.Azure;
                   return MyColor;
                  case 9:
                   MyColor = Color.Brown;
                   return MyColor;
                  case 10:
                   MyColor = Color.Pink;
                   return MyColor;
                  case 11:
                   MyColor = Color.BurlyWood;
                   return MyColor;
                  case 12:
                   MyColor = Color.Chartreuse;
                   return MyColor;
                  default:
                   MyColor = Color.Pink;
                   return MyColor;
            }
        }
    }
}