我們在前面已經完成了餅圖和橫條圖的自訂類,下面我們將要應用這些類了。
使用vs.net建立一個名為Insight_cs的Web應用程式,並且添加到剛才的Insight工程中。刪除預設的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)
{
//從資料庫中取得資料,用於畫圖
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");
//設定產生圖的類型(pie or bar)
string type = "";
if(null==Request["type"])
{
type = "PIE";
}
else
{
type = Request["type"].ToString().ToUpper();
}
//設定圖大小
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"]);
}
//設定圖表標題
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,右擊”引用“,將出現”添加引用“,將組件檔案Insight_cs.WebCharts.dll加入,使其成為項目中的namespace。
下面我們就可以瀏覽結果了。
首先建立一個demochart.aspx檔案,在其代碼中,加入一下內容:
<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表示大標題文字。
subtitle表示小標題文字。
其結果顯示1(圖片在文章《ASP.NET畫圖全攻略(上)》)。
由此,我們完成了利用asp.net技術畫圖的過程。
綜合起來,可以總結出以下幾點:1.利用ASP.NET技術,可以在不使用第三方組件的情況下,畫出理想的圖形。2.畫圖核心是構造一個BitMap(位元影像)對象,它為要建立的圖形提供了記憶體空間。然後,利用有關namespace提供的類和方法畫出圖形。最後就可以調用Bitmap對象的“Save”方法,將其發送到任何.NET的輸出資料流中,這裡是直接將圖形的內容發送到瀏覽器,而沒有將其儲存到磁碟中。
摘至:http://www.phome.net/document/net/200503/net11106132211044.html