Click here to Skip to main content
12,252,465 members (58,211 online)
Click here to Skip to main content
Add your own
alternative version

Stats

144K views
985 downloads
42 bookmarked
Posted

GraphSheet Control for Windows

, 29 Nov 2005
Rate this:
Please Sign up or sign in to vote.
A GraphSheet control in .NET, using GDI+ and the System.Drawing namespace.

Sample Image - GraphSheetControl.png

Introduction

In this article, I intend to create a GraphSheet control in .NET, using GDI+. It's a plain simple control using all available features of the Graphics object. We create the GraphSheet control as a 'Windows User Control' project. More on the control and its usage in 'Why? When? How?' below...

The code

The code is self explanatory. In this GraphSheet control, I create a bitmap of the graph sheet with gridlines. I have also created public methods to make it easier to draw points, and to add a set of points to an array which can then be used to draw a line or a curve. There are also public variables to introduce an offset in the X or Y axes or both axes, in the graph. We also create Boolean flags to enable or disable gridlines, display scale units as text, etc.

Using the Control

Add the control to any Windows project.. and then call its methods and properties like any other control.

Private Sub Form1_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
     gs.Xscale_Max = 10
     gs.Yscale_Max = 10
     gs.Xscale_units = 1
     gs.Yscale_units = 1

     gs.showBorder = True

     gs.displayUnits = True
     gs.fontSize = 6

     'gs.MarginX = 10
     'gs.MarginY = 40
     gs.initializeGraphSheet()
     gs.AddPoint(1, 1)
     gs.AddPoint(2, 5)
     gs.AddPoint(3, 3)
     gs.AddPoint(5, 5)
     gs.AddPoint(7, 5)
     gs.AddPoint(9, 9)
     gs.AddPoint(7, 9)
     gs.AddPoint(4, 6)

    gs.DrawGraph(GraphSheetControl.GraphSheet.PlotType.Curve, _
          Color.Blue, False)
    'gs.DrawPoint(2, 5, Color.SaddleBrown)
End Sub

.NET and Graphics

.NET has definitely made things simple for producing graphics, especially for VB programmers. We had to really be good at mathematics if we had to produce an optimized yet flexible code like this in Visual basic 6.0. One good example is the DrawCurve method of the Graphics object which draws a curve through a set of points passed to it as an array. If it were Visual Basic 6.0, it wouldn't have been this simple.

oG.DrawCurve(New Pen(color.red), PointsArray)

.NET though, hasn't made everything easy for the graphics part of things, for VB programmers at least. Creating a Windows control that would draw itself entirely is not easy, and even if done, not feasible to be used in production.

  • Every time the control's Paint event is fired, it would have to draw itself. Implementing this causes flicker, continuously firing the event, and even MSDN advises against writing code in paint events for such controls.
  • For a control that draws itself, I could not find any easy mechanism to save the drawing as a graphic object, to be restored during repaints. I could only find methods to save and restore the state of the Graphics object. It seems like drawing can be stored only as an Image, and have to be restored from an Image. I preferred using a simple mechanism of creating an Image object and displaying it in a PictureBox in this article's code.
  • Graphics objects don't redraw / repaint themselves, neither do they remember their last drawn state. You have to call an Invalidate method to make sure your object updates every time there's a change.
  • You can draw on various surfaces, a Form, a Panel, etc. But if you use anything other than the Image objects, you will have to redraw on every paint.

Why? When? and How?

Well, we have Chris Maunder commenting on this article. God, where did I get into...was my first expression on the comment. Smile | :)

The GraphSheet control was to make my code more pleasant on a small project, where I needed to plot graphs based on information, dynamically. The image added to this article has a small clipping of the output (right hand side of the image) of this project.

The control did make the final code well maintainable. I had to just modify code in the control to make offsetting, err handling, etc., possible, and the change took effect on all graphs drawn thereafter.

With the Drawing and Graphics namespace related code isolated, the project's code clearly showed a sign of big relief. I had to create as many instances of the GraphSheet control needed, add points from the data to the control (here, we could also modify the control to allow data binding), and draw the graphs. More importantly, I had created logic in the control to size the graph according to the size of the control, and to allow offsetting if necessary.

To add to all this, since it's a control, I could create an instance dynamically and position it on my project.

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

Share

About the Author

Harish loves computers for their basic quality of running tasks given and doing things clockwork without day-dreaming like him.

Note: My employer does not subscribe to my thoughts posted in my articles.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey20-Feb-12 2:47
membermanoj kumar choubey20-Feb-12 2:47 
GeneralWrong second serie graph Pin
edwarmartinez23-May-08 17:10
memberedwarmartinez23-May-08 17:10 
QuestionHow to set up to compile in Visual Basic 2005 Express Pin
ringram207721-Feb-07 7:37
memberringram207721-Feb-07 7:37 
AnswerRe: How to set up to compile in Visual Basic 2005 Express Pin
pm810-Oct-07 23:34
memberpm810-Oct-07 23:34 
GeneralUnits dont appear Pin
sanketkhan30-Jan-07 13:39
membersanketkhan30-Jan-07 13:39 
GeneralSize the graph according to the size of the control Pin
ayshegul4-Sep-06 22:50
memberayshegul4-Sep-06 22:50 
GeneralVery Helpful Pin
Cheebee23-May-06 5:48
memberCheebee23-May-06 5:48 
Generalmore confusing than helpful Pin
markwaldin31-Mar-06 14:20
membermarkwaldin31-Mar-06 14:20 
GeneralRe: more confusing than helpful Pin
desdichado181915-Apr-06 12:32
memberdesdichado181915-Apr-06 12:32 
GeneralRe: more confusing than helpful Pin
sanketkhan30-Jan-07 15:46
membersanketkhan30-Jan-07 15:46 
Questioni want use paint in vb.net. Please help me. Pin
ss_136325-Mar-06 21:07
memberss_136325-Mar-06 21:07 
Generalgs.clearPointsTable() Pin
[Neno]13-Mar-06 9:25
member[Neno]13-Mar-06 9:25 
GeneralRe: gs.clearPointsTable() Pin
[Neno]13-Mar-06 10:03
member[Neno]13-Mar-06 10:03 
Generalvb,net simple program Pin
kondapally9-Mar-06 5:39
memberkondapally9-Mar-06 5:39 
GeneralMisstakes in V1.1 Pin
Felix Reimer21-Feb-06 20:54
memberFelix Reimer21-Feb-06 20:54 
GeneralLabels for the grid Pin
Rowbinder9-Jan-06 1:24
memberRowbinder9-Jan-06 1:24 
GeneralRe: Labels for the grid Pin
desdichado181915-Apr-06 12:37
memberdesdichado181915-Apr-06 12:37 
GeneralRe: Labels for the grid Pin
sanketkhan30-Jan-07 15:45
membersanketkhan30-Jan-07 15:45 
QuestionI added some features, do you want them? Pin
Dave Sherman23-Nov-05 9:41
memberDave Sherman23-Nov-05 9:41 
AnswerRe: I added some features, do you want them? Pin
kdaily9913-Nov-07 14:00
memberkdaily9913-Nov-07 14:00 
Generaltwo series Pin
Mr Fontenot17-Jul-05 15:20
memberMr Fontenot17-Jul-05 15:20 
GeneralRe: two series Pin
kurtspokas16-May-07 3:39
memberkurtspokas16-May-07 3:39 
GeneralRe: two series Pin
edwarmartinez23-May-08 15:59
memberedwarmartinez23-May-08 15:59 
Generalplot display with VB program Pin
Janet Willi15-May-05 15:55
memberJanet Willi15-May-05 15:55 
QuestionMaximum Lenght? Pin
mcprichy29-Jul-04 12:39
membermcprichy29-Jul-04 12:39 
QuestionMaximum Lenght? Pin
mcprichy29-Jul-04 12:34
membermcprichy29-Jul-04 12:34 
GeneralMore explanation needed Pin
Chris Maunder5-Dec-03 4:40
adminChris Maunder5-Dec-03 4:40 
GeneralRe: More explanation needed Pin
Harish Palaniappan8-Dec-03 3:04
memberHarish Palaniappan8-Dec-03 3:04 
GeneralRe: More explanation needed [modified] Pin
yangjingyu122028-Aug-06 20:59
memberyangjingyu122028-Aug-06 20:59 
GeneralThe code is self explanatory Pin
SteveKing3-Dec-03 4:16
memberSteveKing3-Dec-03 4:16 
GeneralRe: The code is self explanatory Pin
Normski3-Dec-03 6:42
memberNormski3-Dec-03 6:42 
GeneralRe: The code is self explanatory Pin
Harish Palaniappan3-Dec-03 16:51
memberHarish Palaniappan3-Dec-03 16:51 
GeneralImage doesn't load Pin
Joel Holdsworth3-Dec-03 1:49
memberJoel Holdsworth3-Dec-03 1:49 
GeneralRe: Image doesn't load Pin
Harish Palaniappan3-Dec-03 4:02
memberHarish Palaniappan3-Dec-03 4:02 
GeneralRe: Image doesn't load Pin
Chris Maunder5-Dec-03 4:39
adminChris Maunder5-Dec-03 4:39 
GeneralRe: Image doesn't load Pin
Anonymous7-Dec-03 17:41
sussAnonymous7-Dec-03 17:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 29 Nov 2005
Article Copyright 2003 by Harish Palaniappan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid