Results 1 to 11 of 11

Thread: [RESOLVED] How do you make a pie chart

  1. #1

    Thread Starter
    Member Aest's Avatar
    Join Date
    Aug 2010
    Posts
    51

    Resolved [RESOLVED] How do you make a pie chart

    im trying to figure out how to make a pie chart to visualize statistics in my program. however ive never done anything in the graphics department for VB. i cant find to much on google on the subject and what i can i dont get or it doesnt work. can anybody show me a simplistic way to draw a pie chart please?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do you make a pie chart

    Using simple VB methods, a crude pie chart can be created rather easily. The example below shows that.

    Some notes
    1) VB draws arcs counter-clockwise.
    2) VB uses radians not angles, therefore, a conversion is needed
    3) In the example below, I used 10 percentages which total to 100%, 10 pies. If you will be using the pie chart for similar purposes, you must ensure the total is never > 100%

    Add a command button and picturebox. Make the picturebox the size of the circle/pie you desire
    Then paste this code. I commented the code a bunch. Hopefully you can follow it easily
    Code:
    Private Sub Command1_Click()
    
        Dim midPt As Long, endPt As Double
        Dim angle As Double, startPt As Double
        Dim totalPct As Double, Radius As Long
        Dim x As Long
        
        Dim RadianRatio As Double
        Dim lGrayScaleColor As Long
        
        Dim samplePcts(9) As Double
        samplePcts(0) = 5: samplePcts(1) = 15
        samplePcts(2) = 20: samplePcts(3) = 15
        samplePcts(4) = 5: samplePcts(5) = 9
        samplePcts(6) = 10: samplePcts(7) = 12
        samplePcts(8) = 3: samplePcts(9) = 6
        
        ' to color the pies
        Picture1.FillStyle = vbSolid
        
        
        ' VB's arc draws counter-clockwise from W > N > E > S > W
        '           90
        '
        '   180             0/360
        '
        '           270
        
        ' VB uses radians not degrees. Following multiplied against angle = radians
        RadianRatio = (Atn(1) * 4) / 180
        
        ' set midpoint where circle will be centered around
        If Picture1.ScaleWidth > Picture1.ScaleHeight Then
            midPt = Picture1.ScaleHeight \ 2
        Else
            midPt = Picture1.ScaleWidth \ 2
        End If
        
        ' radius of the circle/arc
        Radius = midPt
        
        ' start on the 0 degree mark. Note we want -values and you can't get -0 but can get -360
        startPt = 360 * RadianRatio
        
        For x = 0 To 9 ' process the percentages which total to 100
        
            totalPct = totalPct + samplePcts(x)
            angle = (360 * totalPct) / 100
            
            endPt = angle * RadianRatio
        
            ' set the pie color (grayscale)
            lGrayScaleColor = x * 20
            Picture1.FillColor = RGB(lGrayScaleColor, lGrayScaleColor, lGrayScaleColor)
    
            ' vbWhite below is the outline color
            ' negative startpt & endpt values closes the arc vs opened arc
            Picture1.Circle (midPt, midPt), Radius, vbWhite, -startPt, -endPt
            
            startPt = endPt ' set next pie start pos to last pie's end pos
        
        Next
        
    End Sub
    Last edited by LaVolpe; Sep 2nd, 2010 at 09:20 PM. Reason: added more remarks
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member Aest's Avatar
    Join Date
    Aug 2010
    Posts
    51

    Re: How do you make a pie chart

    thank you for your response. your code doesnt seem to work on my version of VB (visual basic 2008 express). i altered the code to draw on the form itself. however its buggy and i cant figure out why. it skips all but 2 of the slices. i imagine i buggered something up rewriting your code. here is my code and an attached pic of the pie

    Code:
    Private Sub Form3_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            Dim dblIncome, dblMonth, dblTransit, dblSelf, dblDaily, dblSum As Double
    
            dblMonth = CDbl(Form1.lblTotalMonth.Text)
            dblTransit = CDbl(Form1.lblTotalCar.Text)
            dblSelf = CDbl(Form1.lblTotalSelf.Text)
            dblDaily = CDbl(Form1.lblTotalOther.Text)
            dblIncome = CDbl(Form1.lblTotal.Text)
    
            If dblIncome < 0 Then
                dblIncome = 0
            End If
    
            dblSum = dblMonth + dblTransit + dblSelf + dblDaily + dblIncome
    
            dblIncome = (dblIncome / dblSum)
            dblMonth = (dblMonth / dblSum)
            dblTransit = (dblTransit / dblSum)
            dblSelf = (dblSelf / dblSum)
            dblDaily = (dblDaily / dblSum)
    
            Dim startPt, endPt, angle, totalPct As Double
            Dim intx, Radius As Integer
            Dim RadianRatio As Double
    
            Dim Percents(4) As Double
            Percents(0) = dblIncome
            Percents(1) = dblMonth
            Percents(2) = dblTransit
            Percents(3) = dblSelf
            Percents(4) = dblDaily
    
            Dim Colors(4) As Color
            Colors(0) = Color.Green
            Colors(1) = Color.Blue
            Colors(2) = Color.Cyan
            Colors(3) = Color.Gray
            Colors(4) = Color.Aqua
    
            RadianRatio = (3.14159265 / 180)
            startPt = 0
            Radius = 200
    
            For intx = 0 To 4
    
                Dim BrushColor As New Drawing.SolidBrush(Colors(intx))
    
                totalPct = totalPct + Percents(intx)
                angle = (360 * totalPct)
    
                endPt = angle * RadianRatio
    
                e.Graphics.FillPie(BrushColor, 185, 40, Radius, Radius, CInt(-startPt), CInt(-endPt))
    
                startPt = endPt
    
            Next
    
            lblIncomeStat.Text = FormatPercent(dblIncome)
            lblMonthlyStat.Text = FormatPercent(dblMonth)
            lblTransitStat.Text = FormatPercent(dblTransit)
            lblSelfStat.Text = FormatPercent(dblSelf)
            lblDailyStat.Text = FormatPercent(dblDaily)
    
    
        End Sub
    Attached Images Attached Images  

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do you make a pie chart

    Sorry, I gotta bug out, not a .Net user.
    I don't know how FillPie works. Though it looks very similar to VB6's Circle method.
    I see you are using negative values though. And if you set startPt at zero, it can't be negative (no such thing as -0). Could that be the problem? Try starting startPt out as 360*RadianRatio
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Member Aest's Avatar
    Join Date
    Aug 2010
    Posts
    51

    Re: How do you make a pie chart

    i changed startpt back to 360 * radianratio but it still has the same prob. what i dont understand is i set the first color to be green but it starts at blue. is it a problem in the design of the for loop?

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do you make a pie chart

    I can't really help that much as I'm not a .Net guy. But I don't think it is the loop necessarily. I just think the first pie isn't drawing. You may want to verify startPt and endPt. Also, are you sure CInt() should be used for the start/end points? I would think that would round/up down the decimal values? Again, I'm not familiar with the FillPie function
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Member Aest's Avatar
    Join Date
    Aug 2010
    Posts
    51

    Re: How do you make a pie chart

    i didnt initially convert it to integer but option stict was having a problem with doubles in the fillpie method

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do you make a pie chart

    Quote Originally Posted by Aest View Post
    i didnt initially convert it to integer but option stict was having a problem with doubles in the fillpie method
    Try singles, not doubles, per this MSDN link

    Edited: Also ensure .Net's FillPie requires radians, just maybe, it requires degrees? Check your help files or search forum/google for FillPie
    Last edited by LaVolpe; Sep 3rd, 2010 at 09:57 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Member Aest's Avatar
    Join Date
    Aug 2010
    Posts
    51

    Re: How do you make a pie chart

    you know what, i got so involved with the graphics that thought didnt even occur to me. im getting a full cyan circle now but at least its a full circle. ill just fiddle around with this but hopefully this is in the right direction. thanks for all your help

  10. #10

    Thread Starter
    Member Aest's Avatar
    Join Date
    Aug 2010
    Posts
    51

    Re: How do you make a pie chart

    yup i got it working now. now that i knew how to word it correctly i found out what i need on google. turns out the method is (brush, x coordinate, y coordinate, length, height, start angle, sweep angle). so the endpt was actually just 360 * percents(intx) and the startpt was startpt = startpt + angle. and of course what opened my eyes in the first place was the angle was in degrees. thanks so much for your help

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do you make a pie chart

    Glad you got it figured out. One of these days, soon maybe, I'll start to play with .Net and you'll be able to answer my questions

    Don't forget to mark this resolved, if it is now resolved. Use the Thread Tools menu near the top of your first post above.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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