绝想首页

ASP.NET画图全攻略(下)

爱嘉人 [分享] 2013-04-08 01:07:34 星期一 晴天 查看:243 回复:0 发消息给作者



 我们在前面已经完成了饼图和条形图的自定义类,下面我们将要应用这些类了。
  使用vs.net新建一个名为Insight_cs的Web应用程序,并且添加到刚才的Insight工程中。删除默认的webform1.aspx文件,新建一个名为SalesChart.aspx文件。打开此文件,在代码模式下,将第一行替换为:
  
  打开文件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
  Sales Data - Bar
  type表示显示图形的类型,是饼图pie,还是条形图bar。
  width,height表示图形的大小。
  title表示大标题文字。
  subtitle表示小标题文字。
  其结果显示如图1(图片在文章《ASP.NET画图全攻略(上)》)。
  
  由此,我们完成了利用asp.net技术画图的过程。
  综合起来,可以总结出以下几点:1.利用ASP.NET技术,可以在不使用第三方组件的情况下,画出理想的图形。2.画图核心是构造一个BitMap(位图)对象,它为要创建的图形提供了内存空间。然后,利用有关namespace提供的类和方法画出图形。最后就可以调用Bitmap对象的“Save”方法,将其发送到任何.NET的输出流中,这里是直接将图形的内容发送到浏览器,而没有将其保存到磁盘中。
顶一下(32 写日记 1243894 234556
分享排行

 

 

留住已经逝去的峥嵘岁月 记住曾经绽现的万种风情 在记忆即将淡漠的时候 来把这些重新回味

Copyright (C) 2008-2014 www.juexiang.com, All Rights Reserved.

京ICP备2023001011号-3   京公网安备11010802011908号

客服QQ 1017160561 违法和不良信息举报电话 13148464312 邮箱 1017160561@qq.com