Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  GDI+ Chart Success Part 6: Dynamic Line Chart  »  Basic Chart Special Effects
 »  Home  »  Windows Development  »  Graphics  »  GDI+ Chart Success Part 6: Dynamic Line Chart  »  Basic Chart Special Effects
 »  Home  »  Windows Development  »  Win Forms  »  GDI+ Chart Success Part 6: Dynamic Line Chart  »  Basic Chart Special Effects
GDI+ Chart Success Part 6: Dynamic Line Chart
by Ged Mead | Published  08/22/2006 | .NET Newbie Graphics Win Forms | Rating:
Basic Chart Special Effects

Shaded Graph
If you want a chart that looks like this:

then you can achieve this effect with a minor code tweak. Replace Step 6 in the DrawGuidelinesAndChart procedure with the code block below:

' Step 6

' Draw the next line segment on the Graphics object.

gr.DrawLine(Pens.Black, _
picGraph.Width - 1 - Xmove, -OldValue, _
picGraph.Width - 1, -NewValue)
OldValue = NewValue
' Draw another line segment, but this time leave the Y value of
' OldValue as positive.

gr.DrawLine(Pens.LightBlue, _
(picGraph.Width - 1) - Xmove, OldValue, _
picGraph.Width - 1, -NewValue)
OldValue = NewValue

As you can see, the only change is a second sub block which uses Drawline again to draw a second line segment on the Graphics object. The difference here is that the start point of this segment is the positive value of OldValue, which you will recall means that it is located down below the bottom of the PictureBox. So drawing this second line has the effect of creating a line that runs from the bottom of the PictureBox up until it meets the point where we plotted the negative value of NewValue.
Sounds complicated at first, I know, but if you step through it a line at a time I hope it will make sense.

Or something a bit more striking:

Again, this requires just a small edit to Step 6. This time though, instead of drawing lines, we draw a polygon. The top of the polygon is a line that is exactly the same line as we have drawn in the previous examples; the remainder of the polygon is created by drawing two vertical lines down from either end of this line to the bottom of the PictureBox, plus a horizontal line along the bottom.
The FillPolygon method takes these four points and draws a polygon shape filled with our choice of color.

Here's the replacement Step 6:

' Step 6
' Draw Polygon to fill areas below the plotted line.

Dim topLeft As New Point((picGraph.Width - 1) - Xmove, -OldValue)
Dim topRight As New Point((picGraph.Width - 1), -NewValue)
Dim bottomRight As New Point(picGraph.Width - 1, picGraph.Height)
Dim bottomLeft As New Point((picGraph.Width - 1) - Xmove, picGraph.Height)
Dim PolyPoints As Point() = {topLeft, topRight, bottomRight, bottomleft}
gr.FillPolygon(Brushes.Red, PolyPoints)

OldValue = NewValue


 

One final variation for you to try out:

The amended Step 6 looks like this:

' Step 6
' Draw Polygon to fill areas below the plotted line.
Dim topLeft As New Point((picGraph.Width - 1) - Xmove, -OldValue)
Dim topRight As New Point((picGraph.Width - 1), -NewValue)
Dim bottomRight As New Point(picGraph.Width - 1, picGraph.Height)
Dim bottomLeft As New Point((picGraph.Width - 1) - Xmove, picGraph.Height)
Dim PolyPoints As Point() = {topLeft, topRight, bottomRight, bottomLeft}
Dim hb As New Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.Trellis, Color.Black, Color.White)


' Alternative: Uncomment this line for a more subtle version: 
' It changes the ForeColor and BackColor properties for more variations
gr.FillPolygon(hb, PolyPoints)
OldValue = NewValue

You can play around with variations of these for hours! If you want a less jagged effect on the plotted lines, try increasing the value of XMove.

Comments    Submit Comment

Comment #1  (Posted by Mark Prichard on 08/24/2006)
Rating
I appreciate this series very much. Within minutes of finding your articles I had adapted the ideas to work in my personal project, and had a working graph.Thanks for taking the time to create this series.
 
Comment #2  (Posted by Robert Gillespie on 09/05/2006)
Rating
This is great. From knowing nothing about graphs I can now do graphs. Only one question. How would you print the graph.
 
Comment #3  (Posted by Ged Mead on 09/19/2006)
Rating
It looks as though there's going to have to be a Part 7 after all :-} I will publish a follow up showing how to print graphs, hopefully some time in the next few weeks.
In the meantime, Robert, I will email you some outline guidance.
 
Comment #4  (Posted by an unknown user on 09/28/2006)
Rating
Excellent article.
 
Comment #5  (Posted by FERNANDO HOOD on 01/06/2007)
Rating
Excellent document however the function keeps failing at

If Not IsNothing(PicBox.Image) Then
gr.DrawImage(PicBox.Image, -XMove, 0)
End If

System.InvalidOperationException was unhandled
Message="Object is currently in use elsewhere."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.get_ImageRectangle()
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at MaxiCOM.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()





 
Comment #6  (Posted by an unknown user on 01/10/2007)
Rating
How do I deal with a situation where the data for the graph is handle brought in b a subroutine running on it own thread e.g the serial port?
 
Comment #7  (Posted by an unknown user on 01/27/2007)
Rating
One of the best articles I have seen on charting. Very focused and "to the point" descriptions. Concentrates on the task at hand than introducing unnecessary programming complexities.
 
Comment #8  (Posted by an unknown user on 11/28/2007)
Rating
Great series!
 
Comment #9  (Posted by an unknown user on 09/01/2008)
Rating
This is Great!
 
Comment #10  (Posted by an unknown user on 06/12/2009)
Rating
This article teach me lot of things;
.. It is mind Blowing
 
Comment #11  (Posted by on 10/11/2009)
Rating

 
Comment #12  (Posted by As a narcotic analgesic, Tramadol can be abused. on 11/07/2009)
Rating
plavix libido plavix libido plavix libido . plavix libido plavix libido . plavix libido plavix libido . plavix libido uk carisoprodol supplier uk carisoprodol supplier . uk carisoprodol supplier uk carisoprodol supplier . uk carisoprodol supplier uk carisoprodol supplier . uk carisoprodol supplier uk carisoprodol supplier uk carisoprodol supplier . generic ambien soft tab generic ambien soft tab generic ambien soft tab generic ambien soft tab generic ambien soft tab . generic ambien soft tab generic ambien soft tab generic ambien soft tab generic ambien soft tab generic ambien soft tab .
 
Comment #13  (Posted by an unknown user on 11/12/2009)
Rating
First class intro for novices!!
 
Comment #14  (Posted by water-soluble and they are renally excreted. on 11/25/2009)
Rating
The mission of the your site.: URLsWithNothing
 
Comment #15  (Posted by Dsfgrg on 11/27/2009)
Rating
The more you know about site: URLsWithURL
 
Comment #16  (Posted by Silvia on 02/18/2010)
Rating
b6KspU I am always excited to visit this blog in the evenings.Please churning hold the contents. It is very entertaining.
 
Comment #17  (Posted by Dave on 03/02/2010)
Rating
Excellent, this really helped me out and was really well explained...
 
Comment #18  (Posted by Dave on 03/02/2010)
Rating
Where can I find the rest of the series please?
 
Comment #19  (Posted by Dave on 03/02/2010)
Rating
Where can I find the rest of the series please?
 
Sponsored Links