Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Chart Success: GDI+ Graphics at work. Part 1  »  First Things First: Data
 »  Home  »  Windows Development  »  Graphics  »  Chart Success: GDI+ Graphics at work. Part 1  »  First Things First: Data
Chart Success: GDI+ Graphics at work. Part 1
by Ged Mead | Published  03/06/2005 | .NET Newbie Graphics | Rating:
First Things First: Data

   It’s obvious that we will need some data in order to create our example chart.Because this article is aimed at showing you ways of using GDI+ and the Graphics Class,I want to spend as little time as possible on the data gathering side of the project.

For this reason I have chosen to generate the data at design time.Although this may be suitable in a few real world situations, I am sure that most times you will need to get the data from other sources, such as data files, databases or directly input by the user.We plan to look at these other approaches in future articles.

For the time being, I have settled for using a simple Structure thatcreates a user-defined Type named GraphData.

   This Type will contain three Fields – Amount, Clr and Description .We will be creating data for some fictional companies and the three fields represent:

Amount -the Annual Turnover in $K

Clr  - the color used in the chart to represent the company

Description – the name of the company.

To create the GraphData Structure, put the following code in your form, making sure it is placed outside any procedures.

     

Structure GraphData
           Dim Amount As Single
           Dim Clr As Color
           Dim Description As String
  
           '  Create a constructor
           Sub New(ByVal amt As Integer, ByVal col As Color, _
                ByVal desc As String)
               Me.Amount = amt
               Me.Clr = col
               Me.Description = desc
           End Sub
      End Structure

  The New constructor is only included to reduce the number of lines of code needed when we "manufacture" the data, which is our next step.

   For convenience of handling, the data will be stored in an arraylist.  The arraylist can hold any number of GraphData objects.  In fact, we are only going to create four for current demonstration purposes.

   Declare and instantiate the arraylist by putting the following line at the top of your code, again placing it outside any procedures.

      Dim Companies As New System.Collections.ArrayList

Now to generate the sample data.We will create the four companies and assign values for their Amount, Col (Color) and Description fields.This code in the Form’s Load event is all that is needed:

   Companies.Add(New GraphData(50, Color.Blue, "Muir Inc."))
   Companies.Add(New GraphData(75, Color.Yellow, "Philmas Co."))
   Companies.Add(New GraphData(62, Color.Red, "Xamco"))
   Companies.Add(New GraphData(27, Color.LightGreen, "Wright plc"))

 We have the data, so it can now be used as the source of the chosen graphics display - the Pie Chart.    

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 03/13/2005)
Rating
The subject you have picked is one of the most interesting ones in programming. But sure enough, that may just be my opinion. When an interesting subject comes with explanations that are detailed but still very readable, then this means that the author has a great talent for writing and it shows immense responsibility and patience with beginning programmers. And that’s exactly what your article has achieved.
 
Comment #2  (Posted by an unknown user on 03/16/2005)
Rating
Very nice, Ged.

A small suggestion for part II: when would you use GDI+ vs. DirectX?
 
Comment #3  (Posted by Ged Mead on 03/22/2005)
Rating
Thanks for the comments. Part 2 is out now .... but I think it'll be a while before I get round to DirectX!
 
Comment #4  (Posted by an unknown user on 06/12/2005)
Rating
Hi Ged,
Nice and the way u explained is excellent .
The explanation is in steps and easy to understand and for me it the best methord to understand .
Good work i dont have word to appriciate u but excellent job.
From
Mohd Sufian
IT Specialist
Eastern Polymer Group
Thailand
 
Comment #5  (Posted by Dave on 10/03/2005)
Rating
Explained quite well! However I have a persistence problem that you mention in your article. I am drawing inside a picture box. My drawing code is in its own function which I call from the Paint() method. When the form first opens my drawing flashes up quite briefly and then disappears. It reappears and remains whenever I resize the window. How do I get it to come up without telling it to refresh which puts it in an endless loop?
 
Comment #6  (Posted by an unknown user on 04/05/2006)
Rating
Great explanation.I would like to know how to integrate database data with GDI.
 
Comment #7  (Posted by Ronan on 04/13/2006)
Rating
Hi i have read these tutorials and am looking your help.
I am trying to develop a pie chart interface in Visual Studio 2003 for a PDA (Smart Device Application) and tried your code. however it is not compatible as it does not recognise the drawPie and FillPie commands, etc. Do you have an example i could use to help me for this?
Thank you, Ronan
 
Comment #8  (Posted by an unknown user on 08/09/2006)
Rating
Excellent article. I have learnt a lot of GDI+ Stuff from it.
 
Comment #9  (Posted by an unknown user on 10/19/2006)
Rating
Very Useful article. Prakash Bajaj
 
Comment #10  (Posted by an unknown user on 12/22/2006)
Rating
really useful
 
Comment #11  (Posted by an unknown user on 12/29/2006)
Rating
no corners cut, all is clear
 
Comment #12  (Posted by an unknown user on 07/09/2007)
Rating
thank you
Very simple
detailed explanation
 
Comment #13  (Posted by an unknown user on 07/22/2007)
Rating
best for beginner's. language is just perfect
 
Comment #14  (Posted by neelabh on 07/22/2007)
Rating
Language is clear nd decription is best for beginner
 
Comment #15  (Posted by an unknown user on 10/01/2007)
Rating
Easy to follow tutorial. Picked up the main points straight away.
 
Comment #16  (Posted by an unknown user on 12/28/2007)
Rating
I have been searching for a good graphics article to get me started...with this one I found what I was looking for...GREAT JOB. Now I need to found out how to do vertical text.
 
Comment #17  (Posted by an unknown user on 02/05/2008)
Rating
Free, good, patient advice. A rare commodity and precious to beginners.
 
Comment #18  (Posted by an unknown user on 02/06/2008)
Rating
thanks for giving such a good article to start GDI+ from scratch.
 
Comment #19  (Posted by an unknown user on 02/20/2008)
Rating
good one
 
Sponsored Links