Experimenting with canvas and scrollbar

Arjen Markus (20 august 2008) With Plotchart you can create time charts and Gantt charts with any number of items. But the screen may not be large enough to hold all of them. So scrolling up and down would be nice. Provided of course that the title and the time axis at the top remain visible!

With a bit of juggling the canvas items and the help of a scrollbar this is - in principle - not difficult to implement. I thought the example script might be useful for someone with a similar problem.


# scroll_canvas.tcl --
#     Experiment with scrolling canvas items selectively
#

canvas .c -width 400 -height 200
scrollbar .y -command scrollc

grid .c .y -sticky news

.c create text 200 10 -text "Title"
.c lower [.c create rectangle 0 0 400 30 -fill white -outline {} ]

.c lower [.c create text 10 50 -text "Item" -anchor w -tag Move]
.c lower [.c create polygon 80 50 140 50 140 350 -fill green -tag Move]

# Set the parameters for the scrollbar
.y set 0.0 0.5
set currentPos 0.0

proc scrollc {operation number {unit ""}} {
    # Ignore scroll operation
    if { $operation == "moveto" } {
        set dely [expr {400*($::currentPos-$number)}]
        set ::currentPos $number
        .c move Move 0 $dely
    }
}

Screenshots

Experimenting with canvas and scrollbar screen.png

gold added pix