📄 default2.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection Con;
SqlCommand Com;
SqlDataAdapter Da;
DataSet ds;
string strconn = "Server=(local);Database=db_05;Uid=sa;Pwd=";
protected void Page_Load(object sender, EventArgs e)
{
DrawHistogram();
}
//绘制柱形图
private void DrawHistogram()
{
int height = 400, width = 420;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
Con = new SqlConnection(strconn);
SqlDataAdapter da1 = new SqlDataAdapter("select count(*),商品名,订货数量 from 商品销售表 where 订单号 like '" + Session["Histogram"] + "' Group By 商品名,订货数量", Con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
Pen mypen = new Pen(brush, 1);
//绘制线条
//绘制横向线条
int x = 100;
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
g.DrawLine(mypen, x, 80, x, 340);
x = x + 40;
}
Pen mypen1 = new Pen(Color.Blue, 2);
g.DrawLine(mypen1, 60, 80, 60, 340);
g.DrawString("" + Session["Histogram"] + "号订单商品统计", font1, brush1, new PointF((x - 180) / 2, 30));
//绘制纵向线条
int y = 106;
for (int i = 0; i < 9; i++)
{
g.DrawLine(mypen, 60, y, x + 20, y);
y = y + 26;
}
g.DrawLine(mypen1, 60, y, x + 20, y);
//x轴
x = 68;
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
g.DrawString(ds1.Tables[0].Rows[i][1].ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置
x = x + 40;
}
//y轴
String[] m = {"200", "180", "160", "140", "120", "100", "80", "60",
"40", "20", "0"};
y = 73;
for (int i = 0; i < 11; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Red, 25, y); //设置文字内容及输出位置
y = y + 26;
}
string cmdtxt = "SELECT * from 商品销售表 where 订单号 like '" + Session["Histogram"] + "'";
Con.Open();
Com = new SqlCommand(cmdtxt, Con);
Da = new SqlDataAdapter();
Da.SelectCommand = Com;
ds = new DataSet();
Da.Fill(ds);
int[] Count = new int[ds.Tables[0].Rows.Count];
int j = 0;
for (j = 0; j < ds.Tables[0].Rows.Count; j++)
{
Count[j] = Convert.ToInt32(ds.Tables[0].Rows[j][4].ToString());
}
//显示柱状效果
x = 70;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
SolidBrush mybrush = new SolidBrush(Color.Red);
g.FillRectangle(mybrush, x, 340 - Count[i] * 26 / 20, 20, Count[i] * 26 / 20);
x = x + 40;
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -