rubypost 0.0.1 → 0.0.2

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