Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Chart Success : GDI+ Graphics At Work Part 4
 »  Home  »  Windows Development  »  Graphics  »  Chart Success : GDI+ Graphics At Work Part 4
Chart Success : GDI+ Graphics At Work Part 4
by Ged Mead | Published  05/10/2005 | .NET Framework Graphics | Rating:
Ged Mead

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.

 

View all articles by Ged Mead...
Setting Up

Data Source 

 First, let’s generate some data to display via the chart.   We will create it at design time  to allow us to get on with the core Graphics Class parts of the project.  If you prefer to have the data input by the user at run time then you can adapt the techniques covered in Part 3.

 

Create Data

  Shown below is what has become  our standard code  to create initial variables and to generate  and store some data.   I won’t trundle through the details as they are fully covered in earlier articles:

 


Option Strict On


Imports System.Drawing.Drawing2D
Imports System.Collections

 

Structure GraphData
        Dim Country As String
        Dim Sales As Integer
        Dim BarColor As Color
        Sub New(ByVal country As String, ByVal sales As Short, ByVal barcol As Color)
            Me.Country = country
            Me.Sales = sales
            Me.BarColor = barcol
        End Sub
    End Structure

 

'  Create Variables

‘ # of pixels vertical Axis is inset from PictureBox Left
    Dim LeftMargin As Integer = 35
    '  # of Pixels left unused at right side of PictureBox
    Dim RightMargin As Integer = 15
    '  The number of pixels above bottom of baseline
    Dim BaseMargin As Integer = 35
    '  Margin at Top
    Dim TopMargin As Integer = 10
    '   Set size of gap between each bar
    Dim BarGap As Integer = 12

    Dim SalesData As New ArrayList 
    Dim HighSale As Double  ' Maximum sales figure
    Dim VertScale As Double  ' Scaling used for bar heights

  '   Minor changes for this version:
  '   Declare a variable to hold a Graphics object
        Dim g As Graphics
  '  Variable for Bitmap to be displayed in (PictureBox) control
    Dim bmap As Bitmap
  '  Length of vertical axis
    Dim VertLineLength As Integer
Dim BarWidth As Integer  ' width of bars
Dim BaseLineLength As Integer  ' X Axis length

Private Sub GetData()
        SalesData.Clear() ' Avoid data duplication
        '  Generate some data and store it in the arraylist
        SalesData.Add(New GraphData("Belgium", 934, Color.Blue))
        SalesData.Add(New GraphData("Greece", 385, Color.DarkOrange))
        SalesData.Add(New GraphData("Portugal", 1029, Color.Green))
        SalesData.Add(New GraphData("Spain", 729, Color.IndianRed))
        SalesData.Add(New GraphData("Turkey", 1472, Color.Tomato))
        SalesData.Add(New GraphData("UK", 1142, Color.Aquamarine))
End Sub


 

Controls

   Add a PictureBox named PBBarChart and a Button named btnDraw to the Form. 

 

  Place the button somewhere down in the bottom corner of the Form.

 

   Stretch the PictureBox so that it is close to the top, right and left edges of the form.   Set its Anchor property so that it is anchored to all four sides.  Doing this will allow the user to click the button for a resized version of the chart to be displayed whenever the form size is altered .   (As we will see in later articles, there are more dynamic ways of achieving this, but we’ll stick with this method for now.)

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 05/13/2005)
Rating
Great article, Ged. Thanks for your efforts.
 
Comment #2  (Posted by betrl8thanever on 05/24/2005)
Rating
Excellent work Ged. Very impressive.
 
Comment #3  (Posted by Chris on 07/11/2005)
Rating
This article was great. However in the procedure DrawVerticalAxis, you are declaring VertLineLength as local. VertLineLength was previously declared global in the form. Because of the local declaration the Vertical Line Length is reset back to zero after the procedure call and all the 3d bars display a vertical reading of zero. I removed Dim VertLineLength As Integer = PBBarChart.Height - (BaseMargin + TopMarg and set VertLineLength = PBBarChart.Height - (BaseMargin + TopMargin). The 3d bars now display correctly.

 
Comment #4  (Posted by XTab on 07/11/2005)
Rating
Chris, Many thanks for spotting that error in the article. I have now corrected it. Ged Mead.
 
Comment #5  (Posted by an unknown user on 08/15/2005)
Rating
Looking forward to line charts!
 
Comment #6  (Posted by an unknown user on 12/29/2005)
Rating
Ged, great work done.
I have a problem related to pie chart. I am implementing image mapping on pie chart to do this I need to find out points arrary of a pie piece, after calling FillPie i am unable to get the coordinates of that pie piece. Plese post the way of getting it's coordinates. You can contect me on gupta.mk@rediffmail.com.I will be greatful to you for any help.
 
Comment #7  (Posted by an unknown user on 01/16/2006)
Rating
Best I have seen! Because I understand all, also with my bad English knowledge.
I tried it -> AND IT WORKS!!
 
Comment #8  (Posted by an unknown user on 03/06/2006)
Rating
Someone finally led us through a complete, basic app. Thanks
 
Comment #9  (Posted by an unknown user on 03/10/2006)
Rating
It's Great article
 
Comment #10  (Posted by an unknown user on 05/09/2006)
Rating
This helps a lot. Thanks
 
Comment #11  (Posted by an unknown user on 06/17/2007)
Rating
Thanks. this is the best i have learned.
 
Comment #12  (Posted by an unknown user on 06/17/2007)
Rating
hello sir
great work I am new in vb.net and for me it's take bit time to understand but your method is easy to understand but i have only one problem with draw horizontal lines it's draws one line but i cna't see other nine lines, I know it's my mistake but i could't find it, i have debuge the program but
this two are remains same all the time LineStart.Y -= VertGap LineEnd.Y -= VertGap , so i dont't know that it's wright and second thing about months, it's displays the months but all the name's of months displays on top of eachother so again there is my mistake but i could't find it so sir can you please help me with this
my e-mail is (bigbapu@hotmail.co.uk)
waiting for your kind rep.

have a nice time
 
Comment #13  (Posted by an unknown user on 12/28/2007)
Rating
Dear Sir Your lessons are more helpful. I'm a newbie VB users and I have a question: your Sub works properly whith positive values but if I put negative data in the Sales Data array they are set to zero.
How can I modifie your graphics for visualize negative values?
 
Comment #14  (Posted by Ged Mead on 12/28/2007)
Rating
Hi, Although I haven't tried this with negative values, the approach you will need to use is to readjust the axis so that instead of the base line being fixed at zero, which it is now, you physically move it up the vertical axis. This will allow negative values to "begin" (i.e. to start to be drawn) from the relevant point below the horizontal axis. Hope this helps. If I have time over the holiday I will try and create an additional demo to show this being done.
 
Comment #15  (Posted by an unknown user on 06/06/2008)
Rating
The best and most useful article I have ever read..A true blessing. THANK YOU VERY MUCH!!!
 
Comment #16  (Posted by an unknown user on 06/06/2008)
Rating
The best and most useful article I have ever read..A true blessing. THANK YOU VERY MUCH!!!
 
Comment #17  (Posted by an unknown user on 06/14/2008)
Rating
very useful article!
 
Comment #18  (Posted by Moon on 10/12/2008)
Rating
incentive for a company to grab control ,
 
Comment #19  (Posted by Zena Madara on 10/12/2008)
Rating
youtube is improving well, but i have little request: ,
 
Comment #20  (Posted by Andrew on 12/11/2008)
Rating
These articles are the best I have seen/read on this subject and I have been developing applications using VB for over 20 years. It takes me back to what my lecturer at University told us once - 'Keep it Simple and anything is possible!'
 
Comment #21  (Posted by Verynandus on 03/13/2009)
Rating
Thanks for your Tutorial
 
Sponsored Links