All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
GDI+绘制饼图分析男女比例

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:29:17 Browse times: 353 Comment times: 0

GDI+绘制饼图分析男女比例


Head photo

Go homepage
Upload pictures
Write articles

GDI+绘制饼图分析男女比例

绘制饼图分析男女比例
饼图是一种常用来显示数据比例的图表技术,常用来突出某一重要数据。本节将通过一个实例来介绍使用饼图显示公司员工的男女比例情况。实例运行结果如图1所示。
程序开发步骤如下所示。
(1)新建一个Windows应用程序,命名为26_02,其主窗体默认为Form1.cs。
(2)Form1.cs窗体中,添加一个Button控件,用来执行画图操作。
(3)程序主要代码如下。
单击【分析男女比例】按钮,程序调用Graphics对象的FillPie方法,根据数据库中记录,绘制一张体现公司员工男女比例的饼图。【分析男女比例】按钮的Click事件代码如下:
private void button1_Click(object sender, EventArgs e)
{
//连数据库
SqlConnection sqlcon = new SqlConnection("Data Source=(local);Database=db_26;Uid=sa;Pwd=");
string P_str_sel = "select * from tb_02";
SqlDataAdapter myda1 = new SqlDataAdapter(P_str_sel, sqlcon);
DataSet myds1 = new DataSet();
myda1.Fill(myds1);
//计算男、女总和
string P_str_sumNum = "select sum(boyNum+girlNum) as sumNum FROM tb_02";
SqlDataAdapter myda2 = new SqlDataAdapter(P_str_sumNum, sqlcon);
DataSet myds2 = new DataSet();
myda2.Fill(myds2);
int P_int_sum = Convert.ToInt32(myds2.Tables[0].Rows[0][0].ToString());
//获取男员工数目
int P_int_boy = Convert.ToInt32(myds1.Tables[0].Rows[0][0].ToString());
//获取女员工数目
int P_int_girl = Convert.ToInt32(myds1.Tables[0].Rows[0][1].ToString());
Graphics graphics = this.CreateGraphics();
try
{
graphics.Clear(Color.White);
Pen pen1 = new Pen(Color.Red);
Brush brush1 = new SolidBrush(Color.YellowGreen);
Brush brush2 = new SolidBrush(Color.Blue);
Brush brush3 = new SolidBrush(Color.Brown);
Font font1 = new Font("Courier New", 16, FontStyle.Bold);
Font font2 = new Font("Courier New", 8);
graphics.FillRectangle(brush1, 0, 0, 370, 350); //绘制背景
graphics.DrawString("公司员工男女比例分析", font1, brush2, new Point(60, 20)); //书写标题
//男员工在圆中分配的角度
float angle1 = Convert.ToSingle((360 * (Convert.ToSingle(P_int_boy) / Convert.ToSingle(P_int_sum))));
//女员工在圆中分配的角度
float angle2 = Convert.ToSingle((360 * (Convert.ToSingle(P_int_girl) / Convert.ToSingle(P_int_sum))));
graphics.FillPie(brush2, 100, 60, 180, 180, 0, angle1); //绘制男员工所占比例
graphics.FillPie(brush3, 100, 60, 180, 180, angle1, angle2); //绘制女员工所占比例
//绘制标识
graphics.DrawRectangle(pen1, 50, 255, 260, 50); //绘制范围框
graphics.FillRectangle(brush2, 85, 265, 20, 10); //绘制小矩形
graphics.DrawString("男员工占公司总人数比例:" + Convert.ToInt32(P_int_boy) * 100 / Convert.ToInt32 (P_int_sum) + "%", font2, brush2, 120, 265);
graphics.FillRectangle(brush3, 85, 285, 20, 10);
graphics.DrawString("女员工占公司总人数比例:" + Convert.ToInt32(P_int_girl) * 100 / Convert.ToInt32 (P_int_sum) + "%", font2, brush3, 120, 285);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
graphics.Dispose();
}
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Data.SqlClient;
namespace _6_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//连数据库
SqlConnection sqlcon = new SqlConnection("Data Source=(local);Database=db_26;Uid=sa;Pwd=");
string P_str_sel = "select * from tb_02";
SqlDataAdapter myda1 = new SqlDataAdapter(P_str_sel, sqlcon);
DataSet myds1 = new DataSet();
myda1.Fill(myds1);
//计算男、女总和
string P_str_sumNum = "select sum(boyNum+girlNum) as sumNum FROM tb_02";
SqlDataAdapter myda2 = new SqlDataAdapter(P_str_sumNum, sqlcon);
DataSet myds2 = new DataSet();
myda2.Fill(myds2);
int P_int_sum = Convert.ToInt32(myds2.Tables[0].Rows[0][0].ToString());
//获取男员工数目
int P_int_boy = Convert.ToInt32(myds1.Tables[0].Rows[0][0].ToString());
//获取女员工数目





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.