Class: Gnuplot::Plot

Inherits:
Object
  • Object
show all
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

SPlot

Constant Summary collapse

QUOTED =
%w[title output xlabel ylabel]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil, cmd = 'plot') {|_self| ... } ⇒ Plot

Returns a new instance of Plot.

Yields:

  • (_self)

Yield Parameters:

  • _self (Gnuplot::Plot)

    the object that the method was called on



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_linesObject

Returns the value of attribute arbitrary_lines.



100
101
102
# File 'lib/gnuplot.rb', line 100

def arbitrary_lines
  @arbitrary_lines
end

#cmdObject

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

#ioObject

Returns the value of attribute io.



100
101
102
# File 'lib/gnuplot.rb', line 100

def io
  @io
end

#setsObject

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