Results 1 to 15 of 15

Thread: [RESOLVED] [2005] Bar Graphs

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Resolved [RESOLVED] [2005] Bar Graphs

    My teacher gradebook program keeps up with student grades for for quarters of the school year. I'd like to have a tab in the program that will display a bar graph/histogram of the grades for the whole class - for each quarter.

    Something like this.

    A********* 38%
    B***** 21%
    C****** 25%
    D** 8%
    F** 8%

    But it might look nicer if there's a control that can do this sort of thing. Kind of like a progress bar. Basically, I want to get a quick picture of how the student averages look.

    I'm thinking of just drawing colored boxes for the bars. Or maybe use a progress bar. Or possibly even just text characters like above.

    The school year has 4 quarters so I'd like to make this fit neatly on one tabPage. I might also like to have this function for individual grades.

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Bar Graphs

    I don’t know any control that can be used to show a graph but there could be one. What do you want the graph to look like? A circle divided with different colors? Vertical or horizontal bars?

    If you want the graph to be bars then you can easily draw them on the form or use label control or any other with different colors.

    If you want the graph to be a circle shape then here is an example of it:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim A As Single = 0.38F
    4.     Dim B As Single = 0.21F
    5.     Dim C As Single = 0.25F
    6.     Dim D As Single = 0.08F
    7.     Dim F As Single = 0.08F
    8.  
    9.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    10.         Dim startAngle, sweepAngle As Single
    11.         'Draw the A
    12.         startAngle = 270.0F
    13.         sweepAngle = 360.0F * A
    14.         e.Graphics.FillPie(Brushes.Green, New Rectangle(40, 40, 100, 100), startAngle, sweepAngle)
    15.         'Draw the B
    16.         startAngle += sweepAngle
    17.         sweepAngle = 360.0F * B
    18.         e.Graphics.FillPie(Brushes.Blue, New Rectangle(40, 40, 100, 100), startAngle, sweepAngle)
    19.         'Draw the C
    20.         startAngle += sweepAngle
    21.         sweepAngle = 360.0F * C
    22.         e.Graphics.FillPie(Brushes.Yellow, New Rectangle(40, 40, 100, 100), startAngle, sweepAngle)
    23.         'Draw the D
    24.         startAngle += sweepAngle
    25.         sweepAngle = 360.0F * D
    26.         e.Graphics.FillPie(Brushes.Violet, New Rectangle(40, 40, 100, 100), startAngle, sweepAngle)
    27.         'Draw the F
    28.         startAngle += sweepAngle
    29.         sweepAngle = 360.0F * F
    30.         e.Graphics.FillPie(Brushes.Red, New Rectangle(40, 40, 100, 100), startAngle, sweepAngle)
    31.     End Sub
    32. End Class

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] Bar Graphs

    Assuming I go with circle graphs, how hard would it be to also print them on paper? I know that bar graphs made from text would be easy to print. Not sure about graphics stuff.

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Bar Graphs

    Quote Originally Posted by DroopyPawn
    Assuming I go with circle graphs, how hard would it be to also print them on paper? I know that bar graphs made from text would be easy to print. Not sure about graphics stuff.
    It won’t be difficult. All you need is to get the bitmap of the graph so that you can print it or do what ever with it. Here, I updated the code so that it draws the graph in a bitmap. Since you have the bitmap you can print it on the paper.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim A As Single = 0.38F
    4.     Dim B As Single = 0.21F
    5.     Dim C As Single = 0.25F
    6.     Dim D As Single = 0.08F
    7.     Dim F As Single = 0.08F
    8.     Dim myBitmap As Bitmap
    9.  
    10.     Private Sub DrawGraphButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawGraphButton.Click
    11.  
    12.         Dim bmp As New Bitmap(200, 200)
    13.         Dim g As Graphics = Graphics.FromImage(bmp)
    14.         Dim startAngle, sweepAngle As Single
    15.         'Draw the A        
    16.         startAngle = 270.0F
    17.         sweepAngle = 360.0F * A
    18.         g.FillPie(Brushes.Green, New Rectangle(0, 0, bmp.Width, bmp.Height), startAngle, sweepAngle)
    19.         'Draw the B        
    20.         startAngle += sweepAngle
    21.         sweepAngle = 360.0F * B
    22.         g.FillPie(Brushes.Blue, New Rectangle(0, 0, bmp.Width, bmp.Height), startAngle, sweepAngle)
    23.         'Draw the C        
    24.         startAngle += sweepAngle
    25.         sweepAngle = 360.0F * C
    26.         g.FillPie(Brushes.Yellow, New Rectangle(0, 0, bmp.Width, bmp.Height), startAngle, sweepAngle)
    27.         'Draw the D        
    28.         startAngle += sweepAngle
    29.         sweepAngle = 360.0F * D
    30.         g.FillPie(Brushes.Violet, New Rectangle(0, 0, bmp.Width, bmp.Height), startAngle, sweepAngle)
    31.         'Draw the F        
    32.         startAngle += sweepAngle
    33.         sweepAngle = 360.0F * F
    34.         g.FillPie(Brushes.Red, New Rectangle(0, 0, bmp.Width, bmp.Height), startAngle, sweepAngle)
    35.         g.Dispose()
    36.         myBitmap = bmp
    37.     End Sub
    38. End Class

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] Bar Graphs

    I wonder how hard it would be to create a custom control that would build a bar graph with 5 bars. I could set it's "StudentCount" property to tell it how many students. Then set it's ACount property to tell how many As. BCount for number of students with B,s and so on. The control would then figure the percents and draw the bars in it's paint method.

    Of course, I have no idea how to do all that.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] Bar Graphs

    Woohoo! I created a control to do what I want. I used a set of PictureBox controls with the BackgroundColor properties set to a blue color. All I have to send the control is a value for the number of grades (ACount, BCount, CCount, DCount, FCount) and it figures the percents for me then makes the PictureBoxes (the bars) the correct length.

    Now I'll think about making it scalable based on how big I draw the control on my form.

    One issue I haven't figured out yet... I want the StudentCount property to be ReadOnly but I had to set it in code inside the control. Therefore, I had to make it readable and writable to work. Any tips on that one?
    Attached Images Attached Images  

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] Bar Graphs

    Any ideas on what might be the easiest way to scale the bars horizontally so that the longest one alway gets really close to the right end of the control?

    For example, in the attached picture, I entered the same data as in the picture above but I multiplied the length of each bar by 3. Multiplying by 3 will not work in every case but in this case it does show what I'm after. My scaling code would need to work for any set of numbers.
    Attached Images Attached Images  

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Bar Graphs

    You can set a constant length for all the picture boxes, let say Width = 100. Then you calculate the persentige of each grade by deviding the total students count by grade count. For example if you have 10 students and three of them got "A" then the percentige for "A" would be 3/10 = 0.3 which is the percentige of the "A" grade. Then you multiply the constant Width = 100 by 0.3 wich = 30 and assing it to the "Width of the pictureBox. In this way the pictureBox Width will never be more than 100.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] Bar Graphs

    I got this...
    vb Code:
    1. pBiggestPercent = pAPercent
    2.         If pBPercent > pBiggestPercent Then pBiggestPercent = pBPercent
    3.         If pCPercent > pBiggestPercent Then pBiggestPercent = pCPercent
    4.         If pDPercent > pBiggestPercent Then pBiggestPercent = pDPercent
    5.         If pFPercent > pBiggestPercent Then pBiggestPercent = pFPercent
    6. If pBiggestPercent > 0 Then pMultiplier = 100 / pBiggestPercent Else pMultiplier = 0
    to work with this...

    Private Sub lblPercent_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAPercent.TextChanged, lblBPercent.TextChanged, lblCPercent.TextChanged, lblDPercent.TextChanged, lblFPercent.TextChanged
    Dim lblPercent As Label = DirectCast(sender, Label)
    Dim w As Integer = Val(lblPercent.Text)

    vb Code:
    1. Select Case lblPercent.Name
    2.             Case "lblAPercent"
    3.                 Me.pbA.Width = w * pMultiplier
    4.             Case "lblBPercent"
    5.                 Me.pbB.Width = w * pMultiplier
    6.             Case "lblCPercent"
    7.                 Me.pbC.Width = w * pMultiplier
    8.             Case "lblDPercent"
    9.                 Me.pbD.Width = w * pMultiplier
    10.             Case "lblFPercent"
    11.                 Me.pbF.Width = w * pMultiplier
    12.         End Select
    13.  
    14.         UpdateStudentCount()
    15.     End Sub

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Bar Graphs

    I am not sure what are you tring to achive with this code. I don't know what the pMultiplier contains and what is it for. Why you need to get the bigist percentige? Read my previuse post again. This task is very simple staff.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] Bar Graphs

    I wanted the bars to be scalable. If I make the Control.Width = 1000, then the bars are still only 100 or less in width. By finding the BiggestPercent, I can stretch that one all the way across the control and scale the others in a similar way.

    Anyhow, I got it working that way. So I can have short bars or really long ones depending on the size of the control itself.

  12. #12
    New Member
    Join Date
    Sep 2007
    Posts
    10

    Re: [RESOLVED] [2005] Bar Graphs

    please could some one tell me is there any way how to display bargraphs in a datagrid or list view

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [RESOLVED] [2005] Bar Graphs

    Make several bitmaps of bars (different lengths) and set the cell's picture property to whichever bar you need maybe?

  14. #14
    New Member
    Join Date
    Sep 2007
    Posts
    10

    Re: [RESOLVED] [2005] Bar Graphs

    actually i wanted the same thing what you have done in a datagrid which displays the datas and their graphs in their repective cells

    is there a method to do this ???

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [RESOLVED] [2005] Bar Graphs

    There's always a way to do it. But I wouldn't know what to do on this one.

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