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/doc/created.rid +1 -0
- data/doc/fr_class_index.html +77 -0
- data/doc/fr_file_index.html +35 -0
- data/doc/fr_method_index.html +142 -0
- data/doc/index.html +24 -0
- data/lib/draw.rb +115 -0
- data/lib/drawable.rb +153 -185
- data/lib/graph.rb +222 -211
- data/lib/objects.rb +132 -98
- data/lib/options.rb +127 -127
- data/lib/revert_float_to_s.rb +6 -6
- data/lib/rubypost.rb +1 -0
- data/tests/{test_cricle.rb → test_circle.rb} +4 -5
- data/tests/test_graph.rb +13 -10
- data/tests/test_pair.rb +2 -4
- data/tests/test_path.rb +46 -62
- data/tests/test_picture.rb +26 -0
- data/tests/test_square.rb +15 -15
- metadata +13 -4
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
|
-
|
|
11
|
-
|
|
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
|
-
|
|
15
|
-
|
|
15
|
+
@@Inputs.add_input('graph')
|
|
16
|
+
@@Inputs.add_input('sarith')
|
|
16
17
|
self.set_size(10,10)
|
|
17
|
-
|
|
18
|
+
end
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
def add_data(d)
|
|
21
|
+
@dati.push(d)
|
|
22
|
+
end
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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
|
|
48
|
+
str = str + "endgraph"
|
|
36
49
|
str
|
|
37
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
66
|
+
def add_pre_command(c)
|
|
67
|
+
@pre_commands.push(c)
|
|
68
|
+
end
|
|
56
69
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
70
|
+
def add_post_command(c)
|
|
71
|
+
@post_commands.push(c)
|
|
72
|
+
end
|
|
60
73
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
def add_range(range)
|
|
87
|
+
add_pre_command(range)
|
|
88
|
+
end
|
|
76
89
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
90
|
+
def add_coords(coords)
|
|
91
|
+
add_pre_command(coords)
|
|
92
|
+
end
|
|
80
93
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
107
|
+
def initialize(filename=nil)
|
|
108
|
+
super()
|
|
109
|
+
@filename = filename
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def compile
|
|
113
|
+
"\"" + @filename + "\" " + compile_options + ";"
|
|
123
114
|
end
|
|
124
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
#
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
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
|
-
|
|
164
|
+
attr_writer :dot_type
|
|
154
165
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
166
|
+
#default is a latex bullet
|
|
167
|
+
def initialize(p='btex $\bullet$ etex')
|
|
168
|
+
@dot_type = p
|
|
169
|
+
end
|
|
159
170
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
184
|
+
def initialize(x='linear',y='linear')
|
|
185
|
+
@x = x
|
|
186
|
+
@y = y
|
|
187
|
+
end
|
|
177
188
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
189
|
+
def loglog
|
|
190
|
+
@x = 'log'
|
|
191
|
+
@y = 'log'
|
|
192
|
+
end
|
|
182
193
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
194
|
+
def linearlinear
|
|
195
|
+
@x = 'log'
|
|
196
|
+
@y = 'log'
|
|
197
|
+
end
|
|
187
198
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
199
|
+
def linearlog
|
|
200
|
+
@x = 'linear'
|
|
201
|
+
@y = 'log'
|
|
202
|
+
end
|
|
192
203
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
204
|
+
def loglinear
|
|
205
|
+
@x = 'log'
|
|
206
|
+
@y = 'linear'
|
|
207
|
+
end
|
|
197
208
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
220
|
+
attr_writer :xmin, :xmax, :ymin, :ymax
|
|
210
221
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
-
|
|
239
|
+
attr_writer :x, :y
|
|
229
240
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
241
|
+
def initialize(x=String.new, y=String.new)
|
|
242
|
+
super()
|
|
243
|
+
@x = x
|
|
244
|
+
@y = y
|
|
245
|
+
end
|
|
235
246
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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
|
-
|
|
256
|
+
attr_writer :label
|
|
246
257
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
284
|
-
|
|
285
|
-
|
|
294
|
+
def compile
|
|
295
|
+
'glabel.top(' + @label + ", OUT);\n"
|
|
296
|
+
end
|
|
286
297
|
|
|
287
|
-
end
|
|
298
|
+
end
|
|
288
299
|
|
|
289
300
|
end
|