惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - 追逐苦痛

20230509001 - DataTable 导出成Excel 20230425001 - DataGridView绑定了数据之后, 再添加CheckBox列的解决方案 20230424001 - 打开文件对话框OpenFileDialog类 20200812001 - SQL openquery 传参数 20190917002 - SQL 中处理交叉重复条件参考 20190917001 - 去除DataTable中重复的数据 20190410001 - 遍历控件参考 20180709001 - 委托传值 20180706001 - 动态添加 tabPage 20180519001 - DataTable Group by功能参考 20161020001 DataGridView 选中的 DataGridViewCheckBoxCell 不添加重复项 20161014006 DataGridView Combobox 数据绑定与传值 20161014001 DataGridView 单元格内容 自动计算 20161013001 DataGridView 数据转 DataTable 20161011001 treeView 递归 20160929001 Guid生成 20160815001 - 修改字段的长度 20160715001 - 分发与订阅 总结 20160712001 SQL server R2 更名
ASP.net用Graphics实现的统计图(折线图、柱状图、饼图)
追逐苦痛 · 2020-05-20 · via 博客园 - 追逐苦痛

一.折线图

转载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)
            {
              
            }
        }
    }
}