Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scrollable canvas - like a ticker tape

Status
Not open for further replies.

ajaayman

Programmer
Feb 14, 2003
12
0
0
US
I am trying to do the following:
Create a scrollable canvas using Tk. On the scrollable canvas, plot a graph using X-Y coordinates. X is essentially time while Y is a number. As the time progresses, the new numbers arriving have to be plotted.
Two things have to happen:
1. The view of the canvas has to change as a new point is drawn since a new number has arrived.
2. The X axis has to change its time value. For example, if a new number came in at 11:30:00 and is plotted, the number that was plotted at the time 09:30:00 has to disappear from the canvas.
Like a ticker tape...
This last part is the one I wrestling with. Can someone help me or point me in the right direction?

Thanks.
 
Here is an example:
Code:
  canvas .c -width 200 -height 120 -xscrollincrement 1
  pack .c
  bind . <KeyPress> { set ::stop 1 }
  bind . <ButtonPress> { set ::stop 1 }
  bind . <Destroy> { exit }
  set ::stop 0
  set ::t 0
  while {!$::stop}   {
    update
    after 100
    set v [expr {int(sin($::t / 24.0) * 50) + 60}]
    .c create line $::t $v [incr ::t] [incr v]
    if {$::t > 200} { .c xview scroll 1 unit }
  }
HTH

ulis
 
Trust me, you really want to use the Blt extension rather than writing your own. Blt has a stripchart widget that does exactly what you want. It'll be much, much easier than trying to write your own, and it will have many, many more features than you can even begin to think of now. For more information on Blt, check out the Tcl'ers Wiki ( specifically the &quot;Blt&quot; page,
Here's a really quick example showing just a few of the features of the stripchart widget:

Code:
package require BLT

# Create a stripchart widget and pack it to the screen.

blt::stripchart .s
pack .s -expand yes -fill both

# We'll be using Blt &quot;vectors&quot; to store our information.
# Vectors are efficient data storage objects that can hold
# an arbitrary number of floating-point data points. Blt
# provides several different vector operations for
# manipulating data. It also allows you to hook up vectors
# to Blt widgets such as the stripchart, which will then
# automatically update themselves whenever the vector data
# changes.

blt::vector create x
blt::vector create y

# The X axis is our time axis. We'll display 60 seconds
# worth of data, scrolling in 1-second intervals. We'll
# provide a command, FormatTick, to format the raw x value,
# which will be a seconds-based time value in this case,
# into a more readable form for human consumption.

.s axis configure x -autorange 60 -shiftby 1     -command FormatTick

proc FormatTick {widget x} {
    return [clock format $x -format &quot;%I:%M:%S&quot;]
}

# Display Y data ranging from -1 to +1

.s axis configure y -min -4 -max 4

# We'll show our data plot as a smoothed line graph,
# without symbols at the data points.

.s element create rawData -symbol {} -color blue     -smooth natural -xdata x -ydata y

# Automatically generate data every second and add the
# data to our vectors, which automatically updates our
# stripchart.

proc click {} {
    global x y

    # Append the latest data as new values
    # at the end of our vectors.

    set x(++end) [clock seconds]
    
    set len [y length]
    if {$len == 0} {
        set y(++end) [expr { rand() - 0.5 }]
    } else {
        set y(++end) [expr { rand() - 0.5 + $y(end)}]
    }

    # Schedule the next invocation of click

    after 1000 click
}

# Start the clicks going

click
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top