欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net中生成饼状与柱状图实例

程序员文章站 2024-02-23 14:48:28
本文实例讲述了asp.net中生成饼状与柱状图的实现方法。分享给大家供大家参考。具体方法如下: 一、生成图形的公共方法: 复制代码 代码如下:using system;...

本文实例讲述了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="bzimage.aspx" alt=""/>
                </td>
            </tr>
</table>

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