asp.net中生成饼状与柱状图实例
本文实例讲述了asp.net中生成饼状与柱状图的实现方法。分享给大家供大家参考。具体方法如下:
一、生成图形的公共方法:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
//
//usingSystem.Data;
//usingSystem.Web.UI.WebControls;
//
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
namespaceTools
{
publicstaticclassOWCImageHelp
{
///<summary>
///动态的生成柱状图和饼状图
///</summary>
///<paramname="arrValueNames">行坐标要显示的字段</param>
///<paramname="arrValues">纵坐标要显示的数字</param>
///<paramname="title">标题</param>
publicstaticvoidGetZBImage(string[]arrValueNames,int[]arrValues,stringtitle)
{
BitmapobjBitMap=newBitmap(650,300);
GraphicsobjGraphics;
objGraphics=Graphics.FromImage(objBitMap);
objGraphics.Clear(Color.White);
//int[]arrValues={40000,32000,24000,30000,36000,28000};
//string[]arrValueNames=newstring[]{"第一次","第二次","第三次","第四次","第五次","第六次"};
objGraphics.DrawString(title,newSystem.Drawing.Font("宋体",16),Brushes.Blue,newPointF(5,5));
PointFsymbolLeg=newPointF(335,20);
PointFdescLeg=newPointF(360,16);
//画出说明部分的图形
for(inti=0;i<arrValueNames.Length;i++)
{
objGraphics.FillRectangle(newSolidBrush(GetColor(i)),symbolLeg.X,symbolLeg.Y,20,10);
objGraphics.DrawRectangle(Pens.Black,symbolLeg.X,symbolLeg.Y,20,10);
objGraphics.DrawString(arrValueNames[i].ToString(),newSystem.Drawing.Font("宋体",10),Brushes.Black,descLeg);
symbolLeg.Y+=15;
descLeg.Y+=15;
}
floatTotalValues=0;
for(inti=0;i<=arrValues.Length-1;i++)
{
TotalValues+=arrValues[i];
}
//绘出矩形图。
floatRectangleheight=0;
PointFrecLeg=newPointF(12,200-arrValues[0]/TotalValues*300);
for(inti=0;i<arrValues.Length;i++)
{
Rectangleheight=arrValues[i]/TotalValues*300;
objGraphics.FillRectangle(newSolidBrush(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(),newSystem.Drawing.Font("宋体",10),Brushes.Blue,recLeg);
recLeg.X+=35;
}
//绘出圆形图。
floatsglCurrentAngle=0;
floatsglTotalAngle=0;
for(inti=0;i<arrValues.Length;i++)
{
sglCurrentAngle=arrValues[i]/TotalValues*360;
objGraphics.FillPie(newSolidBrush(GetColor(i)),220,95,100,100,sglTotalAngle,sglCurrentAngle);
objGraphics.DrawPie(Pens.Black,220,95,100,100,sglTotalAngle,sglCurrentAngle);
sglTotalAngle+=sglCurrentAngle;
}
objBitMap.Save(System.Web.HttpContext.Current.Response.OutputStream,ImageFormat.Gif);
}
//定义颜色。
privatestaticColorGetColor(intitemIndex)
{
ColorobjColor;
if(itemIndex==0)
{
objColor=Color.Maroon;
}
elseif(itemIndex==1)
{
objColor=Color.Red;
}
elseif(itemIndex==2)
{
objColor=Color.Gray;
}
elseif(itemIndex==3)
{
objColor=Color.Blue;
}
elseif(itemIndex==4)
{
objColor=Color.Orange;
}
elseif(itemIndex==5)
{
objColor=Color.Cyan;
}
elseif(itemIndex==6)
{
objColor=Color.Bisque;
}
elseif(itemIndex==7)
{
objColor=Color.Maroon;
}
elseif(itemIndex==8)
{
objColor=Color.Maroon;
}
else
{
objColor=Color.Blue;
}
returnobjColor;
}
}
}二、新建生成饼状柱状图页面BZImage.aspx:
后台:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingBLL;
usingModels;
publicpartialclassGridViewDemo_BZImage:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
GetBZIamge();
}
///<summary>
///生成饼状柱状图
///</summary>
publicvoidGetBZIamge()
{
DataTabledt=BLL.StudentBLL.SelAllStudent();
string[]rows=newstring[dt.Rows.Count];
int[]columns=newint[dt.Rows.Count];
for(inti=0;i<dt.Rows.Count;i++)
{
rows[i]=dt.Rows[i]["学生姓名"].ToString();
columns[i]=Convert.ToInt32(dt.Rows[i]["薪金"].ToString());
}
Tools.OWCImageHelp.GetZBImage(rows,columns,"学生薪水查询");
}
}三、显示饼状柱状图的页面:
前台:
<tablestyle="width:600px"onMouseOver="over()"onMouseOut="out()">
<tr>
<tdstyle="height:21px;width:35px;"align="center">
<imgid="BZImage"src="BZImage.aspx"alt=""/>
</td>
</tr>
</table>
希望本文所述对大家的asp.net程序设计有所帮助。