ASP.net用Graphics实现的统计图(折线图、柱状图、饼图)

一.折线图

转载ASP.net用Graphics实现的统计图(折线图、柱状图、饼图)
代码:

private void CreateImage()
        {
            int height = 480, width = 700;
            Bitmap image = new Bitmap(width,height);
            Graphics g = Graphics.FromImage(image);
            try
            {
                  //清空图片背景
                g.Clear(Color.White);

Font font = new Font("Arial",9,FontStyle.Regular);
                Font font1 = new Font("宋体",20,FontStyle.Bold);
                Font font2 = new Font("Arial",8,FontStyle.Regular);

LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.BlueViolet,1.2f,true);
                g.FillRectangle(Brushes.AliceBlue,0,0,width,height);

Brush brush1 = new SolidBrush(Color.Blue);
                Brush brush2 = new SolidBrush(Color.SaddleBrown);

g.DrawString("北京市 2013年XX统计折线图",font1,brush1,new PointF(85,30));

//画图片的边框线
                g.DrawRectangle(new Pen(Color.Blue),0,0,image.Width-1,image.Height-1);

Pen mypen = new Pen(brush,1);
                Pen mypen2 = new Pen(Color.Red,2);

//绘制线条
                //绘制纵向线条
                int x = 60;
                for (int i = 0; i < 8; ++i)
                {
                    g.DrawLine(mypen,x,80,x,340);
                    x += 80;
                }
                Pen mypen1 = new Pen(Color.Blue,3);
                x = 60;
                g.DrawLine(mypen1,x,82,x,340);

//绘制横向线条
                int y = 106;
                for (int i = 0; i < 10; ++i)
                {
                    g.DrawLine(mypen,60,y,620,y);
                    y += 26;
                }
                g.DrawLine(mypen1,60,y-26,620,y-26);

//x轴
                String[] n = {"第一期","第二期","第三期","第四期","上半年","下半年","全年统计" };
                x = 45;
                for (int i = 0; i < 7; i++)
                {
                    g.DrawString(n[i].ToString(),font,Brushes.Red,x,348);
                    x += 77;
                }
                //y轴
                String[] m = { "220人","200人","175人","150人","125人","100","75人","50人","25人"};
                y = 100;
                for (int i = 0; i < 9;++i )
                {
                    g.DrawString(m[i].ToString(),font,Brushes.Red,10,y);
                    y += 26;
                }

int[] Count1 = { 28,60,56,44,88,110,198};
                int[] Count2 = { 15,24,25,13,39,38,37};

//显示折线效果
                Font font3 = new Font("Arial",10,FontStyle.Bold);
                SolidBrush mybrush = new SolidBrush(Color.Red);
                Point[] points1=new Point[7];
                Point[] points2 = new Point[7];
                int startX = 60;
                int startX1=58;
                for (int t = 0; t < 7; ++t)
                {
                    points1[t].X = startX; points1[t].Y = 340 - Count1[t];
                    points2[t].X = startX; points2[t].Y = 340 - Count2[t];
                    startX += 80;
                    //绘制数字
                    g.DrawString(Count1[t].ToString(),font3,Brushes.Red,startX1,points1[t].Y-20);
                    g.DrawString(Count2[t].ToString(),font3,Brushes.Green,startX1+1,points2[t].Y-15);
                    startX1 += 80;
                }
                //绘制折线
                g.DrawLines(mypen2,points1);

Pen mypen3 = new Pen(Color.Green,2);
                g.DrawLines(mypen3,points2);

//绘制标识
                g.DrawRectangle(new Pen(Brushes.Red),180,390,250,50);
                g.FillRectangle(Brushes.Red,270,402,20,10);//绘制小矩形
                g.DrawString("报名人数",font2,Brushes.Red,292,400);

g.FillRectangle(Brushes.Green,270,422,20,10);
                g.DrawString("通过人数",font2,Brushes.Green,292,420);

System.IO.MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.ClearContent();
                Response.ContentType = "image/Jpeg";
                Response.BinaryWrite(ms.ToArray());


            }
            catch (Exception e)
            {
              
            }finally
            {
                image.Dispose();
                image.Dispose();
            }
        }

二.柱状图

 

转载ASP.net用Graphics实现的统计图(折线图、柱状图、饼图)

代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

using System.Drawing.Drawing2D;

namespace zhuzhuangtu

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            CreateImage();

        }

        private void CreateImage()

        {

            int height = 500, width = 700;

            Bitmap image = new Bitmap(width, height);

            //创建Graphics类对象

            Graphics g = Graphics.FromImage(image);

            try

            {

                //清空图片背景色

                g.Clear(Color.Red);

                Font font = new Font("Arial", 10, FontStyle.Regular);

                Font font1 = new Font("宋体", 20, FontStyle.Bold);

 

                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true);

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

 

                g.DrawString("河北省" + " " + "2010年" + "成绩统计柱状图", font1, brush, new PointF(70, 30));

 

                //画图片的边框线

                g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);

 

                Pen mypen = new Pen(brush, 1);

                //绘制斜线

                //绘制横向线条

                int x = 100;

                for (int i = 0; i < 14; i++)

                {

                    g.DrawLine(mypen, x, 80, x, 340);

                    x = x + 40;

                }

                Pen mypen1 = new Pen(Color.Blue, 2);

                x = 60;

                g.DrawLine(mypen1, x, 80, x, 340);

 

                //绘制纵向线条

                int y = 106;

                for (int i = 0; i < 9; ++i)

                {

                    g.DrawLine(mypen, 60, y, 620, y);

                    y = y + 26;

                }

                g.DrawLine(mypen1, 60, y, 620, y);

 

                //x轴

                string[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" };

                x = 78;

                for (int i = 0; i < 7; ++i)

                {

                    g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348);  //设置文字内容及输出

                    x = x + 78;

                }

                //y轴

                string[] m = { "250", "225", "200", "175", "150", "125", "100", "75", "50", "25", "0" };

                y = 72;

                for (int i = 0; i < 10; ++i)

                {

                    g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y);  //设置文字内容及输出

                    y = y + 26;

                }

                //绘制柱状图

                x = 80;

                Font font2 = new Font("Arial",10,FontStyle.Bold);

                SolidBrush mybrush = new SolidBrush(Color.Red);

                SolidBrush mybrush2 = new SolidBrush(Color.Green);

 

                int[] Count1 = {39,111,71,40,150,111,261 };

                int[] Count2 = { 26, 68, 35, 14, 94, 49, 114 };

 

                //共7期

                for (int i = 0; i < 7; ++i)

                {

                    g.FillRectangle(mybrush, x, 340 - Count1[i], 20, Count1[i]);

                    g.DrawString(Count1[i].ToString(), font2, Brushes.Red, x, 340 - Count1[i] - 15);

                    x += 20;

                    g.FillRectangle(mybrush2, x, 340 - Count2[i], 20, Count2[i]);

                    g.DrawString(Count2[i].ToString(), font2, Brushes.Green, x, 340 - Count2[i] - 15);

                    x += 60;

                }

 

                //绘制标识

                Font font3 = new Font("Arial",10,FontStyle.Regular);

                g.DrawRectangle(new Pen(Brushes.Blue),170,400,250,50);

                g.FillRectangle(Brushes.Red,270,410,20,10);

                g.DrawString("报名人数",font3,Brushes.Red,292,408);

 

                g.FillRectangle(Brushes.Green,270,430,20,10);

                g.DrawString("通过人数",font3,Brushes.Green,292,428);

 

                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                Response.ClearContent();

                Response.ContentType = "image/Jpeg";

                Response.BinaryWrite(ms.ToArray());

 

            }

            catch (Exception e)

            {

            }

            finally

            {

                g.Dispose();

                image.Dispose();

            }

        }

    }

}

三 .饼状图

 转载ASP.net用Graphics实现的统计图(折线图、柱状图、饼图)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Collections;
namespace zhexian
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateImage();
        }
        private void CreateImage()
        {
            //int height = 480, width = 700;
            //Bitmap image = new Bitmap(width,height);
            //Graphics g = Graphics.FromImage(image);
            try
            {
                  //清空图片背景
                //g.Clear(Color.White);
                Font fontlegend = new Font("verdana",9);
                Font fonttitle = new Font("verdana",10,FontStyle.Bold);
                float Total = 0.0f, tmp;
                Total = Convert.ToSingle(50);

                //背景宽
                int width = 350;
                int bufferspace = 15;
                int legendheight = fontlegend.Height * 10 + bufferspace;
                int titleheight = fonttitle.Height + bufferspace;
                int height = width + legendheight + titleheight + bufferspace;
                int pieheight = width;
                Rectangle pierect = new Rectangle(0,titleheight,width,pieheight);

                //加上各种随即色
                ArrayList colors = new ArrayList();
                Random rnd = new Random();
                for (int i = 0; i < 2;++i )
                {
                    colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255),rnd.Next(255),rnd.Next(255))));
                }

                //创建一个bitmap实例
                Bitmap objbitmap = new Bitmap(width,height);
                Graphics objgraphics = Graphics.FromImage(objbitmap);

                //画一个白色背景
                objgraphics.FillRectangle(new SolidBrush(Color.White),0,0,width,height);

                //画一个亮黄色背景
                objgraphics.FillRectangle(new SolidBrush(Color.Beige),pierect);

                //以下为画饼图(有几行row画几个)
                float currentdegree = 0.0f;

                //画通过人数
                objgraphics.FillPie((SolidBrush)colors[1],pierect,currentdegree,Convert.ToSingle(-45));
                currentdegree += -45;


                //画未通过人数
                objgraphics.FillPie((SolidBrush)colors[0], pierect,currentdegree,Convert.ToSingle(-315));
                //以下为生成主标题              
                SolidBrush blackbrush = new SolidBrush(Color.Black);
                SolidBrush bluebrush = new SolidBrush(Color.Blue);
                string title = "机关单位成绩统计饼状图";
                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                objgraphics.DrawString(title,fonttitle,blackbrush,new Rectangle(0,0,width,titleheight),stringFormat);

                //列出各字段与得数目
                objgraphics.DrawRectangle(new Pen(Color.Red,2),0,height+10-legendheight,width,legendheight+50);
                objgraphics.DrawString("-----------统计信息----------",fontlegend,blackbrush,20,height-legendheight+fontlegend.Height*1+1);
                objgraphics.DrawString("统计单位:XX学校",fontlegend,blackbrush,20,height-legendheight+fontlegend.Height*3+1);
                objgraphics.DrawString("统计年份:2013年",fontlegend,blackbrush,20,height-legendheight+fontlegend.Height*4+1);
                objgraphics.DrawString("统计期数:34",fontlegend,blackbrush,20,height-legendheight+fontlegend.Height*5+1);


                System.IO.MemoryStream ms = new MemoryStream();
                objbitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.ClearContent();
                Response.ContentType = "image/Jpeg";
                Response.BinaryWrite(ms.ToArray());
                objbitmap.Dispose();
                objbitmap.Dispose();

            }
            catch (Exception e)
            {
              
            }
        }
    }
}

posted @ 2020-05-20 17:15  追逐苦痛  阅读(819)  评论(0编辑  收藏  举报