Asp.Net(C#)中如何画图表(饼图、折线、柱状图)

来源:互联网 发布:西安华为软件新城 编辑:程序博客网 时间:2024/05/02 16:47

     我们经常在项目中遇到,许多的数据为了真观的体现给用户,不得不引和饼图,趋势图,等等柱状图来开形象的来呈现数据,如果在不使用第三方控件的情况下,我们都知道路在winform程序中是非常容易实现的,如是webform的实现就有点麻烦。本程序是在webform上放Image控件,加上.ashx一般处理文件实现的,原理为将Image的rsc属性转到xxxxx.ashx文件处,xxxxx.ashx文件反加一张已经画好的图片,在调用xxxxx.ashx文件时可以传递参数,如用户信息,根据用户ID生成各用户相将要使用的图:

(1)饼图:

bintu.ashx文件的内容如下:

using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace WebApplication3
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "
http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class getSourceImage : IHttpHandler
    {
        private long TotalSalary;
        public void ProcessRequest(HttpContext context)
        {
            const int PieHeight = 500;
            const int PIeWidth = 520;

            Bitmap objBitmap = new Bitmap(PIeWidth, PieHeight);
            Graphics objGraphics = Graphics.FromImage(objBitmap);
            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, PIeWidth, PieHeight);
            int iLoop2;
            // 定义位置与椭圆的大小。
            int x = 20;
            int y = 20;
            int width = 300;
            int height = 150;
            Rectangle DepartmentLegend;
            int DepartmentLegend_Y = 40;

                // 初始化起点的角度与每块饼的扫过角度。
            int startAngle = 0;
            int sweepAngle = 45;

                // 使用复合模式控制 Alpha 混色。
            SolidBrush colorBrush = new SolidBrush(Color.Aqua);
            Random rand = new Random();

            objGraphics.DrawString("本次统计部门名称:" + "这里填部门名称", new Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, new PointF(x + width + 18, DepartmentLegend_Y));

            DepartmentLegend_Y += 25;

            // 绘制部门名称下方的水平线。
            objGraphics.DrawLine(Pens.Black, x + width + 10, DepartmentLegend_Y, 600, DepartmentLegend_Y);

            DepartmentLegend_Y += 5;
            objGraphics.DrawLine(Pens.Black, x + width + 10, DepartmentLegend_Y, 600, DepartmentLegend_Y);

            DepartmentLegend_Y += 5;

            //---------------------------------------------------------------------------------
            this.TotalSalary = 100;
            Int32[] partSalary =new int[5];
            partSalary[0] = 10;
            partSalary[1] = 20;
            partSalary[2] = 30;
            partSalary[3] = 15;
            partSalary[4] = 25;

            for (int i = 0; i < partSalary.Length; i++)
            {
                sweepAngle = Convert.ToInt32(360 * Convert.ToSingle(partSalary[i]) / Convert.ToSingle(TotalSalary));
                colorBrush.Color = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
                if (startAngle < 180)
                {
                    for (iLoop2 = 0; iLoop2 < 10; iLoop2++)
                    {
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50, colorBrush.Color),
                                            x,
                                            (y + iLoop2),
                                            width,
                                            height,
                                            startAngle,
                                            sweepAngle);
                        iLoop2 += 1;
                    }
                }
                objGraphics.FillPie(colorBrush, x, y, width, height, startAngle, sweepAngle + 1);
                startAngle += Convert.ToInt32(360 * Convert.ToSingle(partSalary[i]) / Convert.ToSingle(TotalSalary));
                DepartmentLegend = new Rectangle(x + width + 10, DepartmentLegend_Y, 20, 20);
                objGraphics.FillRectangle(colorBrush, DepartmentLegend);
                objGraphics.DrawString("AAAA" + "性的平均薪资:" + String.Format("{0:c}", "BBB"), new Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, new PointF(x + width + 30, DepartmentLegend_Y));
                DepartmentLegend_Y += 25;
            }
            //-------------------------------------------------------
            context.Response.ContentType = "Image/PNG";
            context.Response.Clear();
            context.Response.BufferOutput = true;

            using (MemoryStream ms = new MemoryStream())
            {
                objBitmap.Save(ms, ImageFormat.Png);
                ms.Flush();
                context.Response.BinaryWrite(ms.GetBuffer());
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
效果图如下:

柱图如下:

zhutu.ashx文件如下:

using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;


namespace WebApplication3
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int height = 400, width = 700;
            Bitmap image = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(image);
            Pen mypen = new Pen(Color.Blue,1);
            Font font = new Font("Arial", 10, FontStyle.Regular);

            //---------------------------------------------
            g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);

            //---------------------------------------------
            int x = 50;
            for (int i = 0; i < 11; i++)
            {
                g.DrawLine(mypen, x, 50, x, 310);
                x = x + 40;
            }
            //-------------------------------------------------
            int y = 50;
            for (int i = 0; i < 11; i++)
            {
                g.DrawLine(mypen, 50, y, 450, y);
                y = y + 26;
            }

            //---------------------------------------------------
            String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年","全年"};
            x = 60;
            for (int i = 0; i < 7; i++)
            {
                g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 315);
                x = x + 55;
            }
            //---------------------------------------------------------------------

            String[] m = {"250","225", "200", "175", "150", "125", "100", " 75"," 50", " 25", " 0"};
            y = 55;
            for (int i = 0; i < 10; i++)
            {
                g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y);
                y = y + 26;
            }
            //------------------------------------------------------------------------------
            int[] count1 = new int[5];
            int[] count2 = new int[5];
            count1[0] = 90; count1[1] = 50; count1[2] = 120; count1[3] = 30; count1[4] = 70;
            count2[0] = 120; count2[1] = 80; count2[2] = 60; count2[3] = 90; count2[4] = 100;

            //--------------------------------------------------------------------------------
            x = 50;
            Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
            SolidBrush mybrush = new SolidBrush(Color.Red);
            SolidBrush mybrush2 = new SolidBrush(Color.Green);
            for (int i = 0; i < 5; i++)
            {
               
                //-------------------------------------------------------------------------------------

                g.FillRectangle(mybrush, x, 310 - count1[i], 25, count1[i]);
                g.DrawString(count1[i].ToString(), font2, Brushes.Red, x, 310 - count1[i] - 15);
                //------------------------------------
                x = x + 80;
               
            }
            x = 90;
            for (int i = 0; i < 5; i++)
            {
                g.FillRectangle(mybrush2, x, 310 - count2[i], 25, count2[i]);
                g.DrawString(count2[i].ToString(), font2, Brushes.Green, x, 310 - count2[i] - 15);
                x = x + 80;

            }


            context.Response.ContentType = "Image/PNG";
            context.Response.Clear();
            context.Response.BufferOutput = true;

            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, ImageFormat.Png);
                ms.Flush();
                context.Response.BinaryWrite(ms.GetBuffer());
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
效果图发下:

 

原创粉丝点击