在ASP.NET页面中实现数据棒图(转载)
首先看一下效果图(具体内容就看源码吧:)
源码:
bar
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//下面程序中使用的ImageFormat类所在的命名空间
using System.Drawing.Imaging;
//下面程序中使用到关于数据库方面的类所在的命名空间
using System.Data.OleDb;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
string sRouter = "d:\\WebSite\\Bar\\App_Data\\db1.mdb";
//获得当前Access数据库在服务器端的绝对路径
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + sRouter;
//创建一个数据库连接
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT YF ,SL FROM Table01 ORDER BY YF";
myConn.Open();
OleDbCommand myCommand = new OleDbCommand(strCom, myConn);
OleDbDataReader myOleDbDataReader = myCommand.ExecuteReader();
//创建OleDbDataReader实例,并以此实例来获取数据库中各条记录数据
int[] iXiaoSH = new int[12];
//定义一个数组,用以存放从数据库中读取的销售数据
string[] sMoth = new string[12];
//定义一个数组,用以存放从数据库中读取的销售月份
int iIndex = 0;
while (myOleDbDataReader.Read())
{
iXiaoSH[iIndex] = myOleDbDataReader.GetInt32(1);
sMoth[iIndex] = myOleDbDataReader.GetInt32(0).ToString() + "月";
iIndex++;
}
//读取Table01数据表中的各条数据,并存放在先前定义的二个数组中
myConn.Close();
myOleDbDataReader.Close();
//关闭各种资源
Bitmap bm = new Bitmap(600, 250);
//创建一个长度为600,宽带为250的Bitmap实例
Graphics g;
g = Graphics.FromImage(bm);
//由此Bitmap实例创建Graphic实例
g.Clear(Color.Snow);
//用Snow色彩为背景色填充此绘画图面
g.DrawString(" ××公司××产品2004年度销售情况一览表", new Font("宋体", 16), Brushes.Black, new Point(5, 5));
//在绘画图面的指定位置,以指定的字体、指定的颜色绘制指定的字符串。即为图表标题
//以下代码是是实现图01中的右上部
Point myRec = new Point(535, 30);
Point myDec = new Point(560, 26);
//以上是在图01中为下面绘制定位
g.DrawString("单位:万件", new Font("宋体", 9), Brushes.Black, new Point(525, 12));
for (int i = 0; i < sMoth.Length; i++)
{
g.DrawRectangle(Pens.Black, myRec.X, myRec.Y, 20, 10);
//绘制小方块
g.FillRectangle(new SolidBrush(GetColor(i)), myRec.X, myRec.Y, 20, 10);
//填充小方块
g.DrawString(sMoth[i].ToString(), new Font("宋体", 9), Brushes.Black, myDec);
//绘制小方块右边的文字
myRec.Y += 15;
myDec.Y += 15;
}
//以下代码是绘制图01中的Bar图,及其销售数量
int iBarWidth = 40;
int scale = 10;
for (int i = 0; i < iXiaoSH.Length; i++)
{
g.DrawRectangle(Pens.Black, (i * iBarWidth) + 15, 250 - (iXiaoSH[i] * scale), 20, (iXiaoSH[i] * scale) + 5);
//绘制Bar图
g.FillRectangle(new SolidBrush(GetColor(i)), (i * iBarWidth) + 15, 250 - (iXiaoSH[i] * scale), 20, (iXiaoSH[i] * scale) + 5);
//以指定的色彩填充Bar图
g.DrawString(iXiaoSH[i].ToString(), new Font("宋体", 9), Brushes.Black, (i * iBarWidth) + 20, 235 - (iXiaoSH[i] * scale));
//显示Bar图代表的数据
}
//以下代码是绘制图01中的边框,并形成Jpeg文件,供浏览器显示出来
Pen p = new Pen(Color.Black, 2);
g.DrawRectangle(p, 1, 1, 598, 248);
bm.Save(Response.OutputStream, ImageFormat.Jpeg);
}
private Color GetColor(int itemIndex)
{
Color MyColor;
int i = itemIndex;
switch (i)
{
case 0:
MyColor = Color.Cornsilk;
return MyColor;
case 1:
MyColor = Color.Red;
return MyColor;
case 2:
MyColor = Color.Yellow;
return MyColor;
case 3:
MyColor = Color.Peru;
return MyColor;
case 4:
MyColor = Color.AliceBlue;
return MyColor;
case 5:
MyColor = Color.Bisque;
return MyColor;
case 6:
MyColor = Color.Gray;
return MyColor;
case 7:
MyColor = Color.Maroon;
return MyColor;
case 8:
MyColor = Color.Azure;
return MyColor;
case 9:
MyColor = Color.Orange;
return MyColor;
case 10:
MyColor = Color.Coral;
return MyColor;
case 11:
MyColor = Color.BurlyWood;
return MyColor;
case 12:
MyColor = Color.Chartreuse;
return MyColor;
default:
MyColor = Color.Green;
return MyColor;
}
}
}
本博客内容,如需转载请务必保留超链接。Contact Me:Mail此处省略好几个字...