我们在前面已经完成了饼图和条形图的自定义类,下面我们将要应用这些类了。
使用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的输出流中,这里是直接将图形的内容发送到浏览器,而没有将其保存到磁盘中。
ASP.NET画图全攻略(下)-.NET教程,Asp.Net开发
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.NET画图全攻略(下)-.NET教程,Asp.Net开发
相关推荐
-      VS2010的aspx文件中的html代码的格式化方法
-      .net 反序题目的详细解答第1/2页
-      asp.net创建html文本文件实例
-      比较完整的 asp.net 学习流程
-      官网 Ext direct包中.NET版的问题
-      C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)第1/2页
-      c# 连接字符串数据库服务器端口号 .net状态服务器端口号
-      asp.net教程:简单的C#图片上传代码或C#文件上传代码