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.    

Sponsored Links