Class: Plot::Gnuplot

Inherits:
GenericPlot show all
Defined in:
lib/redshift/util/plot.rb

Constant Summary collapse

@@gnuplot_counter =
0

Instance Attribute Summary collapse

Attributes inherited from GenericPlot

#command_history

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GenericPlot

#add, #clear_data, #close, #dump, #show

Constructor Details

#initializeGnuplot

Returns a new instance of Gnuplot.



197
198
199
200
# File 'lib/redshift/util/plot.rb', line 197

def initialize(*)
  super
  @uniqname = next_uniqname
end

Instance Attribute Details

#is_to_fileObject (readonly)

Returns the value of attribute is_to_file.



230
231
232
# File 'lib/redshift/util/plot.rb', line 230

def is_to_file
  @is_to_file
end

#termObject



218
219
220
# File 'lib/redshift/util/plot.rb', line 218

def term
  @term ||= ENV["GNUTERM"] || best_term
end

#term_optionsObject

Returns the value of attribute term_options.



223
224
225
# File 'lib/redshift/util/plot.rb', line 223

def term_options
  @term_options
end

#uniqnameObject (readonly)

Returns the value of attribute uniqname.



195
196
197
# File 'lib/redshift/util/plot.rb', line 195

def uniqname
  @uniqname
end

Class Method Details

.version(app) ⇒ Object

Returns an array of the form [major, minor].



158
159
160
161
162
163
164
165
166
167
# File 'lib/redshift/util/plot.rb', line 158

def Gnuplot.version(app)
  @version ||= {}
  @version[app] ||=
    begin
      v = `#{app} --version`.match(/gnuplot\s+(\d+)\.(\d+)/)
      v && v[1..2].map {|s| s.to_i}
    rescue
      nil
    end
end

.version_at_least?(app, major_minor) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
# File 'lib/redshift/util/plot.rb', line 169

def Gnuplot.version_at_least?(app, major_minor)
  major, minor = Gnuplot.version(app)
  major_needed, minor_needed = major_minor
  (major == major_needed and minor >= minor_needed) or
    (major > major_needed)
end

Instance Method Details

#best_termObject

Select the best term choice based on platform and gnuplot version.



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/redshift/util/plot.rb', line 182

def best_term
  case RUBY_PLATFORM
  when /mswin32|mingw32/
    "win"
  else
    if Gnuplot.version_at_least?(@app, [4, 2])
      "wxt"
    else
      "x11"
    end
  end
end

#command(str) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/redshift/util/plot.rb', line 232

def command str
  case str
  when /^\s*set\s+term\s+(\S+)/
    self.term = $1
    if /title\s+['"]?([^'"]*)/ =~ str
      @uniqname = $1
    end
  when /^\s*set\s+output\s/
    @is_to_file = true
  end
  super
end

#commitObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/redshift/util/plot.rb', line 245

def commit
  args = @data_stuff.map do |data, options, path|
    if data
      "'#{dump data, path}' #{options}"
    else
      options
    end
  end

  cmd_line = [plot_cmd, args.join(", ")].join(" ")

  command "set mouse"
  command cmd_line
end

#has_working_close_eventObject

Does the specified gnuplot executable support the ‘Close’ event?



177
178
179
# File 'lib/redshift/util/plot.rb', line 177

def has_working_close_event
  Gnuplot.version_at_least?(@app, [4, 3])
end

#next_uniqnameObject



202
203
204
# File 'lib/redshift/util/plot.rb', line 202

def next_uniqname
  "Gnuplot_#{Process.pid}_#{@@gnuplot_counter+=1}"
end

#plot_cmdObject



214
215
216
# File 'lib/redshift/util/plot.rb', line 214

def plot_cmd
  @plot_cmd ||= "plot"
end

#set_window_title(title) ⇒ Object Also known as: window_title=



225
226
227
# File 'lib/redshift/util/plot.rb', line 225

def set_window_title title
  command "set term #{term} title '#{title}' #{term_options}"
end

#use2dObject



210
211
212
# File 'lib/redshift/util/plot.rb', line 210

def use2d
  @plot_cmd = "plot"
end

#use3dObject



206
207
208
# File 'lib/redshift/util/plot.rb', line 206

def use3d
  @plot_cmd = "splot"
end