效果图如下:
1.Page_load事件
Bitmap objBitMap = new Bitmap(400, 200);
Graphics objGraphics;
objGraphics = Graphics.FromImage(objBitMap);
objGraphics.Clear(Color.White);
int[] arrValues = {100,135,115,125,75,120};
string[] arrValueNames = new string[]{"一月","二月","三月","四月","五月","六月"};
objGraphics.DrawString(" X 公司上半年销售情况",
new Font("宋体", 16), Brushes.Black, new PointF(5, 5));
PointF symbolLeg = new PointF(335, 20);
PointF descLeg = new PointF(360, 16);
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 < arrValues.Length; i++)
{
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 35) + 15, 200 - arrValues[i], 20,
arrValues[i] + 5);
objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 - arrValues[i], 20, arrValues[i] + 5);
}
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;
}
objBitMap.Save(Response.OutputStream, ImageFormat.Gif);
2.GetColor()函数
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;
& |