0%

csharp 利用多饼型图分析企业人力资源情况

  • 来源:明日科技
  • 利用多饼型图分析企业人力资源情况

    实例说明

    开发企业人力资源管理系统时,经常使用饼型图分析企业中各个阶层的人员情况,为了让用户更清晰地认识,本实例采用了多饼图表示法实现分析功能。实例运行效果如图1所示。
    图1 利用多饼型图分析企业人力资源情况

    关键技术

    本实例在实现时,首先通过SQL语句分别统计企业总人数和各个阶层人数,然后通过统计的比例绘制饼型图,实现过程中主要用到Graphics类的DrawLine方法、DrawString方法和FillPie方法。

    设计过程

    (1)打开Visual Studio 2008开发环境,新建一个Windows窗体应用程序,命名为MultiCAnalyseHR。
    (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
    private void ShowPic(int Sum)
    {
    using (cmd = new SqlCommand("select t_Point,sum(t_Num) from tb_manpower group by t_Point order by sum(t_Num) desc", con))
    {
    Bitmap bmp = new Bitmap(this.panel1.Width, this.panel1.Height); //创建画布
    Graphics g = Graphics.FromImage(bmp); //创建Graphics对象
    cmd.Connection.Open();
    SqlDataReader dr = cmd.ExecuteReader(); //创建SqlDataReader对象
    while (dr.Read())//开始读取记录
    {
    float f = Convert.ToSingle(dr[1]) / Sum;
    string str = dr[0].ToString();
    drowPic(g, f, str); //调用drowPic方法绘制饼图
    }
    //绘制线条
    g.DrawLine(new Pen(Color.Black), 0, this.panel1.Height / 2, this.panel1.Width, this.panel1.Height / 2);
    g.DrawLine(new Pen(Color.Black), this.panel1.Width / 2, 0, this.panel1.Width / 2, this.panel1.Height);
    this.panel1.BackgroundImage = bmp;//显示绘制的图形
    dr.Close(); //关闭SqlDataReader对象
    con.Close();//关闭数据库连接
    }
    }
    private void drowPic(Graphics g, float f, string str) //根据要求绘制饼图
    {
    if (ConutNum == 0)//如果ConutNum为0时执行
    {
    //绘制扇形
    g.FillPie(new SolidBrush(Color.Black), 0, 0, (this.panel1.Width) / 2, (this.panel1.Height - 10) / 2, 0, 360 * f);
    //绘制文本
    g.DrawString(str, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Black), (this.panel1.Width) / 2 - 70, 10);
    g.DrawString(Convert.ToString(f * 100).Substring(0, 5) + "%", new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Black), (this.panel1.Width) / 2 - 70, 25); //绘制文本
    floatNum = 360 * f; //计算角度
    ConutNum += 1;//使ConutNum为1
    }
    else if (Conu tNum == 1) //如果ConutNum为1时执行
    {
    //绘制扇形
    g.FillPie(new SolidBrush(Color.DarkOrange), (this.panel1.Width) / 2, 0, (this.panel1.Width) / 2, (this.panel1.Height - 10) / 2, floatNum, 360 * f);
    //绘制文本
    g.DrawString(str, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.DarkOrange), (this.panel1.Width) / 2 + 10, 10);
    g.DrawString(Convert.ToString(f * 100).Substring(0, 5) + "%", new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.DarkOrange), (this.panel1.Width) / 2 + 10, 25); //绘制文本
    floatNum += 360 * f; //计算角度
    ConutNum += 1;// ConutNum为2
    }
    else if (ConutNum == 2) // ConutNum为2时执行
    {
    g.FillPie(new SolidBrush(Color.Red), 0, (this.panel1.Height - 10) / 2 + 10, (this.panel1.Width) / 2, (this.panel1.Height - 10) / 2, floatNum, 360 * f);//绘制扇形
    //绘制文本
    g.DrawString(str, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Red), 10, (this.panel1.Height - 10) / 2 + 20);
    g.DrawString(Convert.ToString(f * 100).Substring(0, 5) + "%", new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Red), 10, (this.panel1.Height - 10) / 2 + 35); //绘制文本
    floatNum += 360 * f; //计算角度
    ConutNum += 1;// ConutNum为3
    }
    else if (ConutNum == 3) // ConutNum为3时执行
    {
    g.FillPie(new SolidBrush(Color.Blue), (this.panel1.Width) / 2 - 10, (this.panel1.Height - 10) / 2 + 10, (this.panel1.Width) / 2, (this.panel1.Height - 10) / 2, floatNum, 360 * f); //绘制扇形
    g.DrawString(str, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Blue), (this.panel1.Width) / 2 + 10, (this.panel1.Height - 10) / 2 + 20);//绘制文本
    g.DrawString(Convert.ToString(f * 100).Substring(0, 5) + "%", new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Blue), (this.panel1.Width) / 2 + 10, (this.panel1.Height - 10) / 2 + 35); //绘制文本
    }
    }

    秘笈心法

    心法领悟014:绘制多饼型图的实现原理!
    绘制多饼型图时,首先需要计算各个部分所占的比例,然后依次绘制各个部分在饼型图中所占的比例,在绘制过程中,从第一部分首先开始绘制,绘制结束后,记录其绘制的扇形结束位置,并以该位置为起点,开始绘制第二部分,依次类推,直到绘制完最后一部分,这样就实现了绘制多饼型图的功能。

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