本文实例讲述了asp.net中生成饼状与柱状图的实现方法。分享给大家供大家参考。具体方法如下:

一、生成图形的公共方法:
复制代码 代码如下:using System;
using System.Collections.Generic;
using System.Text;
//
//using System.Data;
//using System.Web.UI.WebControls;
//
using System.Drawing;
using System.Drawing.Imaging;
 
namespace Tools
{
    public static class OWCImageHelp
    {
        /// <summary>
        /// 动态的生成柱状图和饼状图
        /// </summary>
        /// <param name="arrValueNames">行坐标要显示的字段</param>
        /// <param name="arrValues">纵坐标要显示的数字</param>
        /// <param name="title">标题</param>
        public static void GetZBImage(string[] arrValueNames, int[] arrValues, string title)
        {
            Bitmap objBitMap = new Bitmap(650, 300);
            Graphics objGraphics;
            objGraphics = Graphics.FromImage(objBitMap);
            objGraphics.Clear(Color.White);
            //int[] arrValues = { 40000, 32000, 24000, 30000, 36000, 28000 };
            //string[] arrValueNames = new string[] { "第一次", "第二次", "第三次", "第四次", "第五次", "第六次" };
            objGraphics.DrawString(title, new System.Drawing.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 System.Drawing.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 System.Drawing.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(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Gif);
        }
        //定义颜色。
        private static 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;
        }
    }
}
二、新建生成饼状柱状图页面BZImage.aspx:
后台:
复制代码 代码如下:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Models;
public partial class GridViewDemo_BZImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetBZIamge();
    }
    /// <summary>
    /// 生成饼状柱状图
    /// </summary>
    public void GetBZIamge()
    {
        DataTable dt = BLL.StudentBLL.SelAllStudent();
        string[] rows = new string[dt.Rows.Count];
        int[] columns = new int[dt.Rows.Count];
        for (int i = 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, "学生薪水查询");
    }
}
三、显示饼状柱状图的页面:
前台:
复制代码 代码如下:<table style="width: 600px" onMouseOver="over()" onMouseOut="out()">
            <tr>
             <td style="height: 21px; width: 35px;" align="center">
                    <img id="BZImage" src="/UploadFiles/2021-04-02/BZImage.aspx">                 </td>
            </tr>
</table>

希望本文所述对大家的asp.net程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。