asp.net|饼状图|创建|动态|柱状图 做web项目开发,难免会遇到动态创建柱状图或饼型图。现在用.net技术实现动态的创建它们。由于本身比较喜欢足球,更是鲁能的球迷,所以就用了鲁能的主场球迷人数作为例子,请不要太惊奇
! 一个人在西安的山东人希望鲁能可以提前夺冠!!呵呵,扯远了……
动态创建柱状图与饼状图的前台HTML代码如下:
<body> <form id="Form1" method="post" runat="server"> <table width="517" border="0" height="255"> <tr> <td align="middle"><img src="ChartText.aspx"></td> </tr> <tr> <td height="20" align="middle">ASP.NET 中动态创建图形范例</td> </tr> </table> </form> </body>程序运行后的效果如图:
后台代码如下: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 WebApplication1{ ///<summary> /// ChartText 的摘要说明。 /// 作者:Shadow ///</summary> public class ChartText : 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 = newstring[]{"第一次","第二次","第三次","第四次","第五次","第六次"}; 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; } 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; } return objColor; } }}