VB 2005 - 於 DataGridView 控制項一次顯示出多個圓餅圖的原始程式碼

摘要:VB 2005 - 於 DataGridView 控制項一次顯示出多個圓餅圖的原始程式碼

之前我們曾經提到過,您可以在 Windows Form 表單上的一個 DataGridView 控制項中一次顯示出多個圓餅圖(或其他形式的統計圖表)以便能夠一次檢視多個群組數據的比例情況(如圖表1所示)。當時並未列出原始程式碼,有讀者希望能夠提供原始程式碼,所以我們在此將其完整列出,與大家共享 

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