ASP.NET畫圖全攻略(下)_.Net教程
推薦:ASP.NET畫圖全攻略(上)本文代碼是基于Beta2開發 越來越多的Web應用需要使用圖表來進行數據顯示和分析。例如:投票結果顯示,公司生產情況統計圖顯示分析等等。利用圖表來顯示數據,具有直觀,清晰等優點。 傳統的
我們在前面已經完成了餅圖和條形圖的自定義類,下面我們將要應用這些類了。
使用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) { file://從數據庫中取得數據,用于畫圖 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://設定產生圖的類型(pie or bar) string type = ""; if(null==Request["type"]) { type = "PIE"; } else { type = Request["type"].ToString().ToUpper(); } file://設置圖大小 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://設置圖表標題 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表示小標題文字。 |
由此,我們完成了利用asp.net技術畫圖的過程。
綜合起來,可以總結出以下幾點:1.利用ASP.NET技術,可以在不使用第三方組件的情況下,畫出理想的圖形。2.畫圖核心是構造一個BitMap(位圖)對象,它為要創建的圖形提供了內存空間。然后,利用有關namespace提供的類和方法畫出圖形。最后就可以調用Bitmap對象的“Save”方法,將其發送到任何.NET的輸出流中,這里是直接將圖形的內容發送到瀏覽器,而沒有將其保存到磁盤中。
分享:ASP.NET的實時天氣及24小時天氣預報修改其中的url獲得其他城市的天氣情況 如廣州為: http://weather.yahoo.com/forecast/CHXX0037_c.html 注意僅適用于獲得yahoo上的天氣預報
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-ASP.NET畫圖全攻略(下)。