class GnuPlotter

Constants

NOQUOTE

Quotes around the values of the below keys are stripped.

VERSION

Public Class Methods

new() click to toggle source

Constructor method for a GnuPlot object.

# File lib/gnuplotter.rb, line 59
def initialize
  @options  = Hash.new { |h1, k1| h1[k1] = Hash.new { |h2, k2| h2[k2] = [] } }
  @datasets = []
end

Public Instance Methods

add_dataset(options = {}) { |dataset| ... } click to toggle source

Method to add a dataset to the current GnuPlot.

add_dataset(using: "1:2:3:4", with: "vectors nohead", title: "'bar'") do |plotter|
  data.map { |d| plotter << d }
end
# File lib/gnuplotter.rb, line 109
def add_dataset(options = {})
  raise GnuPlotterError, "No block given" unless block_given?

  dataset = DataSet.new(options)
  @datasets << dataset

  yield dataset
end
plot() click to toggle source

Method to execute the plotting of added datasets. Any plot data, i.e. dumb terminal, is returned.

# File lib/gnuplotter.rb, line 120
def plot
  gnuplot("plot")
end
set(options) click to toggle source

Method to set an option in the GnuPlot environment e.g: set(title: “Nobel Prize”) set(grid: :true) # note no such thing as set(grid).

# File lib/gnuplotter.rb, line 67
def set(options)
  raise GnuPlotterError, "Non-hash options given" unless options.is_a? Hash

  options.each do |key, value|
    @options[:set][key.to_sym] << value
  end

  self
end
splot() click to toggle source

Method to execute the splotting of added datasets. Any plot data, i.e. dumb terminal, is returned.

# File lib/gnuplotter.rb, line 126
def splot
  gnuplot("splot")
end
to_gp(cmd = "plot") click to toggle source

Method that returns a GnuPlot script as a list of lines.

# File lib/gnuplotter.rb, line 90
def to_gp(cmd = "plot")
  if ! @datasets.empty?
    data_lines = []

    @datasets.each do |dataset|
      data_lines.push(*dataset.format_data, "e")
      dataset.delete
    end

    plot_settings + [data_settings(cmd, true)] + data_lines
  else
    plot_settings
  end
end
unset(options) click to toggle source

Method to unset an option in the GnuPlot environment e.g: unset(ytics: true) # note no such thing as unset(ytics).

# File lib/gnuplotter.rb, line 79
def unset(options)
  raise GnuPlotterError, "Non-hash options given" unless options.is_a? Hash

  options.each do |key, value|
    @options[:unset][key.to_sym] << value || true
  end

  self
end

Private Instance Methods

data_settings(method, input = nil) click to toggle source

Returns one comma seperated line with plot settings for each dataset.

# File lib/gnuplotter.rb, line 186
def data_settings(method, input = nil)
  list = []

  @datasets.each do |dataset|
    list << dataset.format_options(input)
  end

  "#{method} " + list.join(", ")
end
gnuplot(method) click to toggle source

Method that calls gnuplot via open3 and performs the plotting. Any plot data, i.e. dumb terminal, is returned.

# File lib/gnuplotter.rb, line 134
def gnuplot(method)
  @datasets.each { |dataset| dataset.close }

  result = nil

  Open3.popen3("gnuplot -persist") do |stdin, stdout, stderr, wait_thr|
    plot_settings.each { |line| stdin.puts line }

    if @datasets.empty?
      stdin.puts "#{method} 1/0"
    else
      stdin.puts data_settings(method)
    end

    stdin.close
    result = stdout.read
    stdout.close

    exit_status = wait_thr.value

    unless exit_status.success?
      raise GnuPlotterError, stderr.read
    end
  end

  @datasets.each { |dataset| dataset.delete }

  result
end
plot_settings() click to toggle source

Returns a list of lines with the plot settings.

# File lib/gnuplotter.rb, line 165
def plot_settings
  lines = []

  @options.each do |method, options|
    options.each do |key, list|
      list.each do |value|
        if value == :true or value === true
          lines << %Q{#{method} #{key}}
        elsif NOQUOTE.include? key.to_sym
          lines << %Q{#{method} #{key} #{value}}
        else
          lines << %Q{#{method} #{key} "#{value}"}
        end
      end
    end
  end

  lines
end