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 =
[ "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



88
89
90
91
92
93
94
95
96
# File 'lib/gnuplot.rb', line 88

def initialize (io = nil, cmd = "plot")
  @cmd = cmd
  @sets = []
  @arbitrary_lines = []
  @data = []
  yield self if block_given?
  puts "writing this to gnuplot:\n" + to_gplot + "\n" if $VERBOSE    
  io << to_gplot if io
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methId, *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.



103
104
105
# File 'lib/gnuplot.rb', line 103

def method_missing( methId, *args )
  set methId.id2name, *args
end

Instance Attribute Details

#arbitrary_linesObject

Returns the value of attribute arbitrary_lines.



97
98
99
# File 'lib/gnuplot.rb', line 97

def arbitrary_lines
  @arbitrary_lines
end

#cmdObject

Returns the value of attribute cmd.



84
85
86
# File 'lib/gnuplot.rb', line 84

def cmd
  @cmd
end

#dataObject

Returns the value of attribute data.



84
85
86
# File 'lib/gnuplot.rb', line 84

def data
  @data
end

#setsObject

Returns the value of attribute sets.



84
85
86
# File 'lib/gnuplot.rb', line 84

def sets
  @sets
end

Instance Method Details

#[](var) ⇒ Object

Return the current value of the variable. This will return the setting that is currently in the instance, not one that’s been given to a gnuplot process.



125
126
127
128
# File 'lib/gnuplot.rb', line 125

def [] ( var )
  v = @sets.assoc( var )
  v[1] || nil
end

#add_data(ds) ⇒ Object



131
132
133
# File 'lib/gnuplot.rb', line 131

def add_data ( ds )
  @data << ds
end

#set(var, value = "") ⇒ Object

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.

This is overloaded by the method_missing method so see that for more readable code.



115
116
117
118
# File 'lib/gnuplot.rb', line 115

def set ( var, value = "" )
  value = "\"#{value}\"" if QUOTED.include? var unless value =~ /^'.*'$/
  @sets << [ var, value ]
end

#to_gplot(io = "") ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gnuplot.rb', line 136

def to_gplot (io = "")
  @sets.each { |var, val| io << "set #{var} #{val}\n" }
  @arbitrary_lines.each{|line| io << line << "\n" }

  if @data.size > 0 then
    io << @cmd << " " << @data.collect { |e| e.plot_args }.join(", ")
    io << "\n"

    v = @data.collect { |ds| ds.to_gplot }
  	io << v.compact.join("e\n")
  end

  io
end