Class: GnuplotRB::Animation

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

Overview

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

Options

Animations has several specific options:

  • animate - allows to get animated gif's. Possible values are true (just turn on animation), ot hash with suboptions (:loop - count of loops, default 0 - infinity$; :delay - delay between frames; :optimize - boolean, reduces file size).

  • size - size of gif file in pixels (size: [500, 500]) or (size: 500)

  • background - background color

  • transparent

  • enhanced

  • font

  • fontscale

  • crop

Animation ignores :term option and does not have methods like #to_png or #to_svg. One can also set animation any options related to Plot and they will be considered by all nested plots (if they does not override it with their own values).

Animation inherits all plot array handling methods from Multiplot and adds aliases for them (#plots -> #frames; #update_frame! -> #update_plot!; etc).

Instance Attribute Summary

Attributes inherited from Multiplot

#plots

Instance Method Summary collapse

Methods inherited from Multiplot

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

Methods included from Plottable

#method_missing, #option_name, #option_name!, #own_terminal, #respond_to?, #title, #title!, #to_canvas, #to_gif, #to_png, #to_svg, #xrange, #xrange!, #yrange, #yrange!

Methods included from OptionHandling

#initialize, #new_with_options, option_to_string, #options, #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) ⇒ nil, String

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

Options passed in #plot have priority over those which were set before.

Inner options of Plots have the highest priority (except :term and :output which are ignored).


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gnuplotrb/animation.rb', line 55

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.


84
85
86
87
# File 'lib/gnuplotrb/animation.rb', line 84

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


77
78
79
# File 'lib/gnuplotrb/animation.rb', line 77

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