Class: GnuplotRB::Dataset

Inherits:
Object
  • Object
show all
Includes:
Plottable
Defined in:
lib/gnuplotrb/staff/dataset.rb

Overview

Overview

Dataset keeps control of Datablock or String (some math functions like this 'x*sin(x)' or filename) and options related to original dataset in gnuplot (with, title, using etc).

Constant Summary collapse

OPTION_ORDER =

Order is significant for some options

%w(index using axes title)
INIT_HANDLERS =

Hash of init handlers for data given in different containers.

Hash.new(:init_default).merge(
  String =>          :init_string,
  Datablock =>       :init_dblock
)

Constants included from OptionHandling

OptionHandling::QUOTED_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Plottable

#method_missing, #own_terminal, #respond_to?, #to_iruby, #to_specific_term

Methods included from OptionHandling

option_to_string, #options, ruby_class_to_gnuplot, string_key, valid_terminal?, validate_terminal_options

Constructor Details

#initialize(data, **options) ⇒ Dataset

Overview

Creates new dataset out of given string with math function or filename. If data isn't a string it will create datablock to store data.

Parameters
  • data - String, Datablock or something acceptable by Datablock.new as data (e.g. [x,y] where x and y are arrays)

  • options - hash of options specific for gnuplot dataset, and some special options ('file: true' will make data to be stored inside temporary file).

Examples

Math function:

Dataset.new('x*sin(x)', with: 'lines', lw: 4)

File with points:

Dataset.new('points.data', with: 'lines', title: 'Points from file')

Some data (creates datablock stored in memory):

x = (0..5000).to_a
y = x.map {|xx| xx*xx }
points = [x, y]
Dataset.new(points, with: 'points', title: 'Points')

The same data but datablock stores it in temp file:

Dataset.new(points, with: 'points', title: 'Points', file: true)

52
53
54
55
# File 'lib/gnuplotrb/staff/dataset.rb', line 52

def initialize(data, **options)
  # run method by name
  send(INIT_HANDLERS[data.class], data, options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GnuplotRB::Plottable

Instance Attribute Details

#dataObject (readonly)

Data represented by this dataset


11
12
13
# File 'lib/gnuplotrb/staff/dataset.rb', line 11

def data
  @data
end

Instance Method Details

#cloneObject

Overview

Own implementation of #clone. Creates new Dataset if data stored in datablock and calls super otherwise.


116
117
118
119
120
121
122
# File 'lib/gnuplotrb/staff/dataset.rb', line 116

def clone
  if @type == :datablock
    new_with_options(@options)
  else
    super
  end
end

#plot(*args) ⇒ Object

Overview

Creates new Plot object with only one Dataset given - self. Calls #plot on created Plot. All arguments given to this #plot will be sent to Plot#plot instead.

Example
sin = Dataset.new('sin(x)')
sin.plot(term: [qt, size: [300, 300]])
#=> shows qt window 300x300 with sin(x)
sin.to_png('./plot.png')
#=> creates png file with sin(x) plotted

135
136
137
# File 'lib/gnuplotrb/staff/dataset.rb', line 135

def plot(*args)
  Plot.new(self).plot(*args)
end

#to_s(terminal = nil) ⇒ Object

Overview

Converts Dataset to string containing gnuplot dataset.

Parameters
  • terminal - must be given if data given as Datablock and it does not use temp file so data should be piped out to gnuplot via terminal before use.

Examples
Dataset.new('points.data', with: 'lines', title: 'Points from file').to_s
#=> "'points.data' with lines title 'Points form file'"
Dataset.new(points, with: 'points', title: 'Points').to_s
#=> "$DATA1 with points title 'Points'"

69
70
71
# File 'lib/gnuplotrb/staff/dataset.rb', line 69

def to_s(terminal = nil)
  "#{@type == :datablock ? @data.name(terminal) : @data} #{options_to_string}"
end

#update(data = nil, **options) ⇒ Object

Overview

Creates new dataset with updated data (given data is appended to existing) and merged options. Data is updated only if Dataset stores it in Datablock. Method does nothing if no options given and data isn't stored in in-memory Datablock.

Parameters
  • data - data to append to existing

  • options - hash to merge with existing options

Examples

Updating dataset with Math formula or filename given:

dataset = Dataset.new('file.data')
dataset.update(data: 'asd')
#=> nothing updated
dataset.update(data: 'asd', title: 'File')
#=> Dataset.new('file.data', title: 'File')

Updating dataset with data stored in Datablock:

in_memory_points = Dataset.new(points, title: 'Old one')
in_memory_points.update(data: some_update, title: 'Updated')
#=> Dataset.new(points + some_update, title: 'Updated')
temp_file_points = Dataset.new(points, title: 'Old one', file: true)
temp_file_points.update(data: some_update)
#=> data updated but no new dataset created
temp_file_points.update(data: some_update, title: 'Updated')
#=> data updated and new dataset with title 'Updated' returned

99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gnuplotrb/staff/dataset.rb', line 99

def update(data = nil, **options)
  if data && @type == :datablock
    new_datablock = @data.update(data)
    if new_datablock == @data
      update_options(options)
    else
      self.class.new(new_datablock, options)
    end
  else
    update_options(options)
  end
end