• 饼形统计图


    数据库同 柱状统计图

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Drawing;
    using System.Data;
    using System.Collections;
    using System.IO;
    
    public partial class CakeImage : System.Web.UI.Page
    {
        public string connStr = ConfigurationManager.ConnectionStrings["VisitCountConnectionString"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            DrawLinearGradient();
        }
        //访问人数统计
        public int Total()
        {
            int result = -1;
            string sql = "select count(1) from VisiteCount";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            result = Convert.ToInt32(cmd.ExecuteScalar());
            cmd.Dispose();
            conn.Close();
            return result;
        }
        //柱形图
        public void DrawLinearGradient()
        {
            int[] count = new int[12];
            string sql = "";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlDataAdapter da;
            DataSet ds = new DataSet();
            for (int i = 0; i < 12; i++)
            {
                sql = @"select count(1) as count,Month(loginTime) as month from VisiteCount where YEAR(loginTime)=2013 and MONTH(loginTime)=" + (i + 1) + " group by MONTH(loginTime)";
                da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, i.ToString());
                if (ds.Tables[i].Rows.Count == 0)
                {
                    count[i] = 0;
                }
                else
                {
                    //count[i] = Convert.ToInt32(ds.Tables[i].Rows[0][0].ToString())*100/Total();
                    count[i] = Convert.ToInt32(ds.Tables[i].Rows[0][0].ToString());
                }
            }
            //设置字体
            Font fontlegend = new Font("verdana",9);
            Font fonttitle = new Font("verdana", 10, FontStyle.Bold);
            //设置背景宽度
            int width = 230;
            int bufferspace = 15;
            int lengendheight = fontlegend.Height * 13 + bufferspace;//饼图下方分类表表高度
            int titleheight = fonttitle.Height+bufferspace;//标题栏高度
            int height = width+lengendheight+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 < 12; i++)
            { 
                colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255),rnd.Next(255),rnd.Next(255))));
            }
            //创建一个bitmap
            Bitmap objimage = new Bitmap(width,height);
            Graphics objg = Graphics.FromImage(objimage);
            objg.FillRectangle(Brushes.White, 0, 0, width, height);
            
            //画一个亮色背景
            objg.FillRectangle(new SolidBrush(Color.Beige),pierect);
            //显示标题
            objg.DrawString("2013网站浏览每月比例调查", fonttitle, Brushes.Black, new PointF(32,12));
            //画饼图
            float currentnum = 0.0f;
            for (int i = 0; i < 12; i++)
            {
                objg.FillPie((SolidBrush)colors[i], pierect, currentnum, Convert.ToSingle(count[i]) / Total() * 360);
                currentnum+=Convert.ToSingle(count[i]) / Total() * 360;
            }
            objg.DrawRectangle(new Pen(Color.Blue, 2), 0, height - lengendheight, width, lengendheight);
            string[] n = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
            for (int i = 0; i < 12; i++)
            {
                objg.FillRectangle((SolidBrush)colors[i], 5, height - lengendheight+fontlegend.Height*i+5,10,10);
                string dble = (Convert.ToDouble(count[i]) / Total() * 100).ToString().Substring(0, 4)+"%";
                objg.DrawString(n[i] + "----" + dble, fontlegend, Brushes.Black,20, height - lengendheight+fontlegend.Height*i+1);
            }
            objg.DrawString("2013网络浏览总数:" + Total() + "", fontlegend, Brushes.Black, 5, height - fontlegend.Height);
            Response.ContentType="image/Jpeg";
            objimage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            objg.Dispose();
            objimage.Dispose();
        }
    }
    View Code
  • 相关阅读:
    剑指offer——包含min函数的栈
    剑指offer——顺时针打印矩阵
    剑指offer——二叉树的镜像
    剑指offer——树的子结构
    爬虫的单线程+多任务异步协程:asyncio 3.6
    爬虫中的模拟登陆,IP代理,线程池
    爬虫-数据解析
    爬虫基础
    Markdown语法
    Git
  • 原文地址:https://www.cnblogs.com/Jokers/p/3522084.html
Copyright © 2020-2023  润新知