折线统计图(转载)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Data;

public partial class LineTJImage : System.Web.UI.Page
{
public string connStr = ConfigurationManager.ConnectionStrings["VisitCountConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
DrawLinearGradient();
}
//访问人数统计
public int Total()
{
int result = -1;
string sql = "select count(1) from VisiteCount";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
result = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
conn.Close();
return result;
}
//柱形图
public void DrawLinearGradient()
{
int width = 600, height = 400;
Bitmap image = new Bitmap(width,height);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//画矩形
g.FillRectangle(Brushes.WhiteSmoke, new Rectangle(0, 0, width, height));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0,width,height),Color.Blue,Color.BlueViolet,1.2f,true);
Font font = new Font("Arial",9,FontStyle.Regular);
Font font1 = new Font("宋体",20,FontStyle.Bold);
//写标题
g.DrawString("2013网站浏览次数统计",font1,brush,new PointF(120,30));
Pen pen = new Pen(Color.Blue);
Pen pen1 = new Pen(Color.Blue,2);
//画边框
g.DrawRectangle(pen, 0, 0, width-1, height - 1);
//设定横向起始
int x = 100;
for (int i = 0; i < 11; i++)
{
g.DrawLine(pen, x, 80, x, 340);
x += 40;
}
//画y轴线
int y = 106;
for (int i = 0; i <9; i++)
{
g.DrawLine(pen, 60, y,540,y);
y += 26;
}
g.DrawLine(pen1, 60, y, 540, y);
//画X轴线条
g.DrawLine(pen1,x-480,80,x-480,340);
//X轴
string[] n = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
x = 62;
for (int i = 0; i < 12; i++)
{
g.DrawString(n[i],font,Brushes.Black,new PointF(x,348));
x += 40;
}
g.DrawString("人/月", new Font("宋体",10,FontStyle.Italic), Brushes.Black, new PointF(35, 348));
//y轴
string[] m = { "100", "90", "80", "70", "60", "50", "40", "30", "20", "10", "0" };
y = 85;
for (int i = 0; i < 11; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Black, 25, y);//设置文字内容以及输出位置
y = y + 25;
}
//将检索出的数据按一定比例绘制到图像中
int[] count = new int[12];
string sql = "";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlDataAdapter da;
DataSet ds = new DataSet();
for (int i = 0; i < 12; i++)
{
sql = @"select count(1) as count,Month(loginTime) as month from VisiteCount where YEAR(loginTime)=2013 and MONTH(loginTime)=" + (i + 1) + " group by MONTH(loginTime)";
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, i.ToString());
if (ds.Tables[i].Rows.Count == 0)
{
count[i] = 0;
}
else
{
//count[i] = Convert.ToInt32(ds.Tables[i].Rows[0][0].ToString())*100/Total();
count[i] = Convert.ToInt32(ds.Tables[i].Rows[0][0].ToString());
}
}
x = 70;
Point[] points = new Point[12];
for (int i = 0; i < 12; i++)
{
//g.DrawLine(pen
//g.DrawLine(pen, x, 340 - count[i] * 26 / 10, 20, count[i] * 26 / 10);
points[i] = new Point(x, 340 - count[i] * 26 / 10);
x += 40;
}
g.DrawLines(pen,points);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}

}

折线统计图(转载)

上一篇:__attribute__((packed))详解


下一篇:New Year Wishs