3
# This demonstration script creates a canvas widget showing a 2-D
4
# plot with data points that can be dragged with the mouse.
6
# 2-D plot widget demo (called by 'widget')
10
if defined?($plot_demo) && $plot_demo
15
# demo toplevel widget
16
$plot_demo = TkToplevel.new {|w|
17
title("Plot Demonstration")
23
TkLabel.new($plot_demo, 'font'=>$font, 'wraplength'=>'4i', 'justify'=>'left',
24
'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."){
29
$plot_buttons = TkFrame.new($plot_demo) {|frame|
37
}.pack('side'=>'left', 'expand'=>'yes')
41
command proc{showCode 'plot'}
42
}.pack('side'=>'left', 'expand'=>'yes')
44
$plot_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
47
plotFont = '-*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*'
50
$plot_canvas = TkCanvas.new($plot_demo,'relief'=>'raised','width'=>450,'height'=>300)
51
$plot_canvas.pack('side'=>'top', 'fill'=>'x')
54
TkcLine.new($plot_canvas, 100, 250, 400, 250, 'width'=>2)
55
TkcLine.new($plot_canvas, 100, 250, 100, 50, 'width'=>2)
56
TkcText.new($plot_canvas, 225, 20,
57
'text'=>"A Simple Plot", 'font'=>plotFont, 'fill'=>'brown')
61
TkcLine.new($plot_canvas, x, 250, x, 245, 'width'=>2)
62
TkcText.new($plot_canvas, x, 254,
63
'text'=>10*i, 'font'=>plotFont, 'anchor'=>'n')
67
TkcLine.new($plot_canvas, 100, y, 105, y, 'width'=>2)
68
TkcText.new($plot_canvas, 96, y,
69
'text'=>"#{i*50}.0", 'font'=>plotFont, 'anchor'=>'e')
72
for xx, yy in [[12,56],[20,94],[33,98],[32,120],[61,180],[75,160],[98,223]]
75
item = TkcOval.new($plot_canvas, x-6, y-6, x+6, y+6,
76
'width'=>1, 'outline'=>'black', 'fill'=>'SkyBlue2')
80
$plot_canvas.itembind('point', 'Any-Enter',
81
proc{$plot_canvas.itemconfigure 'current','fill','red'})
82
$plot_canvas.itembind('point', 'Any-Leave',
83
proc{$plot_canvas.itemconfigure 'current','fill','SkyBlue2'})
84
$plot_canvas.itembind('point', '1',
85
proc{|x,y| plotDown $plot_canvas,x,y}, "%x %y")
86
$plot_canvas.itembind('point', 'ButtonRelease-1',
87
proc{$plot_canvas.dtag 'selected'})
88
$plot_canvas.bind('B1-Motion',
89
proc{|x,y| plotMove $plot_canvas,x,y}, "%x %y")
91
$plot = {'lastX'=>0, 'lastY'=>0}
94
# This method is invoked when the mouse is pressed over one of the
95
# data points. It sets up state to allow the point to be dragged.
98
# w - The canvas window.
99
# x, y - The coordinates of the mouse press.
101
def plotDown (w, x, y)
103
w.addtag_withtag 'selected', 'current'
110
# This method is invoked during mouse motion events. It drags the
114
# w - The canvas window.
115
# x, y - The coordinates of the mouse.
117
def plotMove (w, x, y)
118
w.move 'selected', x - $plot['lastX'], y - $plot['lastY']