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?
 
Comment #20  (Posted by Raja on 05/08/2010)
Rating
Hi, where can i find the source code file attachment..
 
Comment #21  (Posted by Silvia on 09/22/2010)
Rating
I am always excited to visit this blog in the evenings.Please churning hold the contents. It is very entertaining.
 
Comment #22  (Posted by OEM software download on 09/23/2011)
Rating
IY2n62 See it for the first time!!...
 
Comment #23  (Posted by Download oem software on 09/29/2011)
Rating
f5zrly Yet, much is unclear. Could you describe in more details!...
 
Comment #24  (Posted by OEM software online on 10/28/2011)
Rating
1EW5oN Good day! I do not see the conditions of using the information. May I copy the text from here on my site if you leave a link to this page?!...
 
Comment #25  (Posted by OEM software download on 10/29/2011)
Rating
L4taoF Uh, well, explain me a please, I am not quite in the subject, how can it be?!...
 
Comment #26  (Posted by OEM software download on 11/05/2011)
Rating
j6m42O Author, keep doing in the same way..!!
 
Comment #27  (Posted by Buy software on 11/05/2011)
Rating
W1ooml The Author is crazy..!!
 
Comment #28  (Posted by Buy OEM software online on 11/06/2011)
Rating
EkMIAP Received the letter. I agree to exchange the articles..
 
Comment #29  (Posted by Buy OEM software online on 11/09/2011)
Rating
bPDvcL Good! Wish everybody wrote so:DD
 
Comment #30  (Posted by Buy cheap Microsoft Office software online on 12/27/2011)
Rating
R7jaTU Sometimes I also see something like this, but earlier I didn`t pay much attention to this!....
 
Comment #31  (Posted by Buy cheap software online on 12/27/2011)
Rating
rwi0w4 Yeah, it is clear now !... Just can not figure out how often do you update your blog?!....
 
Comment #32  (Posted by Cheap oem software on 12/27/2011)
Rating
VmvnJu It`s really useful! Looking through the Internet you can mostly observe watered down information, something like bla bla bla, but not here to my deep surprise. It makes me happy..!!
 
Comment #33  (Posted by microsoft OEM software online on 12/27/2011)
Rating
JAyA5Z Informative, but not convincing. Something is missing but what I can not understand. But I will say frankly: bright and benevolent thoughts!....
 
Comment #34  (Posted by Microsoft Office oem software on 12/27/2011)
Rating
Z2TtA3 I would add something else, of course, but in fact almost everything is mentioned!....
 
Comment #35  (Posted by buy discount oem software on 01/11/2012)
Rating
M9Ra4g comment1
 
Comment #36  (Posted by oem software on 01/11/2012)
Rating
zUPqvC comment1
 
Comment #37  (Posted by discount oem software on 01/11/2012)
Rating
LAu1H7 comment4
 
Comment #38  (Posted by cheap oem software on 01/11/2012)
Rating
o1GK67 comment2
 
Comment #39  (Posted by cheap oem software on 01/11/2012)
Rating
FLZD9Z comment4
 
Comment #40  (Posted by buy oem software online on 01/13/2012)
Rating
gZv33C comment4
 
Comment #41  (Posted by cheap oem software on 01/13/2012)
Rating
4ugZv3 comment6
 
Comment #42  (Posted by oem software on 01/13/2012)
Rating
siGE9X comment4
 
Comment #43  (Posted by oem software online on 01/13/2012)
Rating
9fzFId comment2
 
Comment #44  (Posted by discount oem software on 01/13/2012)
Rating
6H06k2 comment2
 
Sponsored Links