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) has been around computers since the 1980's when the first affordable home computers came on the market. His journey from that very first Dragon 32 to the present has taken him through many different facets of the IT Industry. These include formal training as a Systems Analyst, employment in a mainframe software development environment, and a short time spent demonstrating rugged military IT systems in the days when it took two strong men to carry a 'mobile' system.

His most rewarding challenge was the creation of a financial management system for a large organisation.

Now based in an idyllic lochside location in the West of Scotland, he is currently involved in a range of development projects, whenever he can drag his gaze away from the stunning surrounding views, that is!

Ged is a Microsoft MVP, Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, Helper of the Month competition winner and DevCity.NET newsletter Editor.

 

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.
 
Sponsored Links