推薦:ASP.NET畫圖全攻略(上)本文代碼是基于Beta2開發(fā)越來越多的Web應(yīng)用需要使用圖表來進(jìn)行數(shù)據(jù)顯示和分析。例如:投票結(jié)果顯示,公司生產(chǎn)情況統(tǒng)計圖顯示分析等等。利用圖表來顯示數(shù)據(jù),具有直觀,清晰等優(yōu)點(diǎn)。傳統(tǒ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,其中代碼如下所示:
| 以下為引用的內(nèi)容: 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) { file://從數(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"); file://設(shè)定產(chǎn)生圖的類型(pie or bar) string type = ""; if(null==Request["type"]) { type = "PIE"; } else { type = Request["type"].ToString().ToUpper(); } file://設(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"]); } file://設(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)容:
| 以下為引用的內(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)題文字。 |
由此,我們完成了利用asp.net技術(shù)畫圖的過程。
綜合起來,可以總結(jié)出以下幾點(diǎn):1.利用ASP.NET技術(shù),可以在不使用第三方組件的情況下,畫出理想的圖形。2.畫圖核心是構(gòu)造一個BitMap(位圖)對象,它為要創(chuàng)建的圖形提供了內(nèi)存空間。然后,利用有關(guān)namespace提供的類和方法畫出圖形。最后就可以調(diào)用Bitmap對象的“Save”方法,將其發(fā)送到任何.NET的輸出流中,這里是直接將圖形的內(nèi)容發(fā)送到瀏覽器,而沒有將其保存到磁盤中。
分享:ASP.NET的實時天氣及24小時天氣預(yù)報修改其中的url獲得其他城市的天氣情況如廣州為:http://weather.yahoo.com/forecast/CHXX0037_c.html注意僅適用于獲得yahoo上的天氣預(yù)報
新聞熱點(diǎn)
疑難解答
圖片精選