~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/demos-en/plot.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# plot.rb
 
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
# 2-D plot widget demo (called by 'widget')
 
7
#
 
8
 
 
9
# toplevel widget
 
10
if defined?($plot_demo) && $plot_demo
 
11
  $plot_demo.destroy 
 
12
  $plot_demo = nil
 
13
end
 
14
 
 
15
# demo toplevel widget
 
16
$plot_demo = TkToplevel.new {|w|
 
17
  title("Plot Demonstration")
 
18
  iconname("Plot")
 
19
  positionWindow(w)
 
20
}
 
21
 
 
22
# label
 
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."){
 
25
  pack('side'=>'top')
 
26
}
 
27
 
 
28
# frame
 
29
$plot_buttons = TkFrame.new($plot_demo) {|frame|
 
30
  TkButton.new(frame) {
 
31
    text 'Dismiss'
 
32
    command proc{
 
33
      tmppath = $plot_demo
 
34
      $plot_demo = nil
 
35
      tmppath.destroy
 
36
    }
 
37
  }.pack('side'=>'left', 'expand'=>'yes')
 
38
 
 
39
  TkButton.new(frame) {
 
40
    text 'Show Code'
 
41
    command proc{showCode 'plot'}
 
42
  }.pack('side'=>'left', 'expand'=>'yes')
 
43
}
 
44
$plot_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
 
45
 
 
46
# font 
 
47
plotFont = '-*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*'
 
48
 
 
49
# canvas 
 
50
$plot_canvas = TkCanvas.new($plot_demo,'relief'=>'raised','width'=>450,'height'=>300)
 
51
$plot_canvas.pack('side'=>'top', 'fill'=>'x')
 
52
 
 
53
# plot 
 
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')
 
58
 
 
59
(0..10).each {|i|
 
60
  x = 100 + (i * 30)
 
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')
 
64
}
 
65
(0..5).each {|i|
 
66
  y = 250 - (i * 40)
 
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')
 
70
}
 
71
 
 
72
for xx, yy in [[12,56],[20,94],[33,98],[32,120],[61,180],[75,160],[98,223]]
 
73
  x = 100 + (3*xx)
 
74
  y = 250 - (4*yy)/5
 
75
  item = TkcOval.new($plot_canvas, x-6, y-6, x+6, y+6, 
 
76
                     'width'=>1, 'outline'=>'black', 'fill'=>'SkyBlue2')
 
77
  item.addtag 'point'
 
78
end
 
79
 
 
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")
 
90
 
 
91
$plot = {'lastX'=>0, 'lastY'=>0}
 
92
 
 
93
# plotDown --
 
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.
 
96
#
 
97
# Arguments:
 
98
# w -           The canvas window.
 
99
# x, y -        The coordinates of the mouse press.
 
100
 
 
101
def plotDown (w, x, y)
 
102
  w.dtag 'selected'
 
103
  w.addtag_withtag 'selected', 'current'
 
104
  w.raise 'current'
 
105
  $plot['lastX'] = x
 
106
  $plot['lastY'] = y
 
107
end
 
108
 
 
109
# plotMove --
 
110
# This method is invoked during mouse motion events.  It drags the
 
111
# current item.
 
112
#
 
113
# Arguments:
 
114
# w -           The canvas window.
 
115
# x, y -        The coordinates of the mouse.
 
116
 
 
117
def plotMove (w, x, y)
 
118
  w.move 'selected', x - $plot['lastX'], y - $plot['lastY']
 
119
  $plot['lastX'] = x
 
120
  $plot['lastY'] = y
 
121
end
 
122