Results 1 to 17 of 17

Thread: [RESOLVED] Pie Chart

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Resolved [RESOLVED] Pie Chart

    Hi, can anyone help me with creating a pie chart in vb?
    I've never done it before and have only had google and youtube to guide me on my way haha!

    I found a piece of code which kind of got me in the right direction, but I have 3 values which have already been put into text boxes:
    Code:
    CarbsTextBox.Text = dsFoodData.Tables("tempTable").Rows(0).Item(1) & "g".ToString
            ProteinTextBox.Text = dsFoodData.Tables("tempTable").Rows(0).Item(2) & "g".ToString
            FatTextBox.Text = dsFoodData.Tables("tempTable").Rows(0).Item(3) & "g".ToString
    And then the code I found for the pie chart is:
    Code:
     Public Sub DrawPieChart(ByVal percents() As Integer, ByVal colors() As Color, _
    ByVal surface As Graphics, ByVal location As Point, ByVal pieSize As Size)
            Dim sum As Integer = 0
            For Each percent As Integer In percents
                sum += percent
            Next
            Dim percentTotal As Integer = 0
            For percent As Integer = 0 To percents.Length() - 1
                surface.FillPie( _
                   New SolidBrush(colors(percent)), _
                   New Rectangle(location, pieSize), CType(percentTotal * 360 / 100, Single), _
                   CType(percents(percent) * 360 / 100, Single))
                percentTotal += percents(percent)
            Next
            Return
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PictureBox1.Hide()
            Dim ctr As Integer
            Dim percents(20) As Integer
            ctr = 0
            Dim colors() As Color = {Color.Blue, Color.Green, Color.Red}
            Dim graphics As Graphics = Me.CreateGraphics
            Dim location As Point = New Point(200, 200)
            Dim size As Size = New Size(200, 200)
            DrawPieChart(percents, colors, graphics, location, size)
        End Sub
    End Class
    Would this code be right for what I'm trying to do?
    Apologies if i'm asking for too much here but it's kind of a necessity in my program and I haven't got a scooby haha!

    Thanks in advance

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Pie Chart

    try this:

    Code:
    Public Class Form1
    
        Dim draw As Boolean = False
    
        Public Sub DrawPieChart(ByVal percents() As Integer, ByVal colors() As Color, _
    ByVal surface As Graphics, ByVal location As Point, ByVal pieSize As Size)
            Dim sum As Integer = 0
            For Each percent As Integer In percents
                sum += percent
            Next
            Dim percentTotal As Integer = 0
            For percent As Integer = 0 To percents.Length() - 1
                surface.FillPie( _
                   New SolidBrush(colors(percent)), _
                   New Rectangle(location, pieSize), CType(percentTotal * 360 / 100, Single), _
                   CType(percents(percent) * 360 / 100, Single))
                percentTotal += percents(percent)
            Next
            Return
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            draw = True
            Me.Invalidate()
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If draw Then
                Dim percents() As Integer = {25, 20, 65}
                Dim colors() As Color = {Color.Blue, Color.Green, Color.Red}
                Dim location As Point = New Point(20, 20)
                Dim size As Size = New Size(200, 200)
                DrawPieChart(percents, colors, e.Graphics, location, size)
            End If
        End Sub
    
    End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Pie Chart

    to get a value from a textbox, use CInt(Val(textbox1.text))

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Re: Pie Chart

    Ah thanks a lot guys! It WORKS! Get in!

    Any idea why it's doing this though paul?

    Name:  1.jpg
Views: 143
Size:  48.7 KB
    Name:  2.jpg
Views: 130
Size:  46.8 KB

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Re: Pie Chart

    Nevermind... Fixed it
    Thanks a lot!

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Pie Chart

    I don't know how you fixed it, but percents doesn't have to be an integer array parameter. it could be a single array

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Pie Chart

    Code:
    Public Class Form1
    
        Dim draw As Boolean = False
    
        Public Sub DrawPieChart(ByVal percents() As Single, ByVal colors() As Color, _
    ByVal surface As Graphics, ByVal location As Point, ByVal pieSize As Size)
            Dim sum As Integer = 0
            For Each percent As Integer In percents
                sum += percent
            Next
            Dim percentTotal As Single = 0
            Dim angleTotal As Double = 0
            For percent As Integer = 0 To percents.Length() - 1
                surface.FillPie( _
                   New SolidBrush(colors(percent)), _
                   New Rectangle(location, pieSize), CType(percentTotal * 360 / 100, Single), _
                   CType(percents(percent) * 360 / 100, Single))
                'Find center coordinates of the pie slice and draw the percent.   
                Dim newAngle As Double = percents(percent) * 360 / 100
                Dim centerAngle As Double = (angleTotal + newAngle / 2) / 180 * Math.PI
                Dim pieRadius As Double = pieSize.Width / 2
                Dim centerCoords As New System.Drawing.PointF
                With centerCoords
                    .X = Convert.ToSingle(location.X + pieRadius + (pieRadius / 2) * Math.Cos(centerAngle))
                    .Y = Convert.ToSingle(location.Y + pieRadius + (pieRadius / 2) * Math.Sin(centerAngle))
                End With
                Dim textSize As SizeF = surface.MeasureString("A", Me.Font)
                surface.DrawString((percents(percent) / 100).ToString("0.0%"), Me.Font, Brushes.Black, Convert.ToSingle(centerCoords.X - (textSize.Height / 2)), Convert.ToSingle(centerCoords.Y - (textSize.Height / 2)))
                percentTotal += percents(percent)
                angleTotal += newAngle
            Next
            Return
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            draw = True
            Me.Invalidate()
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If draw Then
                Dim percents() As Single = {CSng(Val(CarbsTextBox.Text)), CSng(Val(ProteinTextBox.Text)), CSng(Val(FatTextBox.Text))}
                Dim colors() As Color = {Color.Blue, Color.Green, Color.Red}
                Dim graphics As Graphics = Me.CreateGraphics
                Dim location As Point = New Point(20, 20)
                Dim size As Size = New Size(200, 200)
                DrawPieChart(percents, colors, graphics, location, size)
            End If
        End Sub
    
    End Class

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Re: [RESOLVED] Pie Chart

    Absolute legend mate! Thanks a lot Paul!
    Massive help

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Pie Chart

    2 improvements there:

    Code:
    Dim textSize As SizeF = surface.MeasureString((percents(percent) / 100).ToString("0.0%"), Me.Font)
    surface.DrawString((percents(percent) / 100).ToString("0.0%"), Me.Font, Brushes.Black, Convert.ToSingle(centerCoords.X - (textSize.Width / 2)), Convert.ToSingle(centerCoords.Y - (textSize.Height / 2)))

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Re: [RESOLVED] Pie Chart

    Yeah that works great! Thanks a lot!

    Only problem being is that the pie chart is leaving a white space and just doing (eg. 83g carbs as 83%) and leaving a white space because all of that doesn't add up to 100.

    Name:  2.jpg
Views: 150
Size:  10.1 KB

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Pie Chart

    Public Sub DrawPieChart(ByVal percents() As Single, ByVal colors() As Color, _
    ByVal surface As Graphics, ByVal location As Point, ByVal pieSize As Size)

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Pie Chart

    that's how pie charts work 1 whole pie = 100%

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Re: [RESOLVED] Pie Chart

    Edit: Ah it's because I need to get the percentage of carbs/fat/protein from total calories...
    Last edited by J95W; Jan 25th, 2014 at 06:55 PM.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Pie Chart

    Code:
    Dim a As Single = 83.8F
    Dim b As Single = 12.3F
    Dim c As Single = 1.0F
    Dim onePercent As Single = 100 / (a + b + c)
    Dim percentageA As Single = a * onePercent
    Dim percentageB As Single = b * onePercent
    Dim percentageC As Single = c * onePercent

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Re: [RESOLVED] Pie Chart

    To Get % of Protein
    Multiply the protein grams by 4. Divide the result by the total number of calories in the food. This is the percent of protein in the food.

    To Get % of Carbs
    Multiply the carbohydrate grams by 4. Divide this result by the total number of calories to get the percent of carbohydrates in the food.

    To Get % of Fat
    Multiply the grams of fat by 9 and divide the result by the total calories to get the percent of fat.

    That's how to get the percentages so will that work with the pie chart?
    I've already got the total calories:
    Code:
    calscount.Text = dsFoodData.Tables("tempTable").Rows(0).Item(0).ToString

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Pie Chart

    if all 3 add up to 100, it'll work

  17. #17

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    62

    Re: [RESOLVED] Pie Chart

    Ah naice! Cheers mate, thanks a lot for your help with this. It's been a huge help. I didn't even know where to start at first haha!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width