![]() |
Multimedia »
GDI+ »
General
Intermediate
GraphSheet Control for WindowsBy Harish PalaniappanA GraphSheet control in .NET, using GDI+ and the System.Drawing namespace. |
VB.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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 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.
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 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.
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.
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.
Invalidate method to make sure your object updates every time there's a change.
Form, a Panel, etc. But if you use anything other than the Image objects, you will have to redraw on every paint. Well, we have Chris Maunder commenting on this article. God, where did I get into...was my first expression on the comment. :)
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Nov 2005 Editor: Smitha Vijayan |
Copyright 2003 by Harish Palaniappan Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |