Class: Gnuplot::Plot
- Inherits:
-
Object
- Object
- Gnuplot::Plot
- Defined in:
- lib/gnuplot.rb
Overview
Holds command information and performs the formatting of that command information to a GNUPLOT process. When constructing a new plot for GNUPLOT, this is the first object that must be instantiated. On this object set the various properties and add data sets.
Direct Known Subclasses
Constant Summary collapse
- QUOTED =
%w[title output xlabel ylabel]
Instance Attribute Summary collapse
-
#arbitrary_lines ⇒ Object
Returns the value of attribute arbitrary_lines.
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#data(*args, &block) ⇒ Object
If
args
is non-empty orblock
is given, calls #add_data. -
#io ⇒ Object
Returns the value of attribute io.
-
#sets ⇒ Object
Returns the value of attribute sets.
Instance Method Summary collapse
-
#<<(line) ⇒ Object
Adds an arbitrary
line
to write to the GNUPLOT process. -
#[](var) ⇒ Object
Return the current value of the GNUPLOT variable
var
. -
#add_data(data = nil, &block) ⇒ Object
Adds the DataSet
data
to the GNUPLOT process’s data sets. -
#initialize(io = nil, cmd = 'plot') {|_self| ... } ⇒ Plot
constructor
A new instance of Plot.
-
#method_missing(method, *args) ⇒ Object
Invoke the set method on the plot using the name of the invoked method as the set variable and any arguments that have been passed as the value.
-
#plot!(verbose = $VERBOSE, io = $stderr) ⇒ Object
Sends the data and commands to the GNUPLOT process, drawing the plot.
-
#set(var, value = '') ⇒ Object
(also: #[]=)
Set a variable to the given
value
. -
#to_gplot(io = io) ⇒ Object
Sends the GNUPLOT data and commands to
io
.
Constructor Details
#initialize(io = nil, cmd = 'plot') {|_self| ... } ⇒ Plot
Returns a new instance of Plot.
102 103 104 105 106 107 108 |
# File 'lib/gnuplot.rb', line 102 def initialize(io = nil, cmd = 'plot') @io, @cmd, @data, @sets, @arbitrary_lines = io, cmd, [], [], [] yield self if block_given? plot! if io end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Invoke the set method on the plot using the name of the invoked method as the set variable and any arguments that have been passed as the value. See the #set method for more details.
126 127 128 |
# File 'lib/gnuplot.rb', line 126 def method_missing(method, *args) set(method, *args) end |
Instance Attribute Details
#arbitrary_lines ⇒ Object
Returns the value of attribute arbitrary_lines.
100 101 102 |
# File 'lib/gnuplot.rb', line 100 def arbitrary_lines @arbitrary_lines end |
#cmd ⇒ Object
Returns the value of attribute cmd.
100 101 102 |
# File 'lib/gnuplot.rb', line 100 def cmd @cmd end |
#data(*args, &block) ⇒ Object
If args
is non-empty or block
is given, calls #add_data. Returns the current data otherwise.
152 153 154 |
# File 'lib/gnuplot.rb', line 152 def data @data end |
#io ⇒ Object
Returns the value of attribute io.
100 101 102 |
# File 'lib/gnuplot.rb', line 100 def io @io end |
#sets ⇒ Object
Returns the value of attribute sets.
100 101 102 |
# File 'lib/gnuplot.rb', line 100 def sets @sets end |
Instance Method Details
#<<(line) ⇒ Object
Adds an arbitrary line
to write to the GNUPLOT process. Returns self
to allow chaining.
165 166 167 168 |
# File 'lib/gnuplot.rb', line 165 def <<(line) @arbitrary_lines << line.to_s self end |
#[](var) ⇒ Object
Return the current value of the GNUPLOT variable var
. This will return the setting that is currently in the instance, not one that’s been given to the GNUPLOT process.
145 146 147 148 |
# File 'lib/gnuplot.rb', line 145 def [](var) assoc = @sets.assoc(var.to_s) assoc.last if assoc end |
#add_data(data = nil, &block) ⇒ Object
Adds the DataSet data
to the GNUPLOT process’s data sets. If data
is not already a DataSet object, DataSet.new will be called with data
and block
as arguments.
159 160 161 |
# File 'lib/gnuplot.rb', line 159 def add_data(data = nil, &block) @data.push(data.is_a?(DataSet) ? data : DataSet.new(data, &block)) end |
#plot!(verbose = $VERBOSE, io = $stderr) ⇒ Object
Sends the data and commands to the GNUPLOT process, drawing the plot.
Prints the plot commands that are sent to the GNUPLOT process to io
if verbose
is true
.
114 115 116 117 118 119 120 121 |
# File 'lib/gnuplot.rb', line 114 def plot!(verbose = $VERBOSE, io = $stderr) if verbose && io io << "Writing this to GNUPLOT:\n" to_gplot(io) end to_gplot end |
#set(var, value = '') ⇒ Object Also known as: []=
Set a variable to the given value
. var
must be a GNUPLOT variable and value
must be the value to set it to. Automatic quoting will be performed if the variable requires it (see QUOTED).
133 134 135 136 137 138 |
# File 'lib/gnuplot.rb', line 133 def set(var, value = '') var = var.to_s value = %Q{"#{value}"} if QUOTED.include?(var) && value !~ /\A'.*'\z/ @sets << [var, value] end |
#to_gplot(io = io) ⇒ Object
Sends the GNUPLOT data and commands to io
.
171 172 173 |
# File 'lib/gnuplot.rb', line 171 def to_gplot(io = io) to_plot(io) { |ds| ds.to_gplot(io) } end |