Module: GnuplotRB::Plottable

Includes:
OptionHandling
Included in:
Dataset, Multiplot, Plot
Defined in:
lib/gnuplotrb/mixins/plottable.rb

Overview

This module contains methods that should be mixed into plottable classes. It includes OptionHandling and implements several plotting methods.

Instance Method Summary collapse

Methods included from OptionHandling

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_id, *args) ⇒ Object

In this gem #method_missing is used both to handle options and to handle plotting to specific terminal.

Options handling

Overview

You may set options using #option_name(option_value) method. A new object will be constructed with selected option set. And finally you can get current value of any option using #options_name without arguments.

Arguments

  • option_value - value to set an option. If none given method will just return current option's value

Examples

plot = Splot.new
new_plot = plot.title('Awesome plot')
plot.title #=> nil
new_plot.title #=> 'Awesome plot'

Plotting to specific term

Overview

Gnuplot offers possibility to output graphics to many image formats. The easiest way to to so is to use #to_<plot_name> methods.

Arguments

  • options - set of options related to terminal (size, font etc). Be careful, some terminals have their own specific options.

Examples

# font options specific for png term
multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
# font options specific for svg term
content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gnuplotrb/mixins/plottable.rb', line 47

def method_missing(meth_id, *args)
  meth = meth_id.id2name
  case
  when meth[0..2] == 'to_'
    term = meth[3..-1]
    super unless OptionHandling.valid_terminal?(term)
    to_specific_term(term, *args)
  when meth[-1] == '!'
    option!(meth[0..-2].to_sym, *args)
  when meth[-1] == '='
    option!(meth[0..-2].to_sym, *args)
    option(meth[0..-2].to_sym)
  else
    option(meth_id, *args)
  end
end

Instance Method Details

#option_name(value = nil) ⇒ Object

Clone existing object and set new options value in created one or just return existing value if nil given.

Method is handled by #method_missing.

You may set options using #option_name(option_value) method. A new object will be constructed with selected option set. And finally you can get current value of any option using #options_name without arguments.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
new_plot = plot.title('Awesome plot')
plot.title #=> nil
new_plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 127


#option_name!(value) ⇒ Object

Set value for an option.

Method is handled by #method_missing.

You may set options using obj.option_name!(option_value) or obj.option_name = option_value methods.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
plot.title #=> nil
plot.title!('Awesome plot')
plot.title #=> 'Awesome plot'
plot = Splot.new
plot.title #=> nil
plot.title = 'Awesome plot'
plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 154


#own_terminalTerminal


123
124
125
# File 'lib/gnuplotrb/mixins/plottable.rb', line 123

def own_terminal
  @terminal ||= Terminal.new
end

#plot(*_) ⇒ Object

You should implement #plot in classes that are Plottable


12
13
14
# File 'lib/gnuplotrb/mixins/plottable.rb', line 12

def plot(*_)
  fail NotImplementedError, 'You should implement #plot in classes that are Plottable!'
end

#respond_to?(meth_id, include_all = false) ⇒ true, false


68
69
70
71
72
73
74
75
# File 'lib/gnuplotrb/mixins/plottable.rb', line 68

def respond_to?(meth_id, include_all=false)
  # Next line is here to force iRuby use #to_iruby
  # instead of #to_svg.
  return super if defined? IRuby
  meth = meth_id.id2name
  term = meth[0..2] == 'to_' && OptionHandling.valid_terminal?(meth[3..-1])
  term || super
end

#title(value = nil) ⇒ Object

Clone existing object and set new options value in created one or just return existing value if nil given.

Method is handled by #method_missing.

You may set options using #option_name(option_value) method. A new object will be constructed with selected option set. And finally you can get current value of any option using #options_name without arguments.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
new_plot = plot.title('Awesome plot')
plot.title #=> nil
new_plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 127


#title!(value) ⇒ Object

Set value for an option.

Method is handled by #method_missing.

You may set options using obj.option_name!(option_value) or obj.option_name = option_value methods.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
plot.title #=> nil
plot.title!('Awesome plot')
plot.title #=> 'Awesome plot'
plot = Splot.new
plot.title #=> nil
plot.title = 'Awesome plot'
plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 154


#to_canvas(path = nil, **options) ⇒ String

Output to plot to according image format.

All of #to_|terminal_name| methods are handled with #method_missing.

Gnuplot offers possibility to output graphics to many image formats. The easiest way to to so is to use #to_<plot_name> methods.

Examples:

# font options specific for png term
multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
# font options specific for svg term
content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)

# File 'lib/gnuplotrb/mixins/plottable.rb', line 183


#to_gif(path = nil, **options) ⇒ String

Output to plot to according image format.

All of #to_|terminal_name| methods are handled with #method_missing.

Gnuplot offers possibility to output graphics to many image formats. The easiest way to to so is to use #to_<plot_name> methods.

Examples:

# font options specific for png term
multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
# font options specific for svg term
content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)

# File 'lib/gnuplotrb/mixins/plottable.rb', line 183


#to_irubyObject

This method is used to embed plottable objects into iRuby notebooks. There is a notebook with examples of its usage.


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gnuplotrb/mixins/plottable.rb', line 81

def to_iruby
  available_terminals = {
    'png'      => 'image/png',
    'pngcairo' => 'image/png',
    'jpeg'     => 'image/jpeg',
    'svg'      => 'image/svg+xml',
    'dumb'     => 'text/plain'
  }
  terminal, options = term.is_a?(Array) ? [term[0], term[1]] : [term, {}]
  terminal = 'svg' unless available_terminals.keys.include?(terminal)
  [available_terminals[terminal], send("to_#{terminal}".to_sym, **options)]
end

#to_png(path = nil, **options) ⇒ String

Output to plot to according image format.

All of #to_|terminal_name| methods are handled with #method_missing.

Gnuplot offers possibility to output graphics to many image formats. The easiest way to to so is to use #to_<plot_name> methods.

Examples:

# font options specific for png term
multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
# font options specific for svg term
content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)

# File 'lib/gnuplotrb/mixins/plottable.rb', line 183


#to_specific_term(terminal, path = nil, **options) ⇒ Object

Output plot to specific terminal (possibly some file). Explicit use should be avoided. This method is called from #method_missing when it handles method names like #to_png(options).

Examples:

## plot here may be Plot, Splot, Multiplot or any other plottable class
plot.to_png('./result.png', size: [300, 500])
contents = plot.to_svg(size: [100, 100])
plot.to_dumb('./result.txt', size: [30, 15])

109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gnuplotrb/mixins/plottable.rb', line 109

def to_specific_term(terminal, path = nil, **options)
  if path
    result = plot(term: [terminal, options], output: path)
  else
    path = Dir::Tmpname.make_tmpname(terminal, 0)
    plot(term: [terminal, options], output: path)
    result = File.binread(path)
    File.delete(path)
  end
  result
end

#to_svg(path = nil, **options) ⇒ String

Output to plot to according image format.

All of #to_|terminal_name| methods are handled with #method_missing.

Gnuplot offers possibility to output graphics to many image formats. The easiest way to to so is to use #to_<plot_name> methods.

Examples:

# font options specific for png term
multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
# font options specific for svg term
content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)

# File 'lib/gnuplotrb/mixins/plottable.rb', line 183


#xrange(value = nil) ⇒ Object

Clone existing object and set new options value in created one or just return existing value if nil given.

Method is handled by #method_missing.

You may set options using #option_name(option_value) method. A new object will be constructed with selected option set. And finally you can get current value of any option using #options_name without arguments.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
new_plot = plot.title('Awesome plot')
plot.title #=> nil
new_plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 127


#xrange!(value) ⇒ Object

Set value for an option.

Method is handled by #method_missing.

You may set options using obj.option_name!(option_value) or obj.option_name = option_value methods.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
plot.title #=> nil
plot.title!('Awesome plot')
plot.title #=> 'Awesome plot'
plot = Splot.new
plot.title #=> nil
plot.title = 'Awesome plot'
plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 154


#yrange(value = nil) ⇒ Object

Clone existing object and set new options value in created one or just return existing value if nil given.

Method is handled by #method_missing.

You may set options using #option_name(option_value) method. A new object will be constructed with selected option set. And finally you can get current value of any option using #options_name without arguments.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
new_plot = plot.title('Awesome plot')
plot.title #=> nil
new_plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 127


#yrange!(value) ⇒ Object

Set value for an option.

Method is handled by #method_missing.

You may set options using obj.option_name!(option_value) or obj.option_name = option_value methods.

Available options are listed in Plot, Splot, Multiplot etc class top level doc.

Examples:

plot = Splot.new
plot.title #=> nil
plot.title!('Awesome plot')
plot.title #=> 'Awesome plot'
plot = Splot.new
plot.title #=> nil
plot.title = 'Awesome plot'
plot.title #=> 'Awesome plot'

# File 'lib/gnuplotrb/mixins/plottable.rb', line 154