柱状统计图

sql语句:

create database DBTest
go
use DBTest
go
create table VisiteCount
(
    id int identity(1,1) primary key not null,--自增列
    loginTime Datetime --访问时间
)

insert into VisiteCount values('2013-01-01');
insert into VisiteCount values('2013-02-01');
insert into VisiteCount values('2013-02-02');
insert into VisiteCount values('2013-03-01');
insert into VisiteCount values('2013-03-02');
insert into VisiteCount values('2013-03-03');
insert into VisiteCount values('2013-04-01');
insert into VisiteCount values('2013-04-02');
insert into VisiteCount values('2013-04-03');
insert into VisiteCount values('2013-04-04');
insert into VisiteCount values('2013-05-01');
insert into VisiteCount values('2013-05-02');
insert into VisiteCount values('2013-05-03');
insert into VisiteCount values('2013-05-04');
insert into VisiteCount values('2013-05-05');
insert into VisiteCount values('2013-06-01');
insert into VisiteCount values('2013-06-02');
insert into VisiteCount values('2013-06-03');
insert into VisiteCount values('2013-06-04');
insert into VisiteCount values('2013-06-05');
insert into VisiteCount values('2013-06-06');
insert into VisiteCount values('2013-07-01');
insert into VisiteCount values('2013-07-02');
insert into VisiteCount values('2013-07-03');
insert into VisiteCount values('2013-07-04');
insert into VisiteCount values('2013-07-05');
insert into VisiteCount values('2013-07-06');
insert into VisiteCount values('2013-07-07');
insert into VisiteCount values('2013-08-01');
insert into VisiteCount values('2013-08-02');
insert into VisiteCount values('2013-08-03');
insert into VisiteCount values('2013-08-04');
insert into VisiteCount values('2013-08-05');
insert into VisiteCount values('2013-08-06');
insert into VisiteCount values('2013-08-07');
insert into VisiteCount values('2013-08-08');
insert into VisiteCount values('2013-09-01');
insert into VisiteCount values('2013-09-02');
insert into VisiteCount values('2013-09-03');
insert into VisiteCount values('2013-09-04');
insert into VisiteCount values('2013-09-05');
insert into VisiteCount values('2013-09-06');
insert into VisiteCount values('2013-09-07');
insert into VisiteCount values('2013-09-08');
insert into VisiteCount values('2013-09-09');
insert into VisiteCount values('2013-10-01');
insert into VisiteCount values('2013-10-02');
insert into VisiteCount values('2013-10-03');
insert into VisiteCount values('2013-10-04');
insert into VisiteCount values('2013-10-05');
insert into VisiteCount values('2013-10-06');
insert into VisiteCount values('2013-10-07');
insert into VisiteCount values('2013-10-08');
insert into VisiteCount values('2013-10-09');
insert into VisiteCount values('2013-10-10');
insert into VisiteCount values('2013-11-01');
insert into VisiteCount values('2013-11-02');
insert into VisiteCount values('2013-11-03');
insert into VisiteCount values('2013-11-04');
insert into VisiteCount values('2013-11-05');
insert into VisiteCount values('2013-11-06');
insert into VisiteCount values('2013-11-07');
insert into VisiteCount values('2013-11-08');
insert into VisiteCount values('2013-11-09');
insert into VisiteCount values('2013-11-10');
insert into VisiteCount values('2013-11-11');
insert into VisiteCount values('2013-12-01');
insert into VisiteCount values('2013-12-02');
insert into VisiteCount values('2013-12-03');
insert into VisiteCount values('2013-12-04');
insert into VisiteCount values('2013-12-05');
insert into VisiteCount values('2013-12-06');
insert into VisiteCount values('2013-12-07');
insert into VisiteCount values('2013-12-08');
insert into VisiteCount values('2013-12-09');
insert into VisiteCount values('2013-12-10');
insert into VisiteCount values('2013-12-11');
insert into VisiteCount values('2013-12-12');
View Code

cs代码:

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.Drawing.Drawing2D;
using System.Data;

public partial class VisiteCount : 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 height = 400, width = 600;//定义画布长宽
        /*新建一个画布*/
        Bitmap image = new Bitmap(width, height); 
        //Graphics GDI+绘图的表面。即绘图的一个平台。
        Graphics g = Graphics.FromImage(image);

        /*绘制背景墙、网格线以及坐标*/
        g.Clear(Color.White);//清空背景
        //创建字体对象
        Font font = new Font("Arial",9,FontStyle.Regular);
        Font font1 = new Font("宋体",20,FontStyle.Bold);
        //创建一个矩形
        Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
        //创建一个线性渐变画笔。
        LinearGradientBrush brush = new LinearGradientBrush(rectangle, Color.Blue, Color.BlueViolet, 1.2f, true);
        //调用Graphics对象的FileRectangle方法在画布中绘制一个使用渐变图案填充的矩形。
        g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
        g.DrawString("2013年各月份网站流量统计", font1, brush, new PointF(130, 30));

        /*画图片的边框线*/
        //绘制由坐标对、宽度和高度指定的矩形。
        g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
        
        /*绘制线条*/
        //绘制纵向线条
        int x = 100;
        Pen mypen = new Pen(brush,1); //用于绘制直线和曲线对象
        for (int i = 0; i < 11; i++)
        {
            g.DrawLine(mypen, x, 80, x, 340);
            x = x + 40;
        }
        Pen mypen1 = new Pen(Color.Blue, 2);
        g.DrawLine(mypen1, x - 480, 80, x - 480, 340);
        //绘制横向线条
        int y = 106;
        for (int i = 0; i < 9; i++)
        {
            g.DrawLine(mypen, 60, y, 540, y);
            y = y + 26;
        }
        g.DrawLine(mypen1, 60, y, 540, y);

        /*为已经绘制的坐标轴绘制数据标记。X轴显示月份,y轴显示百分比刻度*/
        //X轴
        string[] n = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
        x = 62;
        for (int i = 0; i < 12; i++)
        {
            g.DrawString(n[i].ToString(), font, Brushes.Black, x, 380);
            x = x + 40;
        }
        //y轴
        string[] m = {"100%","90%","80%","70%","60%","50%","40%","30%","20%","10%","0%"};
        y = 85;
        for (int i = 0; i < 11; i++)
        {
            g.DrawString(m[i].ToString(),font,Brushes.Black,25,y);//设置文字内容以及输出位置
            y = y + 25;
        }
        //将检索出的数据按一定比例绘制到图像中
        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());
            }
        }

        //显示柱状效果图
        x = 70;
        for (int i = 0; i < 12; i++)
        {
            //g.DrawString(count[i], font, Brushes.Red, x-5, 320 - count[i] * 26 / 10);
            string dble = (Convert.ToDouble(count[i])/Total()*100).ToString().Substring(0,4);
            g.DrawString(dble.ToString()+"%", font, Brushes.Red, x-5, 320 - (count[i]*100/Total()) * 26 / 10);
            SolidBrush mybrash = new SolidBrush(Color.Blue);
            g.FillRectangle(mybrash, x, 340 - (count[i] * 100 / Total()) * 26 / 10, 20, (count[i] * 100 / Total()) * 26 / 10);
            x = x+40;
        }

        //将绘制好的助兴图标显示在页面上
        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());
    }
}
View Code

 

posted @ 2014-01-15 10:38  时光未曾老去.  阅读(405)  评论(0编辑  收藏  举报