📄 default.aspx.cs
字号:
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;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
SqlConnection Con;
SqlDataAdapter Da;
DataSet ds;
string strconn = "Server=(local);Database=db_05;Uid=sa;Pwd=";
protected void Page_Load(object sender, EventArgs e)
{
DrawHistogram();
if (!IsPostBack)
{
bindYear();
Session["line"] = DropDownList1.SelectedValue;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["line"] = DropDownList1.SelectedValue;
}
//绑定年份
private void bindYear()
{
Con = new SqlConnection(strconn);
Da = new SqlDataAdapter("select distinct ShowYear from 访问量表", strconn);
ds = new DataSet();
Da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "ShowYear";
DropDownList1.DataBind();
}
//绘制柱形图
private void DrawHistogram()
{
if (!File.Exists(Server.MapPath("柱形图.gif")))
{
int height = 397, width = 400;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
Con = new SqlConnection(strconn);
Da = new SqlDataAdapter("select ShowYear,sum(Year_M1+Year_M2+Year_M3+Year_M4+Year_M5+Year_M6+Year_M7+Year_M8+Year_M9+Year_M10+Year_M11+Year_M12) from 访问量表 Group By ShowYear", Con);
ds = new DataSet();
Da.Fill(ds);
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 < ds.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("网站各年访问量", 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 = 60;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
g.DrawString(ds.Tables[0].Rows[i][0].ToString() + "\n(" + ds.Tables[0].Rows[i][1].ToString() + ")", font, Brushes.Red, x, 348); //设置文字内容及输出位置
x = x + 41;
}
String[] m = {"27000人", "24000人", "21000人", "18000人", "15000人", "12000人", "9000人", "6000人",
"3000人"};
y = 100;
for (int i = 0; i < 9; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Red, 6, y); //设置文字内容及输出位置
y = y + 26;
}
int[] Count = new int[ds.Tables[0].Rows.Count];
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
Count[j] = Convert.ToInt32(ds.Tables[0].Rows[j][1].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 / 3000, 20, Count[i] * 26 / 3000);
x = x + 40;
}
image.Save(Server.MapPath("柱形图.gif"));
g.Dispose();
image.Dispose();
}
Image1.ImageUrl = "~/柱形图.gif";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -