Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

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

Vertical Axis Values
So far, the user has been able to see the current value displayed in a label, which is useful but not always realistic. However, on the chart itself the visual clue to the current value has been limited to the grey horizontal lines. In most cases we will want something more definite than this. Specifically, it would be better to have a value displayed at the end of each of the guidelines that our users can refer to.

Although as always there are several ways of achieving this, one of the easiest is simply to create a second PictureBox which contains these values and place it to the side of the picGraph PictureBox.

 

Add a second PictureBox to the form. Place it to the left hand side of the original picGraph PictureBox. Set its Size to 30, 400. This will make it the same height as the picGraph PictureBox. Name it picValues.

The easy way to line up the two PictureBoxes is to use the Format menu in the Visual Studio IDE. You probably already know all about this, but just in case:

  • Click the left mouse button on one of the PictureBoxes.
  • Hold down the Control key .
  • Click on the second PictureBox, still holding the Control key down.
  • Select Format - Align - Tops from the IDE Menu:
  • Next, Select Format - Horizontal Spacing - Remove:

  • If necessary, you can manually drag both PictureBoxes together to any suitable position on the form.
  • Finally click anywhere on the form's main area (i.e. not in either of the PictureBoxes) to unlock the selection.

    Code for the Vertical Values
    The procedure used to put guidelines and values in the new PictureBox is called DrawVerticalValues. This is it:

    Private Sub DrawVerticalValues(ByVal PB As PictureBox)
    ' Draw vertical values (again using double buffering, ' although it's not really necessary)
    ' Step 1

    Dim bmp As New Bitmap(PB.Width, PB.Height)
    Dim gv As Graphics = Graphics.FromImage(bmp)

    ' Step 2
    ' Draw guidelines on values strip

    For i As Integer = 40 To 400 Step 40
    gv.DrawLine(Pens.WhiteSmoke, 0, i, PB.Width, i)
    Next i


    ' Step 3
    Dim NextMarker As Integer = 100
    For i As Integer = 0 To 360 Step 40
    gv.DrawString(CStr(NextMarker), New Font("Verdana", 8, FontStyle.Regular), Brushes.Black, 1, i)
    NextMarker -= 10

    Next


    ' Step 4
    PB.Image = bmp
    ' Step 5
    gv.Dispose()
    End Sub

    Step 1 and Step 2 are the same as we have previously discussed, so I won't go through those again.

    Step 3 has the task of populating values into the PictureBox.

    • First, it creates a variable named NextMarker and assigns it an initial value of 100. This value reflects the Maximum value of the ScrollBar.
    • The second line in Step 3 sets up a loop. Because of the values of the Step used in the loop, this effectively means that the loop will run ten times.
    • Within the loop, DrawString is used to write the next marker value onto the Graphics object of the PictureBox. The first value to be written is 100, the next will be 90, then 80, etc. The reason for writing the values in descending order is of course that this procedure is working its way from the top to the bottom of the PictureBox and therefore we need the values to decrease with each step.
    • The next line of code simply reduces the value that we are going to display next. In this example we are reducing the value by 10 each time.
    • The loop ends with the Next keyword.

    Steps 4 and 5 are again simply repeats of the logic we used in previous drawing procedures.

    So there you have a fairly simple way of inserting a vertical list showing values.   Next, we need to consider a more realistic situation where the range of values that may need to be charted don't slot neatly into a preset and pre-scaled PictureBox.  

    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