Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

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

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.

 

View all articles by Ged Mead...
Introduction

   There are some quite advanced features that can be incorporated into a dynamic chart, sometimes depending on where the data is coming from.   In this article, we are going to be begin with a fairly simple demo.   Then we will move on to one that is slightly more sophisticated and finally we will look at further enhancements that could be incorporated into this kind of graph.

   So, let me set the scene.  What do we want to do here?   Well, the first dynamic line chart I plan to create is one that takes some simple user input and displays the values as they are chosen on the chart.   As each new value is selected, the chart will be extended and redrawn to show the continuous changes that have been made.  When the screen width is "full", the earliest values are lost from view as the graphic display continues to scroll from right to left.

  One quick and easy demo is to create put a scrollbar on a form and use the changing scrollbar values as the data for the chart.   Take a look at the sample form below.

 

Form Controls    

    The silver grey area is a PictureBox.  As the values in the ScrollBar change, so the graph represents the new values.  I have used a Vertical ScrollBar here, but you can just as easily use a Horizontal ScrollBar.  In either event, the coding logic will be the same.

   Here are the steps to create the form.  

  1. Add a ScrollBar control to the form.  Set its Minimum property to 0, its Maximum to 104.  (If you are wondering why I've picked that Maximum value, see the note at the bottom this page) .  Set the LargeChange property to 5; leave the SmallChange property as 1.  Change the control's name to SBUserValue to reflect its purpose - scroll bar to detect user's chosen values.
  2. Add a PictureBox and name it picGraph.   Set its height to exactly 400.   Why ?   This is really just a way of keeping the scaling code as easy as possible.   The user values are going to be numbers between 0 and 100, so it will be very easy to multiply the value by 4 to automatically scale it vertically on the picturebox graph.     Real life almost certainly won't be this easy, but it will help us concentrate on the important drawing stuff for this first demonstration.  Change the PictureBox'sname to picGraph and set its backcolor to Gainsborough (or any other color that you prefer)
  3. Finally,add a label to display the changing selection value.  This label plays no part in the chart creation; I've included it so you can check that any given value is correctly represented on the chart itself.   Name it lblValue.
  4. The remaining label controls shown on the screenshot above are entirely optional and are not referred to in any of the code that follows.

   Initial Code

   Place this Imports statement at the top of the form:

 Imports

System.Drawing.Drawing2D

    As you will know if you have read the earlier articles in this series, this allows us to use any of the  Drawing.Drawing2D class methods and properties in code without having to keep writing out the fully qualified names each time.  

   In the next step we will build the code that does all the drawing in the chart, dynamically changing the display each time a new value is selected via the ScrollBar.

----------------------------------------------------------------------------------------------------------------

   Note:

     The reason for setting a Maximum value of 104 (when what we really want is 0 to 100 on the scale) is due to what seems to me to be a small foible with the ScrollBar control.     If you set the Maximum to 100 and the Large Change to 5 then the highest value you will be able to physically obtain by moving the slider or clicking on the ScrollBar's arrow will be 96.  There may be an official fix for this, but I find that offsetting the Maximum by a figure of (LargeChange - 1) seems to do the trick.

  

 

     

   

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
 
Comment #45  (Posted by oem software on 02/07/2012)
Rating
RbZEKa See it for the first time!!....
 
Comment #46  (Posted by oem software on 02/07/2012)
Rating
1ESamS Fresh thoughts, fresh view on the subject..!!
 
Comment #47  (Posted by buy cheap oem software on 02/10/2012)
Rating
9XsxK3 Fresh thoughts, fresh view on the subject..!!
 
Comment #48  (Posted by buy cheap oem software on 02/10/2012)
Rating
1r4mtw I do`t see a feedback or the other coordinates from the blog administration!....
 
Comment #49  (Posted by Buy oem Software on 02/11/2012)
Rating
78qKtI Last a few years has been to Ibiza, so met a person there whose style of presentation is very similar to yours. But, unfortunately, that person is too far from the Internet!....
 
Comment #50  (Posted by Buy oem Software on 02/12/2012)
Rating
q73Ta1 Yeah !... life is like riding a bicycle. You will not fall unless you stop pedaling!!....
 
Comment #51  (Posted by college basketball on 02/26/2012)
Rating
jlzqxq Uh, well, explain me a please, I am not quite in the subject, how can it be?!...
 
Comment #52  (Posted by basketball gambling on 02/26/2012)
Rating
cKt0JX Yeah, it is clear now !... From the very beginning I did not understand where was the connection with the title !!...
 
Comment #53  (Posted by nba basketball on 02/26/2012)
Rating
HRHBr3 Yeah? I read and I understand that I do not understand anything what it is about:D
 
Comment #54  (Posted by basketball betting on 02/26/2012)
Rating
spVaew Thanks for all the answers:) In fact, learned a lot of new information. Dut I just didn`t figure out what is what till the end!...
 
Comment #55  (Posted by nba basketball on 02/26/2012)
Rating
lri5IC Thanks, useful material I added your blog to my bookmarks!...
 
Comment #56  (Posted by Discount OEM Software on 03/07/2012)
Rating
FsW5jB I appreciate you sharing this post.Really looking forward to read more. Will read on...
 
Comment #57  (Posted by Adobe OEM Software on 03/07/2012)
Rating
vMeKUc Really informative article post.Really looking forward to read more. Really Great.
 
Comment #58  (Posted by Buy Cheap OEM Software on 03/08/2012)
Rating
gkv93I Really informative blog article.Really looking forward to read more. Want more.
 
Comment #59  (Posted by Microsoft OEM Software on 03/08/2012)
Rating
LZTKyH Enjoyed every bit of your blog. Much obliged.
 
Comment #60  (Posted by wholesale men clothing on 03/20/2012)
Rating
Enjoyed every bit of your article post. Great.
 
Comment #61  (Posted by Reviews of LifeCell on 04/04/2012)
Rating
Very neat article.Really looking forward to read more. Really Great.
 
Comment #62  (Posted by Reviews of LifeCell on 04/04/2012)
Rating
Im grateful for the post.Really thank you! Cool.
 
Comment #63  (Posted by LifeCell Reviews on 04/04/2012)
Rating
I truly appreciate this blog post.Really thank you! Keep writing.
 
Comment #64  (Posted by LifeCell Reviews on 04/04/2012)
Rating
Major thankies for the article. Keep writing.
 
Comment #65  (Posted by Reviews of LifeCell on 04/04/2012)
Rating
Im obliged for the article post.Much thanks again. Awesome.
 
Comment #66  (Posted by RFP on 04/05/2012)
Rating
j2cK7a Fantastic blog.Thanks Again. Awesome.
 
Comment #67  (Posted by Suppliers on 04/05/2012)
Rating
kMdFol A round of applause for your blog post. Really Great.
 
Comment #68  (Posted by Request for Quotation on 04/05/2012)
Rating
xL0Jgs I loved your blog article. Fantastic.
 
Comment #69  (Posted by Buyers on 04/05/2012)
Rating
grv1vW Fantastic blog.Much thanks again. Really Great.
 
Comment #70  (Posted by RFQ on 04/05/2012)
Rating
3T231r Really enjoyed this post.Much thanks again. Really Cool.
 
Comment #71  (Posted by notebook review on 04/13/2012)
Rating
Major thankies for the article.Thanks Again. Awesome.
 
Comment #72  (Posted by notebook review on 04/13/2012)
Rating
Really informative blog post. Great.
 
Comment #73  (Posted by notebook review on 04/13/2012)
Rating
Enjoyed every bit of your post.Really looking forward to read more. Keep writing.
 
Comment #74  (Posted by laptop notebook on 04/13/2012)
Rating
Thanks so much for the post.Thanks Again. Really Cool.
 
Comment #75  (Posted by laptop review on 04/14/2012)
Rating
fDO93I Thanks for the blog post.Thanks Again. Fantastic.
 
Comment #76  (Posted by laptop review on 04/14/2012)
Rating
WAt7if I cannot thank you enough for the article post. Will read on...
 
Comment #77  (Posted by laptop review on 04/14/2012)
Rating
dMSzMU Enjoyed every bit of your article post.Much thanks again. Really Great.
 
Comment #78  (Posted by laptop notebook on 04/14/2012)
Rating
DWDfvb Awesome blog post.Really thank you! Keep writing.
 
Comment #79  (Posted by laptop notebook on 04/14/2012)
Rating
GOfIkD I loved your article.Thanks Again.
 
Comment #80  (Posted by logo design on 04/18/2012)
Rating
4lLDeF Thanks again for the article.Really thank you! Much obliged.
 
Comment #81  (Posted by Bristol Airport Hotels on 04/18/2012)
Rating
sT2IIi Really informative article.Thanks Again. Really Cool.
 
Comment #82  (Posted by Bristol Airport Hotels on 04/18/2012)
Rating
EiAK29 Looking forward to reading more. Great blog article.Much thanks again. Awesome.
 
Comment #83  (Posted by seo on 05/07/2012)
Rating
Im grateful for the article. Will read on...
 
Comment #84  (Posted by social on 05/07/2012)
Rating
Thanks for the post.Much thanks again. Really Great.
 
Comment #85  (Posted by social on 05/07/2012)
Rating
I really liked your blog post.Much thanks again. Awesome.
 
Comment #86  (Posted by bookmarking on 05/07/2012)
Rating
I really enjoy the post.Much thanks again. Great.
 
Comment #87  (Posted by cheap on 05/07/2012)
Rating
This is one awesome post.Really looking forward to read more. Want more.
 
Comment #88  (Posted by serivce on 05/08/2012)
Rating
I really enjoy the blog.Much thanks again. Want more.
 
Comment #89  (Posted by bookmarking on 05/09/2012)
Rating
VCIiMP Enjoyed every bit of your blog.Much thanks again. Cool.
 
Comment #90  (Posted by services on 05/09/2012)
Rating
fmx2VH Major thanks for the blog. Really Cool.
 
Comment #91  (Posted by bookmarking on 05/10/2012)
Rating
DSDuuc Major thanks for the post. Awesome.
 
Comment #92  (Posted by bookmarking on 05/10/2012)
Rating
1sljxG I appreciate you sharing this post. Will read on...
 
Comment #93  (Posted by service on 05/10/2012)
Rating
Byy3PW Fantastic post. Want more.
 
Comment #94  (Posted by bookmarking on 05/10/2012)
Rating
zaEmWL I really like and appreciate your blog. Want more.
 
Sponsored Links