asp,net動態生成折線圖

public class PrintTable : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(Session["chuzulv"]!=null)
            {
                //數據源
                Table table=(Table)Session["Chuzulv"];

                //可修改屬性
                int Height=300;                                //圖像高度
                int Width=500;                                //圖像寬度
                string TableName="物資出租率";                //表頭
                Font TableHeaderFont=new Font("宋體",12);    //表頭字體
                float High=100;                                //默認最大標尺
                float Low=0;                                //默認最小標尺
                Color BGColor=Color.Black;                    //背景色
                Color HLineColor=Color.DarkCyan;            //橫向表格線顏色
                Color LineColor=Color.WhiteSmoke;            //曲線顏色

                //臨時屬性不可修改
                Bitmap bm = new Bitmap(Width,Height);        //圖像對象
                Graphics g = Graphics.FromImage(bm);        //繪圖對象
                StringFormat drawFormat = new StringFormat();//縱向文字輸出格式
                drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;

                SolidBrush br=new SolidBrush(Color.Red);    //Brush,紅色
                Pen p =new Pen(Color.Red,1);                //Pen,紅色,寬度1
                g.Clear(BGColor);                            //繪製背景色
                g.DrawString(TableName,TableHeaderFont,br,new Point(0,0)); //繪製表頭
                int HStep = (Width - 30) / (table.Rows.Count - 1);                //橫向步長(每日期)
                int VStep;                                    //縱向步長(每百分點)
                int index=0;                                //臨時標示記錄索引
                foreach(TableRow r in table.Rows)
                {
                    index++;
                    if (index == 1)                            //過濾數據源第一行Header
                        continue;
                    if (r.Cells[5].Text != "" && float.Parse(r.Cells[5].Text) > High)    //重新獲取縱座標上下限
                        High = float.Parse(r.Cells[5].Text);
                    if (r.Cells[5].Text != "" && float.Parse(r.Cells[5].Text) < Low)
                        Low = float.Parse(r.Cells[5].Text);
                }
                High += (High % 10 == 0) ? 0 : 10 - High % 10;//上下限對齊
                Low -= (Low % 10 == 0) ? 0 : 10 + Low % 10;
                VStep = (Height - 50) / (int)(High - Low);    //獲取縱向步長

                Point lineStartPoint;                        //曲線起始點
                Point lineEndPoint;                            //曲線終止點
                int BaseVHeight=0;                            //0點縱座標
                int Vertical=30;                            //橫向參考線最高點
                for(float HLine = High ; HLine >= Low ;HLine -= 10)        //繪製橫向輔助線
                {
                    g.DrawLine(new Pen(Color.DarkCyan,1),new Point(20,Vertical),new Point(Width,Vertical));
                    g.DrawString(HLine.ToString()+"%",new Font("宋體",8),br,new Point(5,Vertical - VStep ));
                    if(HLine==0)                           
                        BaseVHeight=Vertical;
                    Vertical += VStep * 10;
                }
                Point startPoint=new Point(0,Height-50);
                startPoint.X += 10 ;
               
                lineStartPoint=new Point(startPoint.X  + HStep / 2,BaseVHeight);

                index = 0;
                float PerCent=0;
                foreach(TableRow r in table.Rows)
                {
                    index++;
                    g.DrawString(r.Cells[0].Text,new Font("宋體",8),br,startPoint,drawFormat);
                    if (index == 1)
                        startPoint.X += 20;
                    else
                        startPoint.X += HStep;
                    if (r.Cells[5].Text != "")
                        if (index == 1)
                            continue;
                        else
                            PerCent = float.Parse(r.Cells[5].Text);
                    else
                        PerCent = 0;
                    if (index == 2)
                        lineStartPoint=new Point(startPoint.X - HStep ,BaseVHeight - (int)PerCent * VStep);
                       
                    lineEndPoint = new Point(startPoint.X - HStep,BaseVHeight - (int)PerCent * VStep);
                    g.DrawLine(new Pen(Color.WhiteSmoke,1),lineStartPoint,lineEndPoint);
                   
                    lineStartPoint = lineEndPoint;
                }
                //輸出
                bm.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
                //清除對象
                ((Table)Session["chuzulv"]).Dispose();
                Session["chuzulv"]=null;
            }
        }

補充一下Session[]==null;無法清除Session對象
還要用Session.Remove(*)來清除

因爲報表不需要交互,所以一直用Table,

前一個頁統計出Table之後存入Session["chuzulv"],然後置一Image控件,ImageUrl屬性指向此頁面

效果如圖,上傳時壓縮了品質,動態生成的Gif效果很不錯

  

前一頁面Image控件的ImageUrl="PrintTable.aspx"

源碼就是這樣了,主要流程就是-->獲取數據-->根據數據生成座標軸-->畫線

因爲這個東西在項目裏面一帶而過,所以沒有把它的功能擴展封裝。
起個頭,有興趣自己擴展吧。



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1184853


發佈了16 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章