plot.tcl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .plot
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Plot Demonstration"
  13. wm iconname $w "Plot"
  14. positionWindow $w
  15. set c $w.c
  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. ## See Code / Dismiss buttons
  19. set btns [addSeeDismiss $w.buttons $w]
  20. pack $btns -side bottom -fill x
  21. canvas $c -relief raised -width 450 -height 300
  22. pack $w.c -side top -fill x
  23. set plotFont {Helvetica 18}
  24. $c create line 100 250 400 250 -width 2
  25. $c create line 100 250 100 50 -width 2
  26. $c create text 225 20 -text "A Simple Plot" -font $plotFont -fill brown
  27. for {set i 0} {$i <= 10} {incr i} {
  28. set x [expr {100 + ($i*30)}]
  29. $c create line $x 250 $x 245 -width 2
  30. $c create text $x 254 -text [expr {10*$i}] -anchor n -font $plotFont
  31. }
  32. for {set i 0} {$i <= 5} {incr i} {
  33. set y [expr {250 - ($i*40)}]
  34. $c create line 100 $y 105 $y -width 2
  35. $c create text 96 $y -text [expr {$i*50}].0 -anchor e -font $plotFont
  36. }
  37. foreach point {
  38. {12 56} {20 94} {33 98} {32 120} {61 180} {75 160} {98 223}
  39. } {
  40. set x [expr {100 + (3*[lindex $point 0])}]
  41. set y [expr {250 - (4*[lindex $point 1])/5}]
  42. set item [$c create oval [expr {$x-6}] [expr {$y-6}] \
  43. [expr {$x+6}] [expr {$y+6}] -width 1 -outline black \
  44. -fill SkyBlue2]
  45. $c addtag point withtag $item
  46. }
  47. $c bind point <Enter> "$c itemconfig current -fill red"
  48. $c bind point <Leave> "$c itemconfig current -fill SkyBlue2"
  49. $c bind point <Button-1> "plotDown $c %x %y"
  50. $c bind point <ButtonRelease-1> "$c dtag selected"
  51. bind $c <B1-Motion> "plotMove $c %x %y"
  52. set plot(lastX) 0
  53. set plot(lastY) 0
  54. # plotDown --
  55. # This procedure is invoked when the mouse is pressed over one of the
  56. # data points. It sets up state to allow the point to be dragged.
  57. #
  58. # Arguments:
  59. # w - The canvas window.
  60. # x, y - The coordinates of the mouse press.
  61. proc plotDown {w x y} {
  62. global plot
  63. $w dtag selected
  64. $w addtag selected withtag current
  65. $w raise current
  66. set plot(lastX) $x
  67. set plot(lastY) $y
  68. }
  69. # plotMove --
  70. # This procedure is invoked during mouse motion events. It drags the
  71. # current item.
  72. #
  73. # Arguments:
  74. # w - The canvas window.
  75. # x, y - The coordinates of the mouse.
  76. proc plotMove {w x y} {
  77. global plot
  78. $w move selected [expr {$x-$plot(lastX)}] [expr {$y-$plot(lastY)}]
  79. set plot(lastX) $x
  80. set plot(lastY) $y
  81. }