5,692,513 members and growing! (16,360 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

Drawing a Bar Chart

By Karthikeyan Muthurajan

This is an article about drawing a bar chart for a given set of values.
VB.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, Windows, .NET, GDI+, Visual Studio, VS.NET2002, VS.NET2003, Dev

Posted: 17 Jun 2004
Updated: 25 Jun 2004
Views: 77,829
Bookmarked: 18 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 3.90 Rating: 2.86 out of 5
8 votes, 34.8%
1
1 vote, 4.3%
2
2 votes, 8.7%
3
3 votes, 13.0%
4
9 votes, 39.1%
5

Sample Image - DrawBarChart.gif

Introduction

This article describes a procedure that will be useful to draw a bar chart in a form, with all features. Using this procedure, any number of graphs can be drawn in a form, by setting the X axis and Y axis.

Input Parameters

  • objEnum As IDictionaryEnumerator - the data from which the graph has to be drawn.
  • intItemCount As Integer - number of items to show in a graph (you can get from the DictionaryEnumerator itself).
  • strGraphTitle As String - title has to be drawn for the graph.
  • Xaxis As Integer - starting X axis.
  • Yaxis As Integer - starting Y axis.
  • MaxWidth As Int16 - maximum width of the graph (for calculation purpose).
  • MaxHt As Int16 - maximum height of the graph (for calculation purpose).
  • clearForm As Boolean - whether the Form has to clear or not.
  • Optional ByVal SpaceRequired As Boolean - whether space is required in between 2 graphs.

The graph has to be called in the Paint event of the Form and the parameters should be resized in the Resize event of the Form.

LoadColorArray procedure is used to have required colors to draw the graph. Because, if we use RGB, overall the graph will not be good visibly.

The parameter clearForm can be used when you need to show 2 graphs in the same Form; it will be useful then...

Public Sub drawBarChart(ByVal objEnum As IDictionaryEnumerator, _
          ByVal intItemCount As Integer, ByVal strGraphTitle As String, _
          ByVal Xaxis As Integer, ByVal Yaxis As Integer, _
          ByVal MaxWidth As Int16, ByVal MaxHt As Int16, _
          ByVal clearForm As Boolean, _
          Optional ByVal SpaceRequired As Boolean = False)

    Dim intGraphXaxis As Integer = Xaxis
    Dim intGraphYaxis As Integer = Yaxis
    Dim intWidthMax As Integer = MaxWidth
    Dim intHeightMax As Integer = MaxHt
    Dim intSpaceHeight As Integer
    Dim intMaxValue As Integer = 0
    Dim intCounter As Integer
    Dim intBarWidthMax
    Dim intBarHeight
    Dim strText As String
    Try
        Dim grfx As Graphics = CreateGraphics()
        If clearForm = True Then
            grfx.Clear(BackColor)
        End If

        grfx.DrawString(strGraphTitle, New Font("VERDANA", 12.0, _
          FontStyle.Bold, GraphicsUnit.Point), _
          Brushes.DeepPink, intGraphXaxis + (intWidthMax / 4), _
          (intGraphYaxis - intHeightMax) - 40)

        'Get the Height of the Bar        

        intBarHeight = CInt(intHeightMax / intItemCount)

        'Get the space Height of the Bar 

        intSpaceHeight = _
          CInt((intHeightMax / (intItemCount - 1)) - intBarHeight)

        'Find Maximum of the input value

        If Not objEnum Is Nothing Then
            While objEnum.MoveNext = True
                If objEnum.Value > intMaxValue Then
                    intMaxValue = objEnum.Value
                End If
            End While
        End If

        'Get the Maximum Width of the Bar

        intBarWidthMax = CInt(intWidthMax / intMaxValue)

        ' Obtain the Graphics object exposed by the Form.


        If Not objEnum Is Nothing Then
            intCounter = 1
            objEnum.Reset()
            'Draw X axis and Y axis lines

            grfx.DrawLine(Pens.Black, intGraphXaxis, _
              intGraphYaxis, intGraphXaxis + intWidthMax, _
              intGraphYaxis)
            grfx.DrawLine(Pens.Black, intGraphXaxis, _
              intGraphYaxis, intGraphXaxis, _
              (intGraphYaxis - intHeightMax) - 25)

            While objEnum.MoveNext = True
                'Get new Y axis

                intGraphYaxis = intGraphYaxis - intBarHeight
                'Draw Rectangle

                grfx.DrawRectangle(Pens.Black, _
                  New Rectangle(intGraphXaxis, intGraphYaxis, _
                  intBarWidthMax * objEnum.Value, intBarHeight))
                'Fill Rectangle

                grfx.FillRectangle(objColorArray(intCounter), _
                  New Rectangle(intGraphXaxis, intGraphYaxis, _
                  intBarWidthMax * objEnum.Value, intBarHeight))
                'Display Text and value

                strText = "(" & objEnum.Key & "," & objEnum.Value & ")"
                grfx.DrawString(strText, New Font("VERDANA", 8.0, _
                  FontStyle.Regular, GraphicsUnit.Point), _
                  Brushes.Black, intGraphXaxis + _
                  (intBarWidthMax * objEnum.Value), intGraphYaxis)
                intCounter += 1
                If SpaceRequired = True Then
                    intGraphYaxis = intGraphYaxis - intSpaceHeight
                End If
                If intCounter > objColorArray.GetUpperBound(0) Then
                    intCounter = 1
                End If
            End While
            If clearForm = True Then
                grfx.Dispose()
            End If
        End If
    Catch ex As Exception
        Throw ex
    End Try
End Sub

Advantages of the procedure

We can have any number of graphs in a single form, and settings of a graph can be changed at runtime. Using LoadColorArrays procedure (Not pasted here. But available in the demo project) we can give good colors in the graph which will attract the users very much.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Karthikeyan Muthurajan


Working as a S/W Engineer with 3+ years of experience
Having MCAD in .NET
Selected as Microsoft India Community Star
Occupation: Web Developer
Location: India India

Other popular GDI+ articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
Generaldoing it in vb 6memberAnupam Verma21:07 3 Jul '07  
QuestionCan it draw in a panel?member6:19 9 Feb '07  
Generaldrawing a vertical bar chart in Web ApplicationmemberM.Ramesh6:28 19 Jul '06  
GeneralI have a problem with the first linemembertutananthanh21:20 27 Jan '05  
Generaljust another point maker... geeeeeeesussanonymous10:09 18 Jun '04  
GeneralPicture not visiblesussAnonymous5:16 18 Jun '04  
GeneralRe: Picture not visiblememberKarthikeyan Muthurajan20:08 20 Jun '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jun 2004
Editor: Smitha Vijayan
Copyright 2004 by Karthikeyan Muthurajan
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project