饼形统计图

2018-06-17 20:14:11来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

数据库同 柱状统计图

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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Csharp: FreeTextbox 编辑器控件运行时错误: &#39;FTB_ResizeGal

下一篇:Jmail发送邮件与带附件乱码解决办法