收 藏 投 稿 繁 体 RSS 
站长吧-中国站长学习与交流的网站
首 页 运 营 学 院 建 站 论 坛
Web master8.net  
 网站运营  
  欢迎在本站发布信息,在线投递稿件请点这里。编辑QQ:4908220,欢迎联系交流。
业界动态 创业故事 推广研究 策划盈利 电子商务 企业平台
  站长工具
SEO查询 Whois查询 Pr查询 域名查询 IP查询 网页编辑器
 建站服务  
  如有建站意向,请尽快联系我们,以便安排时间... 建站服务 QQ4908220 QQ:4908220
作品展示 服务范围 服务流程 服务报价 联系方式 付款方式
文章正文  » 您的当前位置: 首页 >> 学院 >> 程序开发 >> ASP.Net
ASP.NET画图全攻略(下)
  来源:互联网 | 时间:2005-10-04 | 浏览:   相关评论 | 报告错误 | 发布文章
【字号: | | 】 【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色)
     我们在前面已经完成了饼图和条形图的自定义类,下面我们将要应用这些类了。
  使用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的输出流中,这里是直接将图形的内容发送到浏览器,而没有将其保存到磁盘中。
  
  

master8
  • 上一篇:怎样自定义一个服务器端的控件
  • 下一篇:万维网创始人博客处女秀

  • 我要投稿  打印本文  推荐本文  加入收藏  返回顶部  关闭窗口
    搜模板(www.somoban.com) 原创网站模板交易平台
    阿里妈妈再掀疯狂采购风,网站广告位严重告急,急召天下站长
    基于PHP+MySQL的整站、模块、插件开发等或者按需求实现相应功能;
基于各PHP主流建站系统CMS,BBS,BLOG等的模板定制,完全手写代码;
整站数据迁移或备份恢复;网页代码优化、重构;整站常规SEO优化;网站技术支持;
点击了解详情...
    站长论坛
    • 验证码: