rubypost 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/graph.rb CHANGED
@@ -1,312 +1,312 @@
1
-
2
- module RubyPost
3
-
4
- #wrapper for the metapost graphing macro
5
- #multiple coordinate systems in a single graph is not yet supported
6
- #May require pre and post options for graph data which doesn't have
7
- #a neat solution. You can ofcourse, still do this with a CustomDrawable.
8
- class Graph < Drawable
9
-
10
- #automattically sets the size of the graph to 10cm x 10cm
11
- def initialize
12
- super()
13
- @options = Array.new
14
- @dati = Array.new
15
- @@Inputs.add_input('graph')
16
- @@Inputs.add_input('sarith')
17
- self.set_size(10,10)
18
- end
19
-
20
- def add_data(d)
21
- @dati.push(d)
22
- end
23
-
24
- #set the size of the graph in cm
25
- def set_size(w,h)
26
- @width = w
27
- @height = h
28
- end
29
-
30
- #add a graph option such as it Title, Xlabel etc.
31
- def add_graph_option(o)
32
- @options.push(o)
33
- end
34
-
35
- #utility function to compile all the graph options
36
- def compile_options
37
- str = String.new
38
- @options.each { |o| str = str + ' ' + o.compile }
39
- return str
40
- end
41
-
42
- def compile
43
- str = "begingraph(" + @width.to_s + "cm, " + @height.to_s + "cm);\n"
44
- str = str + compile_options + "\n"
45
- @dati.each do |d|
46
- str = str + d.compile_pre_commands + "gdraw " + d.compile + d.compile_post_commands + "\n"
47
- end
48
- str = str + "endgraph"
49
- str
50
- end
51
-
52
- end
53
-
54
- #Base class for all graph data. There are some commands, such as autogrid
55
- #that must be called after the data is entered with gdraw. There are others that
56
- #must be set before the call, such as setcoords. These macro ensure that order
57
- #is maintained
58
- class BaseGraphData < BaseDrawCommand
59
-
60
- def initialize
61
- super()
62
- @pre_commands = Array.new
63
- @post_commands = Array.new
64
- end
65
-
66
- def add_pre_command(c)
67
- @pre_commands.push(c)
68
- end
69
-
70
- def add_post_command(c)
71
- @post_commands.push(c)
72
- end
73
-
74
- def compile_pre_commands
75
- str = String.new
76
- @pre_commands.each { |c| str = str + c.compile }
77
- return str
78
- end
79
-
80
- #add a grid to the graph. This is always done as a post command
81
- #in metapost
82
- def add_grid(grid)
83
- add_post_command(grid)
84
- end
85
-
86
- def add_range(range)
87
- add_pre_command(range)
88
- end
89
-
90
- def add_coords(coords)
91
- add_pre_command(coords)
92
- end
93
-
94
- #add a label to this graph data
95
- def add_label(coords)
96
- add_post_command(coords)
97
- end
98
-
99
- def compile_post_commands
100
- str = String.new
101
- @post_commands.each { |c| str = str + c.compile }
102
- return str
103
- end
104
-
105
- end
106
-
107
- #Implements the graph data filename macro
108
- class GraphFile < BaseGraphData
109
-
110
- attr_writer :filename
111
-
112
- def initialize(filename=nil)
113
- super()
114
- @filename = filename
115
- end
116
-
117
- def compile
118
- "\"" + @filename + "\" " + compile_options + ";"
119
- end
120
-
121
- end
122
-
123
- #set the x and y coordinates arrays independently
124
- #This uses temp files in order to store and create
125
- #the graph. Graphs created with this type of data
126
- #need to be compiled directly to postscript using
127
- #RubyPost::File#compile_to_ps unless you want to
128
- #copy all of the temporay files too!
129
- class GraphData < GraphFile
130
-
131
- attr_writer :x, :y
132
-
133
- #class variable to store the number of the temporary
134
- #files used and keep the filenames separate
135
- @@graph_data_temp_file = 0
136
-
137
- #must make a copy of the arrays as it will be compiled
138
- #at some latter time and the referenced memory may not
139
- #be around then!
140
- def initialize(x=Array.new,y=Array.new)
141
- super()
142
- @x = Array.new(x)
143
- @y = Array.new(y)
144
- @filename = 'temp_graph_data_' + @@graph_data_temp_file.to_s + '.txt'
145
- @@graph_data_temp_file = @@graph_data_temp_file+1
146
- end
147
-
148
- #creates the tempory file with the graph data and send this to the
149
- #metapost file. Also note that we force scientific notation for the
150
- #number format as it is ,most compatible with metapost graph.
151
- def compile
152
- min = [@x.length, @y.length].min
153
- IO::File.open(@filename, 'w') do |f|
154
- min.times { |i| f.puts sprintf("%e", @x[i]) + ' ' + sprintf("%e", @y[i]) }
155
- end
156
- "\"" + @filename + "\" " + compile_options + ";"
157
- end
158
-
159
- end
160
-
161
-
162
- #class for the special options related to graph paths
163
- class GraphDataOption < PathOption
164
- end
165
-
166
- #wraps the plot option for the graph macro
167
- class Plot < GraphDataOption
168
-
169
- attr_writer :dot_type
170
-
171
- #default is a latex bullet
172
- def initialize(p='btex $\bullet$ etex')
173
- @dot_type = p
174
- end
175
-
176
- def compile
177
- 'plot ' + @dot_type.compile
178
- end
179
-
180
- end
181
-
182
- #Option extention for graphs
183
- class GraphOption < Option
184
- end
185
-
186
- #set the axis coords, can be log or linear scale
187
- class Coords < GraphOption
188
-
189
- def initialize(x='linear',y='linear')
190
- @x = x
191
- @y = y
192
- end
193
-
194
- def loglog
195
- @x = 'log'
196
- @y = 'log'
197
- end
198
-
199
- def linearlinear
200
- @x = 'log'
201
- @y = 'log'
202
- end
203
-
204
- def linearlog
205
- @x = 'linear'
206
- @y = 'log'
207
- end
208
-
209
- def loglinear
210
- @x = 'log'
211
- @y = 'linear'
212
- end
213
-
214
- def compile
215
- 'setcoords(' + @x + ',' + @y + ");\n"
216
- end
217
-
218
- end
219
-
220
-
221
- #wraps the setrange function in metapost
222
- #can use strings or numbers.
223
- class Range < GraphOption
224
-
225
- attr_writer :xmin, :xmax, :ymin, :ymax
226
-
227
- def initialize(xmin='whatever', ymin='whatever', xmax='whatever', ymax='whatever')
228
- @xmin = xmin
229
- @xmax = xmax
230
- @ymin = ymin
231
- @ymax = ymax
232
- end
233
-
234
- def compile
235
- 'setrange(' + @xmin.to_s + ', ' + @ymin.to_s + ', ' + @xmax.to_s + ', ' + @ymax.to_s + ");\n"
236
- end
237
-
238
- end
239
-
240
-
241
- #set the position and type of grid tick marks and labels
242
- class AutoGrid < Drawable
243
-
244
- attr_writer :x, :y
245
-
246
- def initialize(x=String.new, y=String.new)
247
- super()
248
- @x = x
249
- @y = y
250
- end
251
-
252
- def compile
253
- 'autogrid(' + @x + ', ' + @y + ")" + compile_options + ";\n"
254
- end
255
-
256
- end
257
-
258
- #base class for graph labels
259
- class GraphLabel < BaseLabel
260
-
261
- def initialize(t=nil,p=0)
262
- super(t,p)
263
- self
264
- end
265
-
266
- def compile
267
- 'glabel.' + @place + '(' + @text.compile + ', ' + @position.compile + ') ' + compile_options + ";\n"
268
- end
269
-
270
- end
271
-
272
- #place an x label on the graph
273
- #overrides and position and place settings
274
- class XLabel < GraphLabel
275
-
276
- def compile
277
- 'glabel.bot(' + @text + ", OUT)" + compile_options + ";\n"
278
- end
279
-
280
- end
281
-
282
- #place an y label on the graph
283
- #overrides and position and place settings
284
- class YLabel < GraphLabel
285
-
286
- def compile
287
- 'glabel.lft(' + @text + ", OUT)" + compile_options + ";\n"
288
- end
289
-
290
- end
291
-
292
- #place an y label on the right hand side of the graph
293
- #overrides and position and place settings
294
- class YLabelRight < GraphLabel
295
-
296
- def compile
297
- 'glabel.rt(' + @text + ", OUT)" + compile_options + ";\n"
298
- end
299
-
300
- end
301
-
302
- #place a title on the top of the graph
303
- #overrides and position and place settings
304
- class GraphTitle < GraphLabel
305
-
306
- def compile
307
- 'glabel.top(' + @text + ", OUT)" + compile_options + ";\n"
308
- end
309
-
310
- end
311
-
1
+
2
+ module RubyPost
3
+
4
+ #wrapper for the metapost graphing macro
5
+ #multiple coordinate systems in a single graph is not yet supported
6
+ #May require pre and post options for graph data which doesn't have
7
+ #a neat solution. You can ofcourse, still do this with a CustomDrawable.
8
+ class Graph < Drawable
9
+
10
+ #automattically sets the size of the graph to 10cm x 10cm
11
+ def initialize
12
+ super()
13
+ @options = Array.new
14
+ @dati = Array.new
15
+ @@Inputs.add_input('graph')
16
+ @@Inputs.add_input('sarith')
17
+ self.set_size(10,10)
18
+ end
19
+
20
+ def add_data(d)
21
+ @dati.push(d)
22
+ end
23
+
24
+ #set the size of the graph in cm
25
+ def set_size(w,h)
26
+ @width = w
27
+ @height = h
28
+ end
29
+
30
+ #add a graph option such as it Title, Xlabel etc.
31
+ def add_graph_option(o)
32
+ @options.push(o)
33
+ end
34
+
35
+ #utility function to compile all the graph options
36
+ def compile_options
37
+ str = String.new
38
+ @options.each { |o| str = str + ' ' + o.compile }
39
+ return str
40
+ end
41
+
42
+ def compile
43
+ str = "begingraph(" + @width.to_s + "cm, " + @height.to_s + "cm);\n"
44
+ str = str + compile_options + "\n"
45
+ @dati.each do |d|
46
+ str = str + d.compile_pre_commands + "gdraw " + d.compile + d.compile_post_commands + "\n"
47
+ end
48
+ str = str + "endgraph"
49
+ str
50
+ end
51
+
52
+ end
53
+
54
+ #Base class for all graph data. There are some commands, such as autogrid
55
+ #that must be called after the data is entered with gdraw. There are others that
56
+ #must be set before the call, such as setcoords. These macro ensure that order
57
+ #is maintained
58
+ class BaseGraphData < BaseDrawCommand
59
+
60
+ def initialize
61
+ super()
62
+ @pre_commands = Array.new
63
+ @post_commands = Array.new
64
+ end
65
+
66
+ def add_pre_command(c)
67
+ @pre_commands.push(c)
68
+ end
69
+
70
+ def add_post_command(c)
71
+ @post_commands.push(c)
72
+ end
73
+
74
+ def compile_pre_commands
75
+ str = String.new
76
+ @pre_commands.each { |c| str = str + c.compile }
77
+ return str
78
+ end
79
+
80
+ #add a grid to the graph. This is always done as a post command
81
+ #in metapost
82
+ def add_grid(grid)
83
+ add_post_command(grid)
84
+ end
85
+
86
+ def add_range(range)
87
+ add_pre_command(range)
88
+ end
89
+
90
+ def add_coords(coords)
91
+ add_pre_command(coords)
92
+ end
93
+
94
+ #add a label to this graph data
95
+ def add_label(coords)
96
+ add_post_command(coords)
97
+ end
98
+
99
+ def compile_post_commands
100
+ str = String.new
101
+ @post_commands.each { |c| str = str + c.compile }
102
+ return str
103
+ end
104
+
105
+ end
106
+
107
+ #Implements the graph data filename macro
108
+ class GraphFile < BaseGraphData
109
+
110
+ attr_writer :filename
111
+
112
+ def initialize(filename=nil)
113
+ super()
114
+ @filename = filename
115
+ end
116
+
117
+ def compile
118
+ "\"" + @filename + "\" " + compile_options + ";"
119
+ end
120
+
121
+ end
122
+
123
+ #set the x and y coordinates arrays independently
124
+ #This uses temp files in order to store and create
125
+ #the graph. Graphs created with this type of data
126
+ #need to be compiled directly to postscript using
127
+ #RubyPost::File#compile_to_ps unless you want to
128
+ #copy all of the temporay files too!
129
+ class GraphData < GraphFile
130
+
131
+ attr_writer :x, :y
132
+
133
+ #class variable to store the number of the temporary
134
+ #files used and keep the filenames separate
135
+ @@graph_data_temp_file = 0
136
+
137
+ #must make a copy of the arrays as it will be compiled
138
+ #at some latter time and the referenced memory may not
139
+ #be around then!
140
+ def initialize(x=Array.new,y=Array.new)
141
+ super()
142
+ @x = Array.new(x)
143
+ @y = Array.new(y)
144
+ @filename = 'temp_graph_data_' + @@graph_data_temp_file.to_s + '.txt'
145
+ @@graph_data_temp_file = @@graph_data_temp_file+1
146
+ end
147
+
148
+ #creates the tempory file with the graph data and send this to the
149
+ #metapost file. Also note that we force scientific notation for the
150
+ #number format as it is ,most compatible with metapost graph.
151
+ def compile
152
+ min = [@x.length, @y.length].min
153
+ IO::File.open(@filename, 'w') do |f|
154
+ min.times { |i| f.puts sprintf("%e", @x[i]) + ' ' + sprintf("%e", @y[i]) }
155
+ end
156
+ "\"" + @filename + "\" " + compile_options + ";"
157
+ end
158
+
159
+ end
160
+
161
+
162
+ #class for the special options related to graph paths
163
+ class GraphDataOption < PathOption
164
+ end
165
+
166
+ #wraps the plot option for the graph macro
167
+ class Plot < GraphDataOption
168
+
169
+ attr_writer :dot_type
170
+
171
+ #default is a latex bullet
172
+ def initialize(p='btex $\bullet$ etex')
173
+ @dot_type = p
174
+ end
175
+
176
+ def compile
177
+ 'plot ' + @dot_type.compile
178
+ end
179
+
180
+ end
181
+
182
+ #Option extention for graphs
183
+ class GraphOption < Option
184
+ end
185
+
186
+ #set the axis coords, can be log or linear scale
187
+ class Coords < GraphOption
188
+
189
+ def initialize(x='linear',y='linear')
190
+ @x = x
191
+ @y = y
192
+ end
193
+
194
+ def loglog
195
+ @x = 'log'
196
+ @y = 'log'
197
+ end
198
+
199
+ def linearlinear
200
+ @x = 'log'
201
+ @y = 'log'
202
+ end
203
+
204
+ def linearlog
205
+ @x = 'linear'
206
+ @y = 'log'
207
+ end
208
+
209
+ def loglinear
210
+ @x = 'log'
211
+ @y = 'linear'
212
+ end
213
+
214
+ def compile
215
+ 'setcoords(' + @x + ',' + @y + ");\n"
216
+ end
217
+
218
+ end
219
+
220
+
221
+ #wraps the setrange function in metapost
222
+ #can use strings or numbers.
223
+ class Range < GraphOption
224
+
225
+ attr_writer :xmin, :xmax, :ymin, :ymax
226
+
227
+ def initialize(xmin='whatever', ymin='whatever', xmax='whatever', ymax='whatever')
228
+ @xmin = xmin
229
+ @xmax = xmax
230
+ @ymin = ymin
231
+ @ymax = ymax
232
+ end
233
+
234
+ def compile
235
+ 'setrange(' + @xmin.to_s + ', ' + @ymin.to_s + ', ' + @xmax.to_s + ', ' + @ymax.to_s + ");\n"
236
+ end
237
+
238
+ end
239
+
240
+
241
+ #set the position and type of grid tick marks and labels
242
+ class AutoGrid < Drawable
243
+
244
+ attr_writer :x, :y
245
+
246
+ def initialize(x=String.new, y=String.new)
247
+ super()
248
+ @x = x
249
+ @y = y
250
+ end
251
+
252
+ def compile
253
+ 'autogrid(' + @x + ', ' + @y + ")" + compile_options + ";\n"
254
+ end
255
+
256
+ end
257
+
258
+ #base class for graph labels
259
+ class GraphLabel < BaseLabel
260
+
261
+ def initialize(t=nil,p=0)
262
+ super(t,p)
263
+ self
264
+ end
265
+
266
+ def compile
267
+ 'glabel.' + @place + '(' + @text.compile + ', ' + @position.compile + ') ' + compile_options + ";\n"
268
+ end
269
+
270
+ end
271
+
272
+ #place an x label on the graph
273
+ #overrides and position and place settings
274
+ class XLabel < GraphLabel
275
+
276
+ def compile
277
+ 'glabel.bot(' + @text + ", OUT)" + compile_options + ";\n"
278
+ end
279
+
280
+ end
281
+
282
+ #place an y label on the graph
283
+ #overrides and position and place settings
284
+ class YLabel < GraphLabel
285
+
286
+ def compile
287
+ 'glabel.lft(' + @text + ", OUT)" + compile_options + ";\n"
288
+ end
289
+
290
+ end
291
+
292
+ #place an y label on the right hand side of the graph
293
+ #overrides and position and place settings
294
+ class YLabelRight < GraphLabel
295
+
296
+ def compile
297
+ 'glabel.rt(' + @text + ", OUT)" + compile_options + ";\n"
298
+ end
299
+
300
+ end
301
+
302
+ #place a title on the top of the graph
303
+ #overrides and position and place settings
304
+ class GraphTitle < GraphLabel
305
+
306
+ def compile
307
+ 'glabel.top(' + @text + ", OUT)" + compile_options + ";\n"
308
+ end
309
+
310
+ end
311
+
312
312
  end
data/lib/label.rb CHANGED
File without changes