数据库同 柱状统计图
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Drawing;usingSystem.Data;usingSystem.Collections;usingSystem.IO;publicpartialclassCakeImage : System.Web.UI.Page
{publicstringconnStr = ConfigurationManager.ConnectionStrings["VisitCountConnectionString"].ToString();PRotectedvoidPage_Load(objectsender, EventArgs e)
{
DrawLinearGradient();
}//访问人数统计publicintTotal()
{intresult = -1;stringsql ="select count(1) from VisiteCount";
SqlConnection conn=newSqlConnection(connStr);
conn.Open();
SqlCommand cmd=newSqlCommand(sql, conn);
result=Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
conn.Close();returnresult;
}//柱形图publicvoidDrawLinearGradient()
{int[] count =newint[12];stringsql ="";
SqlConnection conn=newSqlConnection(connStr);
conn.Open();
SqlDataAdapter da;
DataSet ds=newDataSet();for(inti =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=newSqlDataAdapter(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());
}
}//设置字体Font fontlegend =newFont("verdana",9);
Font fonttitle=newFont("verdana",10, FontStyle.Bold);//设置背景宽度intwidth =230;intbufferspace =15;intlengendheight = fontlegend.Height *13+ bufferspace;//饼图下方分类表表高度inttitleheight = fonttitle.Height+bufferspace;//标题栏高度intheight = width+lengendheight+titleheight+bufferspace;//白色背景的高度intpieheight = width;//饼图的高度Rectangle pierect =newRectangle(0, titleheight, width, pieheight);//加随机色ArrayList colors =newArrayList();
Random rnd=newRandom();for(inti =0; i <12; i++)
{
colors.Add(newSolidBrush(Color.FromArgb(rnd.Next(255),rnd.Next(255),rnd.Next(255))));
}//创建一个bitmapBitmap objimage =newBitmap(width,height);
Graphics objg=Graphics.FromImage(objimage);
objg.FillRectangle(Brushes.White,0,0, width, height);//画一个亮色背景objg.FillRectangle(newSolidBrush(Color.Beige),pierect);//显示标题objg.DrawString("2013网站浏览每月比例调查", fonttitle, Brushes.Black,newPointF(32,12));//画饼图floatcurrentnum =0.0f;for(inti =0; i <12; i++)
{
objg.FillPie((SolidBrush)colors[i], pierect, currentnum, Convert.ToSingle(count[i])/ Total() *360);
currentnum+=Convert.ToSingle(count[i]) / Total() *360;
}
objg.DrawRectangle(newPen(Color.Blue,2),0, height -lengendheight, width, lengendheight);string[] n = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};for(inti =0; i <12; i++)
{
objg.FillRectangle((SolidBrush)colors[i],5, height - lengendheight+fontlegend.Height*i+5,10,10);stringdble = (Convert.ToDouble(count[i]) / Total() *100).ToString().Substring(0,4)+"%";
objg.DrawString(n[i]+"----"+ dble, fontlegend, Brushes.Black,20, height - lengendheight+fontlegend.Height*i+1);
}
objg.DrawString("2013网络浏览总数:"+ Total() +"次", fontlegend, Brushes.Black,5, height -fontlegend.Height);
Response.ContentType="image/Jpeg";
objimage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
objg.Dispose();
objimage.Dispose();
}
}
View Code