当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

在ASP.NET中动态生成饼图的方法

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 0:49:00

:2011-02-07 09:19:46

在ASP.NET中动态的生成GIF图片一文中介绍了使用ASP.NET生成一个GIF图片的方法,本文对这个例子稍加修改就可以生成比较实用的饼图了。

源代码如下:

/////////////////////////////////////////////////////////
<%@ Page Language="C#" ContentType="image/jpeg" %>
<%@ Import namespace="System.Drawing"%>
<html>
<head>
   <script language="C#" runat="server"> 
      void Page_Load(object sender,EventArgs e)
      {
         Bitmap image=new Bitmap(350,200); 
         Graphics g=Graphics.FromImage(image);
         g.Clear(Color.White);
         Rectangle outline=new Rectangle(10,5,300,100); 
         g.DrawEllipse(new Pen(Color.Black,8.0f),outline);
         g.FillPie(new SolidBrush(Color.Red),outline,-20f,120f);
        //这些角度的大小可以由数据库中的数据通过比例计算决定 
        g.FillPie(new SolidBrush(Color.Yellow),outline,100f,120f);
         g.FillPie(new SolidBrush(Color.Blue),outline,220f,100f);
         g.FillPie(new SolidBrush(Color.Green),outline,320f,40f); 
         image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
      }
   </script>
</head>
<body>
   <form runat="server"> </form>
</body>
</html>

使用上面的方法只能显示一个纯粹的图片这样并不能满足我们图文并茂的要求。如果上面的页面名字叫Images.aspx想在其他页面中引用这个动态生成的图片可以在该页中加上下面的语句:

<img src="Images.aspx">

这样在你需要的地方就可以插入一个漂亮的饼图了。



在ASP.NET中动态生成饼图的方法