您的位置:首页 > 编程语言 > ASP

ASP.NET动态创建柱状图与饼状图

2011-01-10 11:17 381 查看
2007年12月29日 星期六 下午 01:42
来源:http://blog.csdn.net/yichangxin/archive/2006/08/01/1008011.aspx

做web项目开发,难免会遇到动态创建柱状图或饼型图。现在用.net技术实现动态的创建它们……

动态创建柱状图与饼状图的前台HTML代码如下:

[align=left]<body>[/align]
[align=left] <form id="Form1" method="post" runat="server">[/align]
[align=left] <table width="517" border="0" height="255">[/align]
[align=left] <tr>[/align]
[align=left] <td align="middle"><img src="WebForm1.aspx"></td>[/align]
[align=left] </tr>[/align]
[align=left] <tr>[/align]
[align=left] <td height="20" align="middle">ASP.NET 中动态创建图形范例</td>[/align]
[align=left] </tr>[/align]
[align=left] </table>[/align]
[align=left] </form>[/align]
[align=left]</body>[/align]
[align=left][/align]

程序运行后的效果如图:



[align=left]后台代码如下:[/align]
[align=left][/align]

[align=left]using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging; //添加 Imaging 的引用。

namespace fund
{
///<summary>
/// ChartText 的摘要说明。
/// 作者:Shadow
///</summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Bitmap objBitMap = new Bitmap(400, 240);
Graphics objGraphics;
objGraphics = Graphics.FromImage(objBitMap);
objGraphics.Clear(Color.White);
int[] arrValues = {40000,32000,24000,30000,36000,28000};
string[] arrValueNames = new string[]{"第一次","第二次","第三次","第四次","第五次","第六次"};
objGraphics.DrawString(" 山东鲁能主场球迷人数统计(人)",
new Font("宋体", 16), Brushes.Blue, 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;
}

float TotalValues = 0;
for (int i = 0; i <= arrValues.Length - 1; i++)
{
TotalValues += arrValues[i];
}
//绘出矩形图。
float Rectangleheight = 0;
PointF recLeg = new PointF(12,200-arrValues[0]/TotalValues*300);
for (int i = 0; i < arrValues.Length; i++)
{
Rectangleheight = arrValues[i] / TotalValues *300;
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 35) + 15, 200 - Rectangleheight, 20,
Rectangleheight + 50);
objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 - Rectangleheight, 20, Rectangleheight + 50);
recLeg.Y=200-Rectangleheight-14;
objGraphics.DrawString(arrValues[i].ToString(), new Font("宋体", 10), Brushes.Blue, recLeg);
recLeg.X+=35;
}
//绘出圆形图。
float sglCurrentAngle = 0;
float sglTotalAngle = 0;
for (int i = 0; i < arrValues.Length; i++)
{
sglCurrentAngle = arrValues[i] / TotalValues * 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);
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///<summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///</summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

//定义颜色。
private Color GetColor(int itemIndex)
{
Color objColor;
if (itemIndex == 0)
{
objColor = Color.Maroon;
}
else if (itemIndex == 1)
{
objColor = Color.Red;
}
else if (itemIndex == 2)
{
objColor = Color.Gray;
}
else if (itemIndex == 3)
{
objColor = Color.Blue;
}
else if (itemIndex == 4)
{
objColor = Color.Orange;
}
else if (itemIndex == 5)
{
objColor = Color.Cyan;
}
else if (itemIndex == 6)
{
objColor = Color.Bisque;
}
else if (itemIndex == 7)
{
objColor = Color.Maroon;
}
else if (itemIndex == 8)
{
objColor = Color.Maroon;
}
else
{
objColor = Color.Blue;
}
return objColor;
}

}
}[/align]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: