asp.net 繪製曲線圖

來源:互聯網
上載者:User
asp.net

asp.net 繪製曲線圖首先定義DrawClass類

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;

/**//// <summary>
/// DrawClass 的摘要說明
/// </summary>
public class DrawClass
...{
    public DrawClass()
    ...{
        //
        // TODO: 在此處添加建構函式邏輯
        //
    }

    public MemoryStream draw(DataSet ds, int Tnum)
    ...{
        //取得記錄數量
        int count = ds.Tables[0].Rows.Count;
        //記算圖表寬度
        int wd = 80 + 20 * (count - 1);
        //設定最小寬度為800
        if (wd < 800) wd = 800;
        //產生Bitmap對像
        Bitmap img = new Bitmap(wd, 400);
        //產生繪圖對像
        Graphics g = Graphics.FromImage(img);
        //定義黑色畫筆
        Pen Bp = new Pen(Color.Black);
        //定義紅色畫筆
        Pen Rp = new Pen(Color.Red);
        //定義銀灰色畫筆
        Pen Sp = new Pen(Color.Silver);
        //定義大標題字型
        Font Bfont = new Font("Arial", 12, FontStyle.Bold);
        //定義一般字型
        Font font = new Font("Arial", 6);
        //定義大點的字型
        Font Tfont = new Font("Arial", 9);
        //繪製底色
        g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
        //定義黑色過渡型筆刷
        LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
        //定義藍色過渡型筆刷
        LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
        //繪製大標題
        g.DrawString(ds.Tables[0].Rows[0]["sendid"].ToString() + "號訂單發送情況曲線圖", Bfont, brush, 40, 5);
        //取得當前發送量
        int nums = 0;
        for (int i = 0; i < count; i++)
        ...{
            nums += Convert.ToInt32(ds.Tables[0].Rows[i]["sendmum"]);
        }
        //繪製資訊簡報
        string info = "訂單發送時間:" + ds.Tables[0].Rows[0]["sendtime"].ToString() + "  曲線圖產生時間:" + DateTime.Now.ToString() + "  訂單總量:" + Tnum.ToString() + "  當前發送總量:" + nums.ToString();
        g.DrawString(info, Tfont, Bluebrush, 40, 25);
        //繪製圖片邊框
        g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);

        //繪製豎座標線      
        for (int i = 0; i < count; i++)
        ...{
            g.DrawLine(Sp, 40 + 20 * i, 60, 40 + 20 * i, 360);
        }
        //繪製時間軸座標標籤
        for (int i = 0; i < count; i += 2)
        ...{
            string st = Convert.ToDateTime(ds.Tables[0].Rows[i]["sendtime"]).ToString("hh:mm");
            g.DrawString(st, font, brush, 30 + 20 * i, 370);
        }
        //繪製橫座標線
        for (int i = 0; i < 10; i++)
        ...{
            g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 20 * (count - 1), 60 + 30 * i);
            int s = 2500 - 50 * i * 5;
            //繪製發送量軸座標標籤
            g.DrawString(s.ToString(), font, brush, 10, 60 + 30 * i);
        }

        //繪製深度軸
        g.DrawLine(Bp, 40, 55, 40, 360);
        //繪製橫座標軸
        g.DrawLine(Bp, 40, 360, 45 + 20 * (count - 1), 360);

        //定義曲線轉折點
        Point[] p = new Point[count];
        for (int i = 0; i < count; i++)
        ...{
            p[i].X = 40 + 20 * i;
            p[i].Y = 360 - Convert.ToInt32(ds.Tables[0].Rows[i]["sendmum"]) / 5 * 3 / 5;
        }
        //繪製發送曲線
        g.DrawLines(Rp, p);

        for (int i = 0; i < count; i++)
        ...{
            //繪製發送記錄點的發送量
            g.DrawString(ds.Tables[0].Rows[i]["sendmum"].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
            //繪製發送記錄點
            g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
        }
        //繪製豎座標標題
        g.DrawString("發送量", Tfont, brush, 5, 40);
        //繪製橫座標標題
        g.DrawString("發送時間", Tfont, brush, 40, 385);


        //儲存繪製的圖片
        MemoryStream stream = new MemoryStream();
        img.Save(stream, ImageFormat.Jpeg);
        return stream;
       

    }
}
 

然後在分頁檔中調用輸出

DrawClass dc = new DrawClass();
        OracleConnection conn = new OracleConnection(con);
        string sql = "select * from atest";
        OracleDataAdapter da = new OracleDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "atest");
        MemoryStream ss=dc.draw(ds, 6);
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(ss.ToArray());
 圖片輸出到page頁面中也有另外一種方式

        FileStream fs = new FileStream(@"c:Curve.jpg", FileMode.Open, FileAccess.Read);
        byte[] mydata = new byte[fs.Length];
        int Length = Convert.ToInt32(fs.Length);
        fs.Read(mydata, 0, Length);
        fs.Close();
        this.Response.OutputStream.Write(mydata, 0, Length);
        this.Response.End();
 

這樣就行了。

 



相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.