Picture Box graphing with 1 value

Hi  All,
I need help with the code to chart with a picture box control,  i have an  app  that has a textbox that is updated in real time with the temperature...I now  want  a real time graph to enhance it.  I would  just  like  a single  line  plot  to  show   the trends  of  the  values  that  have  come through  the textbox. I think updating the line once per minute would be sufficient, and  Can it be coded  so the old  data  just  "drops off" as the new data is added? I am not worried  about saving the  data  at  this  point.   I would  like  to hear  all   comments,  and  would  really  like  some  code  snippets  to assist  me  with  this.   Also,   If  it  would be  easier,  I will save  the data  to a text  file or  db,  then  run a sort of "report"  to produce a graph.  I have looked  at  and  played  with the code  at  Mark2150's  website,  and  Im  sure  its  great  but  I dont  think  I wanted to do  that  much,...as  I only  have  one  value to plot.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It sounds like you want something like a strip chart recorder, with the plot drawn as the paper moves across.
First decide how large the Image will be. What is the range of the value? Let’s say your value is a temperature between 32 and 100 degrees F. That’s a difference of 68. If you use a graph with one degree equal to one pixel then the resolution of the graph will be one degree and the graph will need to show 69 pixels. I used pixels because the display is based on pixels and to use other scale modes requires additional computations to resolve. If the final objective were to print the graph then it would be necessary to use a different scale mode.
Now the time base should be decided. Will the one minute cause the graph to move one pixel? Then how wide should the graph be? 60 pixels would be one hour.
To plot the points (readings), you would use the draw line function. You wold use the last point and the current point to draw the line. This will fill in any jumps in the value.
    OldValue = NewValue
Use the BitBlt to move the graph (chart). Move the Y starting at 0 and a width of one less than the width of the time base to position 1. This will shift the chart one pixel to the right. The column for Y=0 is blank. Think of this as the supply of blank chart paper.
DaveMonAuthor Commented:
that sounds exactly like I want.....I was wondering,  wouldnt  it  be beneficial for me  if  I could just  start  a sample  type  project with a textbox,   add  my  graph code,  and  then  verify  it's  working  by  changing  the textbox values  manually ( within 32-100 of course )  then  I could enter a value in the textbox, wait 1 minute  then observe the plotting of the line, as well as the movment of the "graph paper". After that  is going  well  I can transport the code to my real project and use the real time value from the textbox there.....
One more thing,  I am kinda new at this, and Im not sure how to begin coding for this, could I convince you to give me a rough outline of the code that will need to be there? I wont be picky, but something to get me started would be great as  I am not at all familiar with the Bitblt method, or with how to manipulate last point to current point. In fact,  I only  understand the PictureBox1.Drawline  method  and  Im sketchy  on that.  

thanks  Already

Yes. It's a good idea to test in a seperate project.

Add the following to a modual:
    'see http://www.vbapi.com/ref/b/bitblt.html for info on BitBlt
    Declare Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As Long, ByVal nXDest As Long, ByVal nYDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hdcSrc As Long, ByVal nXSrc As Long, ByVal nYSrc As Long, ByVal dwRop As Long) As Long
    Public Const SRCCOPY = &HCC0020

Add the following to a Form:
Private Sub Timer1_Timer()
    Static OldValue As Double
    Dim NewValue As Double
    Dim retval As Long  ' return value
    NewValue = Picture1.Height - ((Val(Text1.Text) - 32) / (100 - 32) * Picture1.Height)  'proper scaling
    Picture1.Line (2, OldValue)-(1, NewValue), Picture1.ForeColor
    OldValue = NewValue
    retval = BitBlt(Picture1.hDC, 1, 0, Picture1.Width - 1, Picture1.Height, Picture1.hDC, 0, 0, SRCCOPY)
    Timer1.Enabled = True
End Sub

Let me know how it works.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

DaveMonAuthor Commented:

Sorry  to be a pain,  but  this  code  appears  to  make  a  solid  sheet  of  color  move from  left  to right. of the Picture box control, regardless  of the value used  ( I called the function  with  the  change  text  event )  Is  that incorrect? ( for testing, of course )  are there  any  settings  for the timer control  that  I need to specify?  Can   you  tell me  how  this  graph  should be  started with code  ( the real app   already  has the textbox   being  updated in real time...( I borrowed the code in case youre  wondering..heheh )  But  I do  want   to say  thanks your  post  has  gotten me  to within sight  of the  finish  line.

Besure to set the following at design time or in the load event:

    Form1.ScaleMode = vbPixels '3 - smallest unit of monitor
    Picture1.ScaleMode = vbPixels
    Picture1.AutoRedraw = True 'Enables automatic repainting using the image stored in memory.
    Timer1.Interval = 60000
DaveMonAuthor Commented:
Just  one more thing  Lewy,   I put  all of  this  in my  "real "  application  and  it  does  begin,  but I tied the graph  to the text_changed event,  and  I noticed  that  when  the temp  doesnt  change...the  graph  just   sits  there...I was  really  hoping   it  would  continue moving one pixel width per second and rise  and  fall according to the textbox  value....I have  checked  and  rechecked  all of the values you told me to code ...they  all seem fine...  can you think of a way I can  have the graph   constantly  moving  from left  to right?    I will  wait  to hear from you on  this  then  I will award  the points.


You must use a timer to process the graph at the interval you want. That establishes the Time Base.
If the value doesn’t change it still needs to be drawn to create the “flat” line.
There is a problem using a text box to manually enter a value. If in editing the value you remove a digit to be replaced by another digit say 56 to 57, at one point you have a 5 in the text box. If the graph is drawn at this point in time, well it’s not what you want.
Let me know if you need more help.
DaveMonAuthor Commented:
Thanks  Lewy   sorry  I took  so  long  to  accept  your  answer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.