Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Picture Box graphing with 1 value

Posted on 2000-03-13
Last Modified: 2010-05-02
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.

Question by:DaveMon
  • 4
  • 4

Expert Comment

ID: 2612722
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.

Author Comment

ID: 2612975
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


Accepted Solution

Lewy earned 100 total points
ID: 2614372
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.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


Author Comment

ID: 2618065

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.


Expert Comment

ID: 2619743
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

Author Comment

ID: 2624980
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.



Expert Comment

ID: 2628162
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.

Author Comment

ID: 2709718
Thanks  Lewy   sorry  I took  so  long  to  accept  your  answer

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using Access 8 75
How does CurrentUser work? 10 38
Macro Excel - Multiple If conditions 2 81
VBA: loop recent folder and copy txt file. 8 33
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question