ASP.NET畫圖全攻略(下)
2024-07-10 12:55:40
供稿:網(wǎng)友
我們在前面已經(jīng)完成了餅圖和條形圖的自定義類,下面我們將要應(yīng)用這些類了。
使用vs.net新建一個名為insight_cs的web應(yīng)用程序,并且添加到剛才的insight工程中。刪除默認(rèn)的webform1.aspx文件,新建一個名為saleschart.aspx文件。打開此文件,在代碼模式下,將第一行替換為:
<%@ page contenttype="image/gif" language="c#" autoeventwireup="false" codebehind="saleschart.aspx.cs" inherits="insight_cs.saleschart" %>
打開文件saleschart.aspx.cs,其中代碼如下所示:
using system;
using system.data;
using system.web;
using system.io;
using system.data.sqlclient;
using insight_cs.webcharts;//這是自定義的名字空間
namespace insight_cs
{
public class saleschart : system.web.ui.page
{
public saleschart()
{
page.init += new system.eventhandler(page_init);
}
private void page_load(object sender, system.eventargs e)
{
//從數(shù)據(jù)庫中取得數(shù)據(jù),用于畫圖
string sql = "select " +"year(sa.ord_date) as [year], " +"sum(sa.qty) as [qty] " +"from " +"sales sa " +"inner join stores st on(sa.stor_id = st.stor_id) " +"group by " +"year(sa.ord_date) " + "order by " + "[year]";
string connectstring = "password=ben; user id=sa; database=pubs;data source=localhost";
sqldataadapter da = new sqldataadapter(sql,connectstring);
dataset ds = new dataset();
int rows = da.fill(ds,"chartdata");
//設(shè)定產(chǎn)生圖的類型(pie or bar)
string type = "";
if(null==request["type"])
{
type = "pie";
}
else
{
type = request["type"].tostring().toupper();
}
//設(shè)置圖大小
int width = 0;
if(null==request["width"])
{
width = 400;
}
else
{
width = convert.toint32(request["width"]);
}
int height = 0;
if(null==request["height"])
{
height = 400;
}
else
{
height = convert.toint32(request["height"]);
}
//設(shè)置圖表標(biāo)題
string title = "";
if(null!=request["title"])
{
title = request["title"].tostring();
}
string subtitle = "";
if(null!=request["subtitle"])
{
subtitle = request["subtitle"].tostring();
}
if(0<rows)
{
switch(type)
{
case "pie":
piechart pc = new piechart();
pc.render(title,subtitle,width,height,ds,response.outputstream);
break;
case "bar":
barchart bc = new barchart();
bc.render(title,subtitle,width,height,ds,response.outputstream);
break;
default:
break;
}
}
}
private void page_init(object sender, eventargs e)
{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
}
#region web form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
以上的代碼并沒有什么難的,這里就不做分析了。
在vs.net中,打開insight_cs solution,右擊”引用“,將出現(xiàn)”添加引用“,將組件文件insight_cs.webcharts.dll加入,使其成為項目中的namespace。
下面我們就可以瀏覽結(jié)果了。
首先建立一個demochart.aspx文件,在其代碼中,加入一下內(nèi)容:
<img alt="sales data - pie"
src="saleschart.aspx?type=pie&width=300&height=30
0&title=sales+by+year&subtitle=books">
<img alt="sales data - bar"
src="saleschart.aspx?type=bar&width=300&height=30
0&title=sales+by+year&subtitle=books">
type表示顯示圖形的類型,是餅圖pie,還是條形圖bar。
width,height表示圖形的大小。
title表示大標(biāo)題文字。
subtitle表示小標(biāo)題文字。
其結(jié)果顯示如圖1(圖片在文章《asp.net畫圖全攻略(上)》)。
由此,我們完成了利用asp.net技術(shù)畫圖的過程。
綜合起來,可以總結(jié)出以下幾點:1.利用asp.net技術(shù),可以在不使用第三方組件的情況下,畫出理想的圖形。2.畫圖核心是構(gòu)造一個bitmap(位圖)對象,它為要創(chuàng)建的圖形提供了內(nèi)存空間。然后,利用有關(guān)namespace提供的類和方法畫出圖形。最后就可以調(diào)用bitmap對象的“save”方法,將其發(fā)送到任何.net的輸出流中,這里是直接將圖形的內(nèi)容發(fā)送到瀏覽器,而沒有將其保存到磁盤中。