<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img alt="" id="code" src="Default3.aspx" style="cursor: pointer;" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
Bitmap objBitMap = new Bitmap(1000, 600);
Graphics objGraphics;
objGraphics = Graphics.FromImage(objBitMap);
objGraphics.Clear(Color.White);
//人数除以10,绘制矩形条。
int[] arrValues = { 50, 50, 50, 50, 100, 1200 };
//厂区
string[] arrValueNames = new string[] { "第一纺纱厂", "第一纺纱厂", "第一纺纱厂", "第一纺纱厂", "第一纺纱厂", "第一纺纱厂" };
//标题
objGraphics.DrawString("三和集团人力资源柱形分析图",
new Font("宋体", 16), Brushes.Black, new PointF(100, 5));
PointF symbolLeg = new PointF(600, 20);
//竖直条
for (int i = 0; i < arrValues.Length; i++)
{
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 80) + 15, 200 - arrValues[i], 20,
arrValues[i] + 5);
objGraphics.DrawRectangle(Pens.Black, (i * 80) + 15, 200 - arrValues[i], 20, arrValues[i] + 5);
objGraphics.DrawString(arrValueNames[i].ToString(), new Font("宋体", 10), Brushes.Black, new PointF((i * 75) + 15, 50));
objGraphics.DrawString(arrValueNames[i].ToString(), new Font("宋体", 10), Brushes.Black, new PointF((i * 75) + 15, 250));
}
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;
//}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
objBitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
#region 柱形图颜色
private Color GetColor(int itemIndex)
{
Color objColor;
if (itemIndex % 2 == 0)
{
objColor = Color.Yellow;
}
else if (itemIndex % 3 == 0)
{
objColor = Color.Purple;
}
else if (itemIndex % 5 == 0)
{
objColor = Color.Brown;
}
else if (itemIndex % 7 == 0)
{
objColor = Color.Maroon;
}
else
{
objColor = Color.Blue;
}
return objColor;
}
#endregion
实例:
// 在此处放置用户代码以初始化页面
Bitmap objBitMap = new Bitmap(5000, 600);
Graphics objGraphics;
objGraphics = Graphics.FromImage(objBitMap);
objGraphics.Clear(Color.White);
ZSql s = new ZSql();
string sql = "select name,(select count(*) from employee where plantId =plant.Id";
//开始时间
if (!string.IsNullOrEmpty(DBHelper.strChartStartDate))
{
sql += " and (FactoryTime >= '" + DBHelper.strChartStartDate + "')";
}
else
{
sql += " and (FactoryTime >= '1980-01-01')";
}
//截止时间
if (!string.IsNullOrEmpty(DBHelper.strChartEndDate))
{
sql += " and (FactoryTime <= '" + DBHelper.strChartEndDate + "')";
}
else
{
sql += " and (FactoryTime <= '2080-01-01')";
}
sql += " )as ProNum from plant ";
DataSet ds = s.DSet(sql);
//标题
objGraphics.DrawString("三和集团人力资源柱形分析图",
new Font("宋体", 16), Brushes.Black, new PointF(500, 5));
PointF symbolLeg = new PointF(600, 20);
//竖直条
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
int ProNum = int.Parse(ds.Tables[0].Rows[i]["ProNum"].ToString());
string name = ds.Tables[0].Rows[i]["name"].ToString();
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 80) + 30, 200 - ProNum / 10, 20, ProNum / 10 + 5);
objGraphics.DrawRectangle(Pens.Black, (i * 80) + 30, 200 - ProNum / 10, 20, ProNum / 10 + 5);
objGraphics.DrawString(ProNum.ToString(), new Font("宋体", 10), Brushes.Black, new PointF((i * 80) + 30, 50));
objGraphics.DrawString(name, new Font("宋体", 10), Brushes.Black, new PointF((i * 80) + 30, 250));
}
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;
//}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
objBitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
#region 柱形图颜色
private Color GetColor(int itemIndex)
{
Color objColor;
if (itemIndex % 2 == 0)
{
objColor = Color.Yellow;
}
else if (itemIndex % 3 == 0)
{
objColor = Color.Purple;
}
else if (itemIndex % 5 == 0)
{
objColor = Color.Brown;
}
else if (itemIndex % 7 == 0)
{
objColor = Color.Maroon;
}
else
{
objColor = Color.Blue;
}
return objColor;
}
#endregion