void StatPhotos()
{
//生成一个位图
Bitmap objBitMap = new Bitmap(200, 200);
//创建一个绘图类
Graphics objGraphics;
//将要绘制的位图指定
objGraphics = Graphics.FromImage(objBitMap);
//背景色 白色
objGraphics.Clear(Color.White);
//定义一个数字
//SqlParameter[] paras = { new SqlParameter("@Buy", ParameterDirection.Output), new SqlParameter("@Sale", ParameterDirection.Output), new SqlParameter("@YesSale", ParameterDirection.Output) };
//这个地方超级郁闷,我在数据库中查询了三个字段的总数,然后用输出参数返回!老是都返回一个莫名其妙的2,把我郁闷死了,最后换了个写发,直接把三个值用查询返回才解决!搞不懂为什么输出参数为什么会出错!
DataSet ds = Sparter.DAL.SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["ConnectionString"].ToString(), CommandType.StoredProcedure, "S_StatPhoto", null);
int buy = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
int sale = Convert.ToInt32(ds.Tables[0].Rows[0][1]);
int yesSale = Convert.ToInt32(ds.Tables[0].Rows[0][2]);
//这里大家一定要用float,用int如果除出来的是0.几几的小数就直接转成0了
float total = buy + sale + yesSale;
float buyBF = (buy / total) * 100;
float saleBF = (sale / total) * 100;
float yesSaleBF = (yesSale / total) * 100;
//这里是算出来要统计的数据
float[] arrValues = { buyBF, saleBF, yesSaleBF };
//所要统计的类类别
string[] arrValueNames = new string[] { "买家", "卖家", "可卖" };
//在指定矩形里画出统计两个字
objGraphics.DrawString("统计", new Font("宋体", 12), Brushes.Black, new Point(2, 2));
PointF symbolLeg = new PointF(125, 50);
PointF descLeg = new PointF(150, 46);
//绘制条形说明
for (int i = 0; i < arrValueNames.Length; i++)
{
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), symbolLeg.X, symbolLeg.Y, 20, 10);
objGraphics.DrawRectangle(Pens.Black, symbolLeg.X, symbolLeg.Y, 20, 10);
objGraphics.DrawString(arrValueNames[i].ToString(), new Font("宋体", 10), Brushes.Black, descLeg);
symbolLeg.Y += 15;
descLeg.Y += 15;
}
//绘制条形图
for (int i = 0; i < arrValueNames.Length; i++)
{
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 35) + 15, 200 - arrValues[i], 10, arrValues[i] + 5);
objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 - arrValues[i], 10, arrValues[i] + 5);
}
//这里是画饼图 因为我项目里没用到所以也没测试,不过应该没问题,要怪就怪我太懒吧!呵呵!
#region
//float sglCurrentAngle = 0;
//float sglTotalAngle = 0;
//float sglTotalValues = 0;
//for (int i = 0; i <= arrValues.Length - 1; i++)
//{
// sglTotalValues += arrValues[i];
//}
//for (int i = 0; i < arrValues.Length; i++)
//{
// sglCurrentAngle = arrValues[i] / sglTotalValues * 360;
// objGraphics.FillPie(new SolidBrush(GetColor(i)), 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
// objGraphics.DrawPie(Pens.Black, 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
// sglTotalAngle += sglCurrentAngle;
//}
#endregion
//保存
objBitMap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
}
//颜色
private Color GetColor(int itemIndex)
{
Color objColor;
if (itemIndex == 0)
{
objColor = Color.Blue;
}
else if (itemIndex == 1)
{
objColor = Color.Red;
}
else if (itemIndex == 2)
{
objColor = Color.Yellow;
}
else if (itemIndex == 3)
{
objColor = Color.Purple;
}
else if (itemIndex == 4)
{
objColor = Color.Orange;
}
else if (itemIndex == 5)
{
objColor = Color.Brown;
}
else if (itemIndex == 6)
{
objColor = Color.Gray;
}
else if (itemIndex == 7)
{
objColor = Color.Maroon;
}
else if (itemIndex == 8)
{
objColor = Color.Maroon;
}
else
{
objColor = Color.Blue;
}
return objColor;
}
只需要将StatPhotos()方法放到page_load下就ok了
在需要做显示的页面放一个image控件
将其ImageURl属性到这个页面就可以了!