Class: GnuplotRB::Animation

Inherits:
Multiplot show all
Defined in:
lib/gnuplotrb/animation.rb

Overview

Overview

Animation allows to create gif animation with given plots as frames. Possible frames: Plot, Splot, Multiplot. More about its usage in animation notebook.

Constant Summary

Constants included from OptionHandling

OptionHandling::QUOTED_OPTIONS

Instance Attribute Summary

Attributes inherited from Multiplot

#plots

Instance Method Summary collapse

Methods inherited from Multiplot

#[], #add_plots, #initialize, #remove_plot, #replace_plot, #update_plot

Methods included from Plottable

#method_missing, #own_terminal, #respond_to?

Methods included from OptionHandling

#initialize, #new_with_options, option_to_string, #options, ruby_class_to_gnuplot, string_key, valid_terminal?, validate_terminal_options

Constructor Details

This class inherits a constructor from GnuplotRB::Multiplot

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GnuplotRB::Plottable

Instance Method Details

#plot(path = nil, **options) ⇒ Object

Overview

This method creates a gif animation where frames are plots already contained by Animation object.

Arguments
  • term - Terminal to plot to

  • options - will be considered as 'settable' options of gnuplot ('set xrange [1:10]', 'set title 'plot'' etc)

Options passed here have priority over already existing. Inner options of Plots have the highest priority (except :term and :output which are ignored).


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gnuplotrb/animation.rb', line 28

def plot(path = nil, **options)
  options[:output] ||= path
  plot_options = mix_options(options) do |plot_opts, anim_opts|
    plot_opts.merge(term: ['gif', anim_opts])
  end.to_h
  need_output = plot_options[:output].nil?
  plot_options[:output] = Dir::Tmpname.make_tmpname('anim', 0) if need_output
  terminal = Terminal.new
  multiplot(terminal, plot_options)  # guaranteed wait for plotting to finish

  terminal.close
  if need_output
    result = File.binread(plot_options[:output])
    File.delete(plot_options[:output])
  else
    result = nil
  end
  result
end

#to_irubyObject

This method is used to embed gif animations into iRuby notebooks.


57
58
59
60
# File 'lib/gnuplotrb/animation.rb', line 57

def to_iruby
  gif_base64 = Base64.encode64(plot)
  ['text/html', "<img src=\"data:image/gif;base64, #{gif_base64}\">"]
end

#to_specific_term(*_) ⇒ Object

#to_<term_name> methods are not supported by animation


50
51
52
# File 'lib/gnuplotrb/animation.rb', line 50

def to_specific_term(*_)
  fail 'Specific terminals are not supported by Animation'
end