在DataGridView控件中一次显示出多个圆饼图的原始程序代码

之前我们曾经提到过,您可以在Windows Form窗体上的一个DataGridView控件中一次显示出多个圆饼图(或其它形式的统计图表)以便能够一次检视多个群组数据的比例情况(如图表1所示)。当时并未列出原始程序代码,有读者希望能够提供原始程序代码,所以我们在此将其完整列出,与大家共享:(注:本程序范例将附于简体版的Visual C# 2005文件IO与资料存取秘诀」一书)

 

Option Strict On
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Imaging
Public Class Form1

  ' 宣告各个私用变量。
  Private dirInfo As DirectoryInfo
  ' 此变量用来持有男女平均薪资的总和。
  Private TotalSalary As Long
  ' 此变量用来持有女性平均薪资。
  Private FemaleSalary As Long
  ' 此变量用来持有男性平均薪资。
  Private MaleSalary As Long
  ' 此变量用来持有女性空间之扇形区域之第二个边的角度。
  Private sweep As Single

  Private Sub Form1_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load

    Dim myPieChartTable As New DataTable
    myPieChartTable.Columns.Add("部门", GetType(System.String))
    myPieChartTable.Columns.Add("图形", GetType(System.Byte()))

    ' 利用 SqlConnectionStringBuilder 对象来构建连接字符串。
    Dim connectStringBuilder As New SqlConnectionStringBuilder()
    connectStringBuilder.DataSource = "(local)\SQLEXPRESS"
    connectStringBuilder.InitialCatalog = "北风贸易"
    connectStringBuilder.IntegratedSecurity = True

    Using cn1 As New SqlConnection(connectStringBuilder.ConnectionString)

      Dim cmdDepartment As New SqlCommand( _
        "SELECT DISTINCT 部门FROM 章立民研究室", cn1)

      cn1.Open()

      Using dr1 As SqlDataReader = cmdDepartment.ExecuteReader

        While dr1.Read

          ' 取得部门。            
          Dim strDepartment As String = dr1.GetSqlString(0).Value

          Dim myTable As New DataTable

          Using cn2 As New SqlConnection(connectStringBuilder.ConnectionString)

            Dim cmdLiming As New SqlCommand( _
              "SELECT 性别, AVG(目前薪资) AS 平均薪资 FROM 章立民研究室 " & _
              "WHERE 部门= @department GROUP BY 性别ORDER BY 性别", cn2)
            cmdLiming.Parameters.Add( _
              "@department", SqlDbType.NVarChar, 10).Value = strDepartment

            cn2.Open()

            Using dr2 As SqlDataReader = cmdLiming.ExecuteReader()
              myTable.Load(dr2, LoadOption.OverwriteChanges)
            End Using


            ' 将男女平均薪资的总和指派给变数 TotalSalary
            TotalSalary = CInt(myTable.Rows(0)(1)) + CInt(myTable.Rows(1)(1))

            ' 将女性平均薪资指派给变量 FemaleSalary
            FemaleSalary = CInt(myTable.Rows(0)(1))

            ' 将男性平均薪资指派给变量 MaleSalary
            MaleSalary = CInt(myTable.Rows(1)(1))

            ' 计算女性空间之扇形区域之第二个边的角度。
            sweep = CSng(360.0F * (FemaleSalary / TotalSalary))
          End Using

          Using objBitmap As New Bitmap(300, 330)
            Using objGraphics As Graphics = Graphics.FromImage(objBitmap)
              objGraphics.Clear(Drawing.Color.White)

              ' 宣告 Rectangle 结构变量,并指定X 坐标参数、Y 坐标参数、
              ' 宽度参数、高度参数,用来显示圆饼图的矩形大小。
              Dim rect As Rectangle = New Rectangle(40, 10, 200, 200)

              ' 宣告 Rectangle 结构变量,并指定 X 坐标参数、Y 坐标参数、
              ' 宽度参数、高度参数,用来当作圆饼图边界的矩形大小。
              Dim rect2 As Rectangle = New Rectangle(0, 0, 290, 320)

              ' 宣告 Rectangle 结构变量,并指定 X 坐标参数、Y 坐标参数、宽度参数、
              ' 高度参数,用来当作已使用空间的图例。
              Dim MaleLegend As Rectangle = New Rectangle(5, 265, 20, 20)

              ' 宣告 Rectangle 结构变量,并指定 X 坐标参数、Y 坐标参数、宽度参数、
              ' 高度参数,用来当作未使用空间的图例。
              Dim FemaleLegend As Rectangle = New Rectangle(5, 290, 20, 20)

              ' 在画面上显示出矩形,并指定颜色参数与要显示的 Rectangle 参数。
              objGraphics.DrawRectangle(Pens.Black, rect2)

              ' 绘制 "男女平均薪资的总和:" 下方的水平线。
              objGraphics.DrawLine(Pens.Black, 0, 250, 290, 250)

              ' 在画面上显示圆饼图,并指定颜色参数、Rectangle 参数、圆饼图的
              ' 起点参数、以及圆饼图要涵盖的范围参数。
              objGraphics.FillPie(Brushes.Magenta, rect, 0, sweep)
              objGraphics.FillPie(Brushes.Blue, rect, sweep, 360 - sweep)

              ' 在画面上显示女性及男性图例。
              objGraphics.FillRectangle(Brushes.Magenta, FemaleLegend)
              objGraphics.FillRectangle(Brushes.Blue, MaleLegend)

              ' 在画面上显示文字内容,并指定显示内容参数、字型参数、
              ' 笔刷参数、以及位置参数。
              objGraphics.DrawString("男女平均薪资的总和:", _
                New Font("Tahoma", 10, FontStyle.Regular), _
                Brushes.Black, New PointF(0, 220))
              objGraphics.DrawString("男性平均薪资:", _
                New Font("Tahoma", 10, FontStyle.Regular), _
                Brushes.Black, New PointF(25, 265))
              objGraphics.DrawString("女性平均薪资:", _
                New Font("Tahoma", 10, FontStyle.Regular), _
                Brushes.Black, New PointF(25, 290))

              objGraphics.DrawString(TotalSalary.ToString("N") + " ", _
                New Font("Tahoma", 10, FontStyle.Regular), _
                Brushes.Black, New PointF(180, 220))
              objGraphics.DrawString(MaleSalary.ToString("N") + " ", _
                New Font("Tahoma", 10, FontStyle.Regular), _
                Brushes.Black, New PointF(180, 265))
              objGraphics.DrawString(FemaleSalary.ToString("N") + " ", _
                New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, _
                New PointF(180, 290))

              objGraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

              Dim newRow As DataRow
              newRow = myPieChartTable.NewRow()
              myPieChartTable.NewRow()
              newRow("部门") = strDepartment

              Using ms As MemoryStream = New MemoryStream()
                  objBitmap.Save(ms, ImageFormat.Gif)
                  ms.Flush()
                  newRow("图形") = ms.GetBuffer()
              End Using

              myPieChartTable.Rows.Add(newRow)

            End Using
          End Using
        End While
      End Using
    End Using

    Me.DataGridView1.DataSource = myPieChartTable
  End Sub
End Class


图表1

posted on 2006-12-01 10:40  章立民研究室  阅读(3706)  评论(1编辑  收藏  举报

导航