home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / plot.tcl < prev    next >
Text File  |  1996-04-23  |  3KB  |  95 lines

  1. # plot.tcl --
  2. #
  3. # This demonstration script creates a canvas widget showing a 2-D
  4. # plot with data points that can be dragged with the mouse.
  5. #
  6. # @(#) plot.tcl 1.1 95/05/26 15:56:35
  7.  
  8. set w .plot
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "Plot Demonstration"
  12. wm iconname $w "Plot"
  13. positionWindow $w
  14. set c $w.c
  15.  
  16. label $w.msg -font $font -wraplength 4i -justify left -text "This window displays a canvas widget containing a simple 2-dimensional plot.  You can doctor the data by dragging any of the points with mouse button 1."
  17. pack $w.msg -side top
  18.  
  19. frame $w.buttons
  20. pack  $w.buttons -side bottom -expand y -fill x -pady 2m
  21. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  22. button $w.buttons.code -text "See Code" -command "showCode $w"
  23. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  24.  
  25. canvas $c -relief raised -width 450 -height 300
  26. pack $w.c -side top -fill x
  27.  
  28. set plotFont -*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*
  29.  
  30. $c create line 100 250 400 250 -width 2
  31. $c create line 100 250 100 50 -width 2
  32. $c create text 225 20 -text "A Simple Plot" -font $plotFont -fill brown
  33.  
  34. for {set i 0} {$i <= 10} {incr i} {
  35.     set x [expr {100 + ($i*30)}]
  36.     $c create line $x 250 $x 245 -width 2
  37.     $c create text $x 254 -text [expr 10*$i] -anchor n -font $plotFont
  38. }
  39. for {set i 0} {$i <= 5} {incr i} {
  40.     set y [expr {250 - ($i*40)}]
  41.     $c create line 100 $y 105 $y -width 2
  42.     $c create text 96 $y -text [expr $i*50].0 -anchor e -font $plotFont
  43. }
  44.  
  45. foreach point {{12 56} {20 94} {33 98} {32 120} {61 180}
  46.     {75 160} {98 223}} {
  47.     set x [expr {100 + (3*[lindex $point 0])}]
  48.     set y [expr {250 - (4*[lindex $point 1])/5}]
  49.     set item [$c create oval [expr $x-6] [expr $y-6] \
  50.         [expr $x+6] [expr $y+6] -width 1 -outline black \
  51.         -fill SkyBlue2]
  52.     $c addtag point withtag $item
  53. }
  54.  
  55. $c bind point <Any-Enter> "$c itemconfig current -fill red"
  56. $c bind point <Any-Leave> "$c itemconfig current -fill SkyBlue2"
  57. $c bind point <1> "plotDown $c %x %y"
  58. $c bind point <ButtonRelease-1> "$c dtag selected"
  59. bind $c <B1-Motion> "plotMove $c %x %y"
  60.  
  61. set plot(lastX) 0
  62. set plot(lastY) 0
  63.  
  64. # plotDown --
  65. # This procedure is invoked when the mouse is pressed over one of the
  66. # data points.  It sets up state to allow the point to be dragged.
  67. #
  68. # Arguments:
  69. # w -        The canvas window.
  70. # x, y -    The coordinates of the mouse press.
  71.  
  72. proc plotDown {w x y} {
  73.     global plot
  74.     $w dtag selected
  75.     $w addtag selected withtag current
  76.     $w raise current
  77.     set plot(lastX) $x
  78.     set plot(lastY) $y
  79. }
  80.  
  81. # plotMove --
  82. # This procedure is invoked during mouse motion events.  It drags the
  83. # current item.
  84. #
  85. # Arguments:
  86. # w -        The canvas window.
  87. # x, y -    The coordinates of the mouse.
  88.  
  89. proc plotMove {w x y} {
  90.     global plot
  91.     $w move selected [expr $x-$plot(lastX)] [expr $y-$plot(lastY)]
  92.     set plot(lastX) $x
  93.     set plot(lastY) $y
  94. }
  95.