Class: GnuPlotter

Inherits:
Object
  • Object
show all
Defined in:
lib/gnuplotter.rb,
lib/gnuplotter/version.rb

Defined Under Namespace

Classes: DataSet

Constant Summary collapse

NOQUOTE =

Quotes around the values of the below keys are stripped.

[
  :auto,
  :autoscale,
  :cbrange,
  :border,
  :boxwidth,
  :datafile,
  :grid,
  :key,
  :linetype,
  :logscale,
  :nocbtics,
  :palette,
  :rtics,
  :terminal,
  :tic,
  :style,
  :view,
  :yrange,
  :ytics,
  :xrange,
  :xtics,
  :ztics
]
VERSION =
"1.0.2"

Instance Method Summary collapse

Constructor Details

#initializeGnuPlotter

Constructor method for a GnuPlot object.



59
60
61
62
# File 'lib/gnuplotter.rb', line 59

def initialize
  @options  = Hash.new { |h1, k1| h1[k1] = Hash.new { |h2, k2| h2[k2] = [] } }
  @datasets = []
end

Instance Method Details

#add_dataset(options = {}) {|dataset| ... } ⇒ Object

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

Yields:

  • (dataset)

Raises:



109
110
111
112
113
114
115
116
# 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

#plotObject

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



120
121
122
# File 'lib/gnuplotter.rb', line 120

def plot
  gnuplot("plot")
end

#set(options) ⇒ Object

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).

Raises:



67
68
69
70
71
72
73
74
75
# 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

#splotObject

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



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

def splot
  gnuplot("splot")
end

#to_gp(cmd = "plot") ⇒ Object

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object

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

Raises:



79
80
81
82
83
84
85
86
87
# 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