gnuplot 1.0 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/gnuplot.rb +245 -0
  2. metadata +4 -3
@@ -0,0 +1,245 @@
1
+ # Methods and variables for interacting with the gnuplot process. Most of
2
+ # these methods are for sending data to a gnuplot process, not for reading from
3
+ # it. Most of the methods are implemented as added methods to the built in
4
+ # classes.
5
+
6
+ require 'matrix'
7
+
8
+ module Gnuplot
9
+
10
+ # Open a gnuplot process that exists in the current PATH. If the persist
11
+ # flag is true then the -persist flag is added to the command line. The
12
+ # path to the gnuplot executable is determined using the 'which' command.
13
+ #
14
+ # See the gnuplot documentation for information on the persist flag.
15
+ #
16
+ # <b>todo</b> Add a method to pass the gnuplot path to the function.
17
+
18
+ def Gnuplot.open(persist=true)
19
+ cmd = ""
20
+ IO.popen( "which gnuplot" ) { |io| cmd = io.readline.chomp }
21
+ cmd += " -persist" if persist
22
+
23
+ IO::popen(cmd, "w") { |io| yield io }
24
+ end
25
+
26
+
27
+ # Holds command information and performs the formatting of that command
28
+ # information to a Gnuplot process. When constructing a new plot for
29
+ # gnuplot, this is the first object that must be instantiated. On this
30
+ # object set the various properties and add data sets.
31
+
32
+ class Plot
33
+ attr_accessor :cmd, :data, :sets
34
+
35
+ QUOTED = [ "title", "output", "xlabel", "ylabel" ]
36
+
37
+ def initialize (io = nil, cmd = "plot")
38
+ @cmd = cmd
39
+ @sets = []
40
+ @data = []
41
+ yield self if block_given?
42
+
43
+ io << to_gplot if io
44
+ end
45
+
46
+ # Invoke the set method on the plot using the name of the invoked method
47
+ # as the set variable and any arguments that have been passed as the
48
+ # value. See the +set+ method for more details.
49
+
50
+ def method_missing( methId, *args )
51
+ set methId.id2name, *args
52
+ end
53
+
54
+
55
+ # Set a variable to the given value. +Var+ must be a gnuplot variable and
56
+ # +value+ must be the value to set it to. Automatic quoting will be
57
+ # performed if the variable requires it.
58
+ #
59
+ # This is overloaded by the +method_missing+ method so see that for more
60
+ # readable code.
61
+
62
+ def set ( var, value = "" )
63
+ value = "'#{value}'" if QUOTED.include? var unless value =~ /^'.*'$/
64
+ @sets << [ var, value ]
65
+ end
66
+
67
+
68
+ # Return the current value of the variable. This will return the setting
69
+ # that is currently in the instance, not one that's been given to a
70
+ # gnuplot process.
71
+
72
+ def [] ( var )
73
+ v = @sets.assoc( var )
74
+ v[1] || nil
75
+ end
76
+
77
+
78
+ def add_data ( ds )
79
+ @data << ds
80
+ end
81
+
82
+
83
+ def to_gplot (io = "")
84
+ @sets.each { |var, val| io << "set #{var} #{val}\n" }
85
+
86
+ if @data.size > 0 then
87
+ io << @cmd << " " << @data.collect { |e| e.plot_args }.join(", ")
88
+ io << "\n"
89
+
90
+ v = @data.collect { |ds| ds.to_gplot }
91
+ io << v.compact.join("e\n")
92
+ end
93
+
94
+ io
95
+ end
96
+ end
97
+
98
+
99
+ class SPlot < Plot
100
+
101
+ def initialize (io = nil, cmd = "splot")
102
+ super
103
+ end
104
+
105
+ def to_gplot (io = "")
106
+ @sets.each { |var, val| io << "set #{var} #{val}\n" }
107
+
108
+ if @data.size > 0 then
109
+ io << @cmd << " "
110
+ io << @data.collect { |e| e.plot_args }.join(", ")
111
+ io << "\n"
112
+
113
+ @data.each do |ds|
114
+ io << ds.to_gsplot << "e\n"
115
+ end
116
+ end
117
+
118
+ io
119
+ end
120
+
121
+ end
122
+
123
+
124
+
125
+ # Container for a single dataset being displayed by gnuplot. Each object
126
+ # has a reference to the actual data being plotted as well as settings that
127
+ # control the "plot" command. The data object must support the to_gplot
128
+ # command.
129
+ #
130
+ # +data+ The data that will be plotted. The only requirement is that the
131
+ # object understands the to_gplot method.
132
+ #
133
+ # The following attributes correspond to their related string in the gnuplot
134
+ # command. See the gnuplot documentation for more information on this.
135
+ #
136
+ # title, with
137
+ #
138
+ # @todo Use the delegator to delegate to the data property.
139
+
140
+ class DataSet
141
+ attr_accessor :title, :with, :using, :data, :linewidth, :matrix
142
+
143
+ def initialize (data = nil)
144
+ @data = data
145
+ yield self if block_given?
146
+ end
147
+
148
+ def notitle
149
+ @title = "notitle"
150
+ end
151
+
152
+ def plot_args (io = "")
153
+
154
+ # Order of these is important or gnuplot barfs on 'em
155
+
156
+ io << ( (@data.instance_of? String) ? @data : "'-'" )
157
+
158
+ io << " using #{@using}" if @using
159
+
160
+ io << case @title
161
+ when /notitle/ then " notitle"
162
+ when nil then ""
163
+ else " title '#{@title}'"
164
+ end
165
+
166
+ io << " matrix" if @matrix
167
+ io << " with #{@with}" if @with
168
+ io << " linewidth #{@linewidth}" if @linewidth
169
+ io
170
+ end
171
+
172
+ def to_gplot
173
+ case @data
174
+ when nil then nil
175
+ when String then nil
176
+ else @data.to_gplot
177
+ end
178
+ end
179
+
180
+ def to_gsplot
181
+ case @data
182
+ when nil then nil
183
+ when String then nil
184
+ else @data.to_gplot
185
+ end
186
+ end
187
+
188
+ end
189
+ end
190
+
191
+ class Array
192
+ def to_gplot
193
+ if ( self[0].kind_of? Array ) then
194
+ tmp = self[0].zip( *self[1..-1] )
195
+ tmp.collect { |a| a.join(" ") }.join("\n") + "\n"
196
+ else
197
+ s = ""
198
+ self.length.times { |i| s << "#{self[i]}\n" }
199
+ s
200
+ end
201
+ end
202
+
203
+ def to_gsplot
204
+ f = ""
205
+
206
+ if ( self[0].kind_of? Array ) then
207
+ x = self[0]
208
+ y = self[1]
209
+ d = self[2]
210
+
211
+ x.each_with_index do |xv, i|
212
+ y.each_with_index do |yv, j|
213
+ f << [ xv, yv, d[i][j] ].join(" ") << "\n"
214
+ end
215
+ # f << "\n"
216
+ end
217
+ else
218
+ self.length.times do |i|
219
+ f << "#{self[i]}\n"
220
+ end
221
+ end
222
+
223
+ f
224
+ end
225
+ end
226
+
227
+ class Matrix
228
+ def to_gplot (x = nil, y = nil)
229
+ xgrid = x || (0...self.column_size).to_a
230
+ ygrid = y || (0...self.row_size).to_a
231
+
232
+ f = ""
233
+ ygrid.length.times do |j|
234
+ y = ygrid[j]
235
+ xgrid.length.times do |i|
236
+ if ( self[j,i] ) then
237
+ f << "#{xgrid[i]} #{y} #{self[j,i]}\n"
238
+ end
239
+ end
240
+ end
241
+
242
+ f
243
+ end
244
+
245
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.1
3
3
  specification_version: 1
4
4
  name: gnuplot
5
5
  version: !ruby/object:Gem::Version
6
- version: "1.0"
7
- date: 2004-09-20
6
+ version: "2.0"
7
+ date: 2004-11-11
8
8
  summary: Utility library to aid in interacting with gnuplot
9
9
  require_paths:
10
10
  - lib
@@ -25,7 +25,8 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  version: 0.0.0
26
26
  version:
27
27
  platform: ruby
28
- files: []
28
+ files:
29
+ - lib/gnuplot.rb
29
30
  test_files: []
30
31
  rdoc_options: []
31
32
  extra_rdoc_files: []