0%

csharp 网站人气指数曲线分析

  • 来源:明日科技
  • 网站人气指数曲线分析

    实例说明

    网站访问量可以决定网站的人气,那么对网站月访问量的统计分析,可以让网站管理员准确地判断网民上网的需求周期,从而调整网站的内容,更好地为广大网民服务。本实例通过对数据库中的数据进行分析,将每月份网站访问量以折线形式反映给用户。
    实例运行效果如图1所示。
    图1 网站人气指数曲线分析

    关键技术

    本实例主要使用Graphics类的DrawLines方法来绘制网站人气指数曲线图。

    设计过程

    (1)打开Visual Studio 2008开发环境,新建一个Windows窗体应用程序,命名为SiteVisterAnalyse。
    (2)更改默认窗体Form1的Name属性为Frm_Main,在该窗体中添加一个Panel控件,用于显示绘制的网站人气指数折线图。
    (3)程序主要代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    private void drowPic()
    {
    Graphics g = this.CreateGraphics(); //创建Graphics对象
    g.Clear(Color.WhiteSmoke);//设置背景色
    Pen p = new Pen(Color.Blue);//绘制画笔
    //设置用到的字体
    Font fontO = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
    Font fontT = new System.Drawing.Font("华文新魏", 16, FontStyle.Regular);
    Point pointStart = new Point(0, 0); //绘制边框与显示字体
    Size sizeWindows = new Size(this.Width - 8, this.Height - 34); //创建Size对象
    Rectangle rect = new Rectangle(pointStart, sizeWindows); //创建Rectangle对象
    g.DrawRectangle(p, rect);//绘制矩形
    Brush brus = new SolidBrush(Color.Red); //创建笔刷
    g.DrawString("网站人气指数曲线分析", fontT, brus, this.Width / 2.00f - 150, 10.00f);
    //绘制网格线
    int x = this.Width / 10;
    int y = this.Height / 14;
    int z = this.Width / 10;
    int k = y * 12;
    //X
    for (int i = 0; i < 12; i++)
    {
    g.DrawLine(p, x, y * 3 - 10, x, y * 12); //绘制水平线条
    x = x + (this.Width - 34) / 14;
    }
    //X轴
    String[] n = {" 1月", " 2月", " 3月", " 4月", " 5月", " 6月", " 7月",
    " 8月", " 9月", "10月", "11月", "12月"};//绘制月份
    x = this.Width / 10 - 16;
    for (int i = 0; i < 12; i++)
    {
    g.DrawString(n[i].ToString(), fontO, Brushes.Red, x, y * 12);//设置文字内容及输出位置
    x = x + (this.Width - 34) / 14;
    }
    //Y
    for (int i = 0; i < 12; i++)
    {
    g.DrawLine(p, z, k, x + 10, k); //绘制垂直线条
    k = k - (y * 12) / 16;
    }
    //Y轴
    int h = k;
    String[] m = {"5565","5650","4565", "4000", "3565", "3000", "2565", "2000", "1565", "1000",
    " 565"}; //绘制Y轴显示的文字
    k = y * 12;
    for (int i = 0; i < 11; i++)
    {
    g.DrawString(m[10 - i].ToString(), fontO, Brushes.Red, z - 35, k - y); //开始绘制文字
    k = k - (y * 12) / 16;
    }
    int[] Count = new int[12];
    Pen mypen = new Pen(Color.Red, 2); //创建画笔
    Point[] points = new Point[12];
    x = this.Width / 10;
    k = y * 12;
    SqlConnection Con = new SqlConnection("Server=mrwxk\\\wangxiaoke;DataBase=db_TomeOne;Uid=sa;Pwd=");
    string cmdtxt2 = "SELECT * FROM tb_reticulation"; //声明SQL语句
    SqlCommand Com1 = new SqlCommand(cmdtxt2, Con); //创建SqlCommand对象
    SqlDataAdapter da = new SqlDataAdapter();//创建SqlDataAdapter对象
    da.SelectCommand = Com1;
    DataSet ds = new DataSet();//创建DataSet对象
    da.Fill(ds); //Fill方法填充DataSet对象
    int j = 0;
    for (j = 0; j < 12; j++)
    {
    //与Y轴数生成有关(y * 12)/16因为起始为565
    Count[j] = Convert.ToInt32(ds.Tables[0].Rows[0][j + 2].ToString()) * (y * 12) / 16 / 565;
    }
    //设置绘制曲线的坐标数组
    points[0].X = x; points[0].Y = k - Count[0];
    x = x + (this.Width - 34) / 14;
    points[1].X = x; points[1].Y = k - Count[1];
    x = x + (this.Width - 34) / 14;
    points[2].X = x; points[2].Y = k - Count[2];
    x = x + (this.Width - 34) / 14;
    points[3].X = x; points[3].Y = k - Count[3];
    x = x + (this.Width - 34) / 14;
    points[4].X = x; points[4].Y = k - Count[4];
    x = x + (this.Width - 34) / 14;
    points[5].X = x; points[5].Y = k - Count[5];
    x = x + (this.Width - 34) / 14;
    points[6].X = x; points[6].Y = k - Count[6];
    x = x + (this.Width - 34) / 14;
    points[7].X = x; points[7].Y = k - Count[7];
    x = x + (this.Width - 34) / 14;
    points[8].X = x; points[8].Y = k - Count[8];
    x = x + (this.Width - 34) / 14;
    points[9].X = x; points[9].Y = k - Count[9];
    x = x + (this.Width - 34) / 14;
    points[10].X = x; points[10].Y = k - Count[10];
    x = x + (this.Width - 34) / 14;
    points[11].X = x; points[11].Y = k - Count[11];
    g.DrawLines(mypen, points);//绘制折线
    }

    秘笈心法

    心法领悟011:使用DataSet对象作为数据集。
    DataSet对象是ADO.NET的核心成员,它是支持ADO.NET断开式、分布式数据方案的核心对象,也是实现基于非连接的数据查询的核心组件。DataSet对象是创建在内存中的集合对象,它可以包含任意数量的数据表以及所有表的约束、索引和关系等,它实质上相当于在内存中的一个小型关系数据库。

    觉得文章有用?请我喝杯咖啡~