Newbie Ques: Simple Line Chart

Am totally clueless with this graphics drawing thing. All i am trying to do is get a simple line chart drawn, adding new values to the chart on a timer event. Now I know how to draw a single line in a picturebox when the form starts up.

    Private Sub xyGraph_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles xyGraph.Paint
        Dim pn As Pen = New Pen(Color.Green, 2)
        Dim pt1 As Point = New Point(0, 0)
        Dim pt2 As Point = New Point(140, 100)
        e.Graphics.DrawLine(pn, pt1, pt2)
    End Sub

This so far works. But I haven't a clue how to keep firing this event and add new lines to the picturebox.
If there's any tutorials or advice out there it would be great. If I use some 3rd party charting control, I'll never learn anything. 


Answer this question

Newbie Ques: Simple Line Chart

  • Paul Bleisch

    Can you draw on top of things like a text box and radio buttons  

    For example, I have a form with a bunch of radio buttons, text boxes, progress bars, picture box, etc.... 

    I want to draw a filled circle and be able to move it around on top of the controls. Right now I'm using a transparent form. Can I just draw on top of controls by overriding the Paint event in the main form (with the controls)


  • jan schlagenhauf

    Hmm... as far as I know, I don't think you can draw on top of a picturebox. Actually I don't think you can draw on top of any control except Panels and UserControls. 

    If I wanted to create an updated line chart, I would:

    Create a usercontrol LineChart.
    have an array of lines you want to draw.
    OnPaint: draw all the lines.
    have a method AddLine(line)
    In AddLine, invalidate the control
    you won't need a timer since the control will always redisplay when lines are added. 


    good luck. any questions  

  • Spiral401

    That sounds great. I never played around with z-order. Thanks rkimble. 
  • le_mo_mo

    Yes, you can draw <b>on</b> those controls... the problem you are having is trying to draw <b>over</b> those controls.

    The trick you are using now is probably ok.  Another option might be to make a custom control that is added to the form, brought to the front, draws your image on its own surface, and then follows the mouse around by changing its position.  The control would appear over the other controls on the form because it has been brought to the front of the zorder.

  • raja.raj.raja

    Many thanks, to all of you. My simple Line Chart works great. Did it way rkimble suggested, so thanks a lot.
  • detrius67

    You can draw to any control that exposes the CreateGraphics method (the PictureBox being one of them).

    You also don't have to create a user control.  You can continue to use the Paint event of the picture box if you like.

    You just need to create a collection to hold the lines to be drawn (an ArrayList for example).  Your paint function could just clear the picture box and then enumerate the ArrayList and draw every line it contains.  Then you can have other code which modifies (adds or removes lines) the ArrayList and then calls Invalidate() on the picture box.

  • Mike Gold

    One could also call invalidate everytime a new value was added to the collection and hence it will redraw that controls surface. 

    Joe

  • Nbell

    You're quite welcome guys.

    Good luck to both of you!!

  • Newbie Ques: Simple Line Chart