' Program:      Chapter 13 Pie Chart Hands On

' Programmer:   Bradley Shedd

' Date:         September 09, 2006

' Description:  Draw a chart for relative sales amounts.

' Folder:       Chapter 13 Hands On

 

Public Class pieChartForm

 

    Private totalSalesDecimal, bookSalesDecimal, periodicalDecimal, _

      foodSalesDecimal, periodicalSalesDecimal As Decimal

    Private drawChartBoolean = False

 

    Private Sub displayChartButton_Click(ByVal sender As System.Object, _

      ByVal e As System.EventArgs) Handles displayChartButton.Click

        ' Display a pie chart showing relative sales by a department.

        ' Need total sales amount.

        With Me

            Try

                bookSalesDecimal = Decimal.Parse(.booksTextBox.Text)

                Try

                    periodicalSalesDecimal = Decimal.Parse(.periodicalsTextBox.Text)

                    Try

                        foodSalesDecimal = Decimal.Parse(.foodsTextBox.Text)

                        totalSalesDecimal = bookSalesDecimal + periodicalSalesDecimal _

                         + foodSalesDecimal

                        drawChartBoolean = True

                    Catch

                        MessageBox.Show("Invalid Food Sales")

                        .foodsTextBox.Focus()

                    End Try

                Catch

                    MessageBox.Show("Invalid Periodical Sales")

                    .periodicalsTextBox.Focus()

                End Try

            Catch

                MessageBox.Show("Invalid Book Sales")

                .booksTextBox.Focus()

            End Try

 

            ' Force a Paint of the form.

            .Refresh()

        End With

    End Sub

 

    Private Sub exitButton_Click(ByVal sender As System.Object, _

       ByVal e As System.EventArgs) Handles exitButton.Click

        ' End the project.

 

        Me.Close()

    End Sub

 

    Private Sub clearButton_Click(ByVal sender As System.Object, _

       ByVal e As System.EventArgs) Handles clearButton.Click

        ' Clear the screen controls.

 

        With Me

            .foodsTextBox.Clear()

            .periodicalsTextBox.Clear()

            With .booksTextBox

                .Clear()

                .Focus()

            End With

            .legendLabel.Visible = False

            drawChartBoolean = False

            .Refresh()

        End With

    End Sub

 

    Private Sub pieChartForm_Paint(ByVal sender As Object, _

     ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Dim xCenterInteger As Integer = 140

        Dim yCenterInteger As Integer = 180

        Dim clearBrush As New SolidBrush(pieChartForm.DefaultBackColor)

        With Me

            If drawChartBoolean Then

                ' Create the pie chart.

                ' Amounts are a portion of the total circle of 360 degrees.

                ' The pie graphic includes a start angle and end angle.

                If totalSalesDecimal <> 0 Then

                    .legendLabel.Visible = True

                    ' Find the end of the book portion of 360 degrees.

                    Dim endBooksInteger As Integer = Convert.ToInt32(bookSalesDecimal / totalSalesDecimal * 360)

                    .CreateGraphics.FillPie(Brushes.Blue, xCenterInteger, yCenterInteger, _

                      100, 100, 0, endBooksInteger)

                    ' Find the end of the Periodicals portion.

                    Dim endPeriodicalsInteger As Integer = _

                        Convert.ToInt32(periodicalSalesDecimal / totalSalesDecimal * 360)

                    .CreateGraphics.FillPie(Brushes.Yellow, xCenterInteger, yCenterInteger, 100, 100, endBooksInteger, endPeriodicalsInteger)

                    Dim endFoodInteger As Integer = _

                        Convert.ToInt32(foodSalesDecimal / totalSalesDecimal * 360)

                    .CreateGraphics.FillPie(Brushes.Red, xCenterInteger, yCenterInteger, 100, 100, endPeriodicalsInteger + endBooksInteger, endFoodInteger)

                End If

            Else

                .CreateGraphics.FillEllipse(clearBrush, xCenterInteger, yCenterInteger, 100, 100)

            End If

        End With

    End Sub

End Class

 

 

 

Homepage