gnuplotrb 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -4
- data/LICENSE +20 -20
- data/README.rdoc +163 -163
- data/Rakefile +16 -16
- data/gnuplotrb.gemspec +30 -30
- data/lib/gnuplotrb.rb +35 -35
- data/lib/gnuplotrb/animation.rb +129 -129
- data/lib/gnuplotrb/external_classes/array.rb +17 -17
- data/lib/gnuplotrb/external_classes/daru.rb +43 -43
- data/lib/gnuplotrb/external_classes/string.rb +6 -6
- data/lib/gnuplotrb/fit.rb +204 -204
- data/lib/gnuplotrb/mixins/error_handling.rb +48 -48
- data/lib/gnuplotrb/mixins/option_handling.rb +190 -190
- data/lib/gnuplotrb/mixins/plottable.rb +208 -208
- data/lib/gnuplotrb/multiplot.rb +269 -269
- data/lib/gnuplotrb/plot.rb +299 -299
- data/lib/gnuplotrb/splot.rb +18 -18
- data/lib/gnuplotrb/staff/datablock.rb +112 -112
- data/lib/gnuplotrb/staff/dataset.rb +294 -294
- data/lib/gnuplotrb/staff/settings.rb +89 -80
- data/lib/gnuplotrb/staff/terminal.rb +202 -202
- data/lib/gnuplotrb/version.rb +8 -8
- metadata +3 -4
@@ -1,190 +1,190 @@
|
|
1
|
-
module GnuplotRB
|
2
|
-
##
|
3
|
-
# This module contains methods which are mixed into several classes
|
4
|
-
# to set, get and convert their options.
|
5
|
-
module OptionHandling
|
6
|
-
class << self
|
7
|
-
# Some values of options should be quoted to be read by gnuplot properly
|
8
|
-
#
|
9
|
-
# @todo update list with data from gnuplot documentation !!!
|
10
|
-
QUOTED_OPTIONS = %w(
|
11
|
-
title
|
12
|
-
output
|
13
|
-
xlabel
|
14
|
-
x2label
|
15
|
-
ylabel
|
16
|
-
y2label
|
17
|
-
clabel
|
18
|
-
cblabel
|
19
|
-
zlabel
|
20
|
-
rgb
|
21
|
-
font
|
22
|
-
background
|
23
|
-
format
|
24
|
-
format_x
|
25
|
-
format_y
|
26
|
-
format_xy
|
27
|
-
format_x2
|
28
|
-
format_y2
|
29
|
-
format_z
|
30
|
-
format_cb
|
31
|
-
timefmt
|
32
|
-
dt
|
33
|
-
dashtype
|
34
|
-
)
|
35
|
-
|
36
|
-
private_constant :QUOTED_OPTIONS
|
37
|
-
|
38
|
-
##
|
39
|
-
# Replace '_' with ' ' is made to allow passing several options
|
40
|
-
# with the same first word of key. See issue #7 for more info.
|
41
|
-
# @param key [Symbol, String] key to modify
|
42
|
-
# @return [String] given key with '_' replaced with ' '
|
43
|
-
def string_key(key)
|
44
|
-
key.to_s.gsub(/_/) { ' ' } + ' '
|
45
|
-
end
|
46
|
-
|
47
|
-
##
|
48
|
-
# Recursive function that converts Ruby option to gnuplot string
|
49
|
-
#
|
50
|
-
# @param key [Symbol] name of option in gnuplot
|
51
|
-
# @param option an option that should be converted
|
52
|
-
# @example
|
53
|
-
# option_to_string(['png', size: [300, 300]])
|
54
|
-
# #=> 'png size 300,300'
|
55
|
-
# option_to_string(xrange: 0..100)
|
56
|
-
# #=> 'xrange [0:100]'
|
57
|
-
# option_to_string(multiplot: true)
|
58
|
-
# #=> 'multiplot'
|
59
|
-
def option_to_string(key = nil, option)
|
60
|
-
return string_key(key) if !!option == option # check for boolean
|
61
|
-
value = ruby_class_to_gnuplot(option)
|
62
|
-
value = "\"#{value}\"" if QUOTED_OPTIONS.include?(key.to_s)
|
63
|
-
## :+ here is necessary, because using #{value} will remove quotes
|
64
|
-
value = string_key(key) + value if key
|
65
|
-
value
|
66
|
-
end
|
67
|
-
|
68
|
-
##
|
69
|
-
# @private
|
70
|
-
# Method for inner use.
|
71
|
-
# Needed to convert several ruby classes into
|
72
|
-
# value that should be piped to gnuplot.
|
73
|
-
def ruby_class_to_gnuplot(option_object)
|
74
|
-
case option_object
|
75
|
-
when Array
|
76
|
-
option_object.map { |el| option_to_string(el) }
|
77
|
-
.join(option_object[0].is_a?(Numeric) ? ',' : ' ')
|
78
|
-
when Hash
|
79
|
-
option_object.map { |i_key, i_val| option_to_string(i_key, i_val) }
|
80
|
-
.join(' ')
|
81
|
-
when Range
|
82
|
-
"[#{option_object.begin}:#{option_object.end}]"
|
83
|
-
else
|
84
|
-
option_object.to_s
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
##
|
89
|
-
# Check if given terminal available for use.
|
90
|
-
#
|
91
|
-
# @param terminal [String] terminal to check (e.g. 'png', 'qt', 'gif')
|
92
|
-
# @return [Boolean] true or false
|
93
|
-
def valid_terminal?(terminal)
|
94
|
-
Settings.available_terminals.include?(terminal)
|
95
|
-
end
|
96
|
-
|
97
|
-
##
|
98
|
-
# Check if given options are valid for gnuplot.
|
99
|
-
# Raises ArgumentError if invalid options found.
|
100
|
-
# Now checks only terminal name.
|
101
|
-
#
|
102
|
-
# @param options [Hash] options to check (e.g. "{ term: 'qt', title: 'Plot title' }")
|
103
|
-
def validate_terminal_options(options)
|
104
|
-
terminal = options[:term]
|
105
|
-
return unless terminal
|
106
|
-
terminal = terminal[0] if terminal.is_a?(Array)
|
107
|
-
message = 'Seems like your Gnuplot does not ' \
|
108
|
-
"support that terminal (#{terminal}), please see " \
|
109
|
-
'supported terminals with Settings::available_terminals'
|
110
|
-
fail(ArgumentError, message) unless valid_terminal?(terminal)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
##
|
115
|
-
# @private
|
116
|
-
# You should implement #initialize in classes that use OptionsHelper
|
117
|
-
def initialize(*_)
|
118
|
-
fail NotImplementedError, 'You should implement #initialize' \
|
119
|
-
' in classes that use OptionsHelper!'
|
120
|
-
end
|
121
|
-
|
122
|
-
##
|
123
|
-
# @private
|
124
|
-
# You should implement #new_with_options in classes that use OptionsHelper
|
125
|
-
def new_with_options(*_)
|
126
|
-
fail NotImplementedError, 'You should implement #new_with_options' \
|
127
|
-
' in classes that use OptionsHelper!'
|
128
|
-
end
|
129
|
-
|
130
|
-
##
|
131
|
-
# Create new Plot (or Dataset or Splot or Multiplot) object where current
|
132
|
-
# options are merged with given. If no options
|
133
|
-
# given it will just return existing set of options.
|
134
|
-
#
|
135
|
-
# @param options [Hash] options to add
|
136
|
-
# @return [Dataset, Splot, Multiplot] new object created with given options
|
137
|
-
# @return [Hamster::Hash] current options if given options empty
|
138
|
-
# @example
|
139
|
-
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
|
140
|
-
# sin_graph.plot
|
141
|
-
# sin_graph_update = sin_graph.options(title: 'Sin on [-10:10]', xrange: -10..10)
|
142
|
-
# sin_graph_update.plot
|
143
|
-
# # sin_graph IS NOT affected
|
144
|
-
def options(**options)
|
145
|
-
@options ||= Hamster::Hash.new
|
146
|
-
if options.empty?
|
147
|
-
@options
|
148
|
-
else
|
149
|
-
new_with_options(@options.merge(options))
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
##
|
154
|
-
# Update existing Plot (or Dataset or Splot or Multiplot) object with given options.
|
155
|
-
#
|
156
|
-
# @param options [Hash] options to add
|
157
|
-
# @return [Dataset, Splot, Multiplot] self
|
158
|
-
# @example
|
159
|
-
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
|
160
|
-
# sin_graph.plot
|
161
|
-
# sin_graph.options!(title: 'Sin on [-10:10]', xrange: -10..10)
|
162
|
-
# sin_graph.plot
|
163
|
-
# # second #plot call will plot not the same as first, sin_graph IS affected
|
164
|
-
def options!(**options)
|
165
|
-
@options = @options ? @options.merge(options) : Hamster::Hash.new(options)
|
166
|
-
self
|
167
|
-
end
|
168
|
-
|
169
|
-
private
|
170
|
-
|
171
|
-
##
|
172
|
-
# Return current option value if no value given. Create new
|
173
|
-
# object with given option set if value given.
|
174
|
-
def option(key, *value)
|
175
|
-
if value.empty?
|
176
|
-
value = options[key]
|
177
|
-
value = value[0] if value && value.size == 1
|
178
|
-
value
|
179
|
-
else
|
180
|
-
options(key => value)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
##
|
185
|
-
# Just set an option.
|
186
|
-
def option!(key, *value)
|
187
|
-
options!(key => value)
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
1
|
+
module GnuplotRB
|
2
|
+
##
|
3
|
+
# This module contains methods which are mixed into several classes
|
4
|
+
# to set, get and convert their options.
|
5
|
+
module OptionHandling
|
6
|
+
class << self
|
7
|
+
# Some values of options should be quoted to be read by gnuplot properly
|
8
|
+
#
|
9
|
+
# @todo update list with data from gnuplot documentation !!!
|
10
|
+
QUOTED_OPTIONS = %w(
|
11
|
+
title
|
12
|
+
output
|
13
|
+
xlabel
|
14
|
+
x2label
|
15
|
+
ylabel
|
16
|
+
y2label
|
17
|
+
clabel
|
18
|
+
cblabel
|
19
|
+
zlabel
|
20
|
+
rgb
|
21
|
+
font
|
22
|
+
background
|
23
|
+
format
|
24
|
+
format_x
|
25
|
+
format_y
|
26
|
+
format_xy
|
27
|
+
format_x2
|
28
|
+
format_y2
|
29
|
+
format_z
|
30
|
+
format_cb
|
31
|
+
timefmt
|
32
|
+
dt
|
33
|
+
dashtype
|
34
|
+
)
|
35
|
+
|
36
|
+
private_constant :QUOTED_OPTIONS
|
37
|
+
|
38
|
+
##
|
39
|
+
# Replace '_' with ' ' is made to allow passing several options
|
40
|
+
# with the same first word of key. See issue #7 for more info.
|
41
|
+
# @param key [Symbol, String] key to modify
|
42
|
+
# @return [String] given key with '_' replaced with ' '
|
43
|
+
def string_key(key)
|
44
|
+
key.to_s.gsub(/_/) { ' ' } + ' '
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Recursive function that converts Ruby option to gnuplot string
|
49
|
+
#
|
50
|
+
# @param key [Symbol] name of option in gnuplot
|
51
|
+
# @param option an option that should be converted
|
52
|
+
# @example
|
53
|
+
# option_to_string(['png', size: [300, 300]])
|
54
|
+
# #=> 'png size 300,300'
|
55
|
+
# option_to_string(xrange: 0..100)
|
56
|
+
# #=> 'xrange [0:100]'
|
57
|
+
# option_to_string(multiplot: true)
|
58
|
+
# #=> 'multiplot'
|
59
|
+
def option_to_string(key = nil, option)
|
60
|
+
return string_key(key) if !!option == option # check for boolean
|
61
|
+
value = ruby_class_to_gnuplot(option)
|
62
|
+
value = "\"#{value}\"" if QUOTED_OPTIONS.include?(key.to_s)
|
63
|
+
## :+ here is necessary, because using #{value} will remove quotes
|
64
|
+
value = string_key(key) + value if key
|
65
|
+
value
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# @private
|
70
|
+
# Method for inner use.
|
71
|
+
# Needed to convert several ruby classes into
|
72
|
+
# value that should be piped to gnuplot.
|
73
|
+
def ruby_class_to_gnuplot(option_object)
|
74
|
+
case option_object
|
75
|
+
when Array
|
76
|
+
option_object.map { |el| option_to_string(el) }
|
77
|
+
.join(option_object[0].is_a?(Numeric) ? ',' : ' ')
|
78
|
+
when Hash
|
79
|
+
option_object.map { |i_key, i_val| option_to_string(i_key, i_val) }
|
80
|
+
.join(' ')
|
81
|
+
when Range
|
82
|
+
"[#{option_object.begin}:#{option_object.end}]"
|
83
|
+
else
|
84
|
+
option_object.to_s
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Check if given terminal available for use.
|
90
|
+
#
|
91
|
+
# @param terminal [String] terminal to check (e.g. 'png', 'qt', 'gif')
|
92
|
+
# @return [Boolean] true or false
|
93
|
+
def valid_terminal?(terminal)
|
94
|
+
Settings.available_terminals.include?(terminal)
|
95
|
+
end
|
96
|
+
|
97
|
+
##
|
98
|
+
# Check if given options are valid for gnuplot.
|
99
|
+
# Raises ArgumentError if invalid options found.
|
100
|
+
# Now checks only terminal name.
|
101
|
+
#
|
102
|
+
# @param options [Hash] options to check (e.g. "{ term: 'qt', title: 'Plot title' }")
|
103
|
+
def validate_terminal_options(options)
|
104
|
+
terminal = options[:term]
|
105
|
+
return unless terminal
|
106
|
+
terminal = terminal[0] if terminal.is_a?(Array)
|
107
|
+
message = 'Seems like your Gnuplot does not ' \
|
108
|
+
"support that terminal (#{terminal}), please see " \
|
109
|
+
'supported terminals with Settings::available_terminals'
|
110
|
+
fail(ArgumentError, message) unless valid_terminal?(terminal)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
##
|
115
|
+
# @private
|
116
|
+
# You should implement #initialize in classes that use OptionsHelper
|
117
|
+
def initialize(*_)
|
118
|
+
fail NotImplementedError, 'You should implement #initialize' \
|
119
|
+
' in classes that use OptionsHelper!'
|
120
|
+
end
|
121
|
+
|
122
|
+
##
|
123
|
+
# @private
|
124
|
+
# You should implement #new_with_options in classes that use OptionsHelper
|
125
|
+
def new_with_options(*_)
|
126
|
+
fail NotImplementedError, 'You should implement #new_with_options' \
|
127
|
+
' in classes that use OptionsHelper!'
|
128
|
+
end
|
129
|
+
|
130
|
+
##
|
131
|
+
# Create new Plot (or Dataset or Splot or Multiplot) object where current
|
132
|
+
# options are merged with given. If no options
|
133
|
+
# given it will just return existing set of options.
|
134
|
+
#
|
135
|
+
# @param options [Hash] options to add
|
136
|
+
# @return [Dataset, Splot, Multiplot] new object created with given options
|
137
|
+
# @return [Hamster::Hash] current options if given options empty
|
138
|
+
# @example
|
139
|
+
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
|
140
|
+
# sin_graph.plot
|
141
|
+
# sin_graph_update = sin_graph.options(title: 'Sin on [-10:10]', xrange: -10..10)
|
142
|
+
# sin_graph_update.plot
|
143
|
+
# # sin_graph IS NOT affected
|
144
|
+
def options(**options)
|
145
|
+
@options ||= Hamster::Hash.new
|
146
|
+
if options.empty?
|
147
|
+
@options
|
148
|
+
else
|
149
|
+
new_with_options(@options.merge(options))
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
##
|
154
|
+
# Update existing Plot (or Dataset or Splot or Multiplot) object with given options.
|
155
|
+
#
|
156
|
+
# @param options [Hash] options to add
|
157
|
+
# @return [Dataset, Splot, Multiplot] self
|
158
|
+
# @example
|
159
|
+
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
|
160
|
+
# sin_graph.plot
|
161
|
+
# sin_graph.options!(title: 'Sin on [-10:10]', xrange: -10..10)
|
162
|
+
# sin_graph.plot
|
163
|
+
# # second #plot call will plot not the same as first, sin_graph IS affected
|
164
|
+
def options!(**options)
|
165
|
+
@options = @options ? @options.merge(options) : Hamster::Hash.new(options)
|
166
|
+
self
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
|
171
|
+
##
|
172
|
+
# Return current option value if no value given. Create new
|
173
|
+
# object with given option set if value given.
|
174
|
+
def option(key, *value)
|
175
|
+
if value.empty?
|
176
|
+
value = options[key]
|
177
|
+
value = value[0] if value && value.size == 1
|
178
|
+
value
|
179
|
+
else
|
180
|
+
options(key => value)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
##
|
185
|
+
# Just set an option.
|
186
|
+
def option!(key, *value)
|
187
|
+
options!(key => value)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -1,208 +1,208 @@
|
|
1
|
-
module GnuplotRB
|
2
|
-
##
|
3
|
-
# This module contains methods that should be mixed into
|
4
|
-
# plottable classes. It includes OptionHandling and
|
5
|
-
# implements several plotting methods.
|
6
|
-
module Plottable
|
7
|
-
include OptionHandling
|
8
|
-
|
9
|
-
##
|
10
|
-
# @private
|
11
|
-
# You should implement #plot in classes that are Plottable
|
12
|
-
def plot(*_)
|
13
|
-
fail NotImplementedError, 'You should implement #plot in classes that are Plottable!'
|
14
|
-
end
|
15
|
-
|
16
|
-
##
|
17
|
-
# In this gem #method_missing is used both to handle
|
18
|
-
# options and to handle plotting to specific terminal.
|
19
|
-
#
|
20
|
-
# == Options handling
|
21
|
-
# === Overview
|
22
|
-
# You may set options using #option_name(option_value) method.
|
23
|
-
# A new object will be constructed with selected option set.
|
24
|
-
# And finally you can get current value of any option using
|
25
|
-
# #options_name without arguments.
|
26
|
-
# === Arguments
|
27
|
-
# * *option_value* - value to set an option. If none given
|
28
|
-
# method will just return current option's value
|
29
|
-
# === Examples
|
30
|
-
# plot = Splot.new
|
31
|
-
# new_plot = plot.title('Awesome plot')
|
32
|
-
# plot.title #=> nil
|
33
|
-
# new_plot.title #=> 'Awesome plot'
|
34
|
-
#
|
35
|
-
# == Plotting to specific term
|
36
|
-
# === Overview
|
37
|
-
# Gnuplot offers possibility to output graphics to many image formats.
|
38
|
-
# The easiest way to to so is to use #to_<plot_name> methods.
|
39
|
-
# === Arguments
|
40
|
-
# * *options* - set of options related to terminal (size, font etc).
|
41
|
-
# Be careful, some terminals have their own specific options.
|
42
|
-
# === Examples
|
43
|
-
# # font options specific for png term
|
44
|
-
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
|
45
|
-
# # font options specific for svg term
|
46
|
-
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
|
47
|
-
def method_missing(meth_id, *args)
|
48
|
-
meth = meth_id.id2name
|
49
|
-
case
|
50
|
-
when meth[0..2] == 'to_'
|
51
|
-
term = meth[3..-1]
|
52
|
-
super unless OptionHandling.valid_terminal?(term)
|
53
|
-
to_specific_term(term, *args)
|
54
|
-
when meth[-1] == '!'
|
55
|
-
option!(meth[0..-2].to_sym, *args)
|
56
|
-
when meth[-1] == '='
|
57
|
-
option!(meth[0..-2].to_sym, *args)
|
58
|
-
option(meth[0..-2].to_sym)
|
59
|
-
else
|
60
|
-
option(meth_id, *args)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
##
|
65
|
-
# @return [true] for existing methods and
|
66
|
-
# #to_|term_name| when name is a valid terminal type.
|
67
|
-
# @return [false] otherwise
|
68
|
-
def respond_to?(meth_id)
|
69
|
-
# Next line is here to force iRuby use #to_iruby
|
70
|
-
# instead of #to_svg.
|
71
|
-
return super if defined? IRuby
|
72
|
-
meth = meth_id.id2name
|
73
|
-
term = meth[0..2] == 'to_' && OptionHandling.valid_terminal?(meth[3..-1])
|
74
|
-
term || super
|
75
|
-
end
|
76
|
-
|
77
|
-
##
|
78
|
-
# This method is used to embed plottable objects into iRuby notebooks. There is
|
79
|
-
# {a notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/basic_usage.ipynb]
|
80
|
-
# with examples of its usage.
|
81
|
-
def to_iruby
|
82
|
-
available_terminals = {
|
83
|
-
'png' => 'image/png',
|
84
|
-
'pngcairo' => 'image/png',
|
85
|
-
'jpeg' => 'image/jpeg',
|
86
|
-
'svg' => 'image/svg+xml',
|
87
|
-
'dumb' => 'text/plain'
|
88
|
-
}
|
89
|
-
terminal, options = term.is_a?(Array) ? [term[0], term[1]] : [term, {}]
|
90
|
-
terminal = 'svg' unless available_terminals.keys.include?(terminal)
|
91
|
-
[available_terminals[terminal], send("to_#{terminal}".to_sym, **options)]
|
92
|
-
end
|
93
|
-
|
94
|
-
##
|
95
|
-
# @private
|
96
|
-
# Output plot to specific terminal (possibly some file).
|
97
|
-
# Explicit use should be avoided. This method is called from #method_missing
|
98
|
-
# when it handles method names like #to_png(options).
|
99
|
-
#
|
100
|
-
# @param trminal [String] terminal name ('png', 'svg' etc)
|
101
|
-
# @param path [String] path to output file, if none given it will output to temp file
|
102
|
-
# and then read it and return binary contents of file
|
103
|
-
# @param options [Hash] used in #plot
|
104
|
-
# @example
|
105
|
-
# ## plot here may be Plot, Splot, Multiplot or any other plottable class
|
106
|
-
# plot.to_png('./result.png', size: [300, 500])
|
107
|
-
# contents = plot.to_svg(size: [100, 100])
|
108
|
-
# plot.to_dumb('./result.txt', size: [30, 15])
|
109
|
-
def to_specific_term(terminal, path = nil, **options)
|
110
|
-
if path
|
111
|
-
result = plot(term: [terminal, options], output: path)
|
112
|
-
else
|
113
|
-
path = Dir::Tmpname.make_tmpname(terminal, 0)
|
114
|
-
plot(term: [terminal, options], output: path)
|
115
|
-
result = File.binread(path)
|
116
|
-
File.delete(path)
|
117
|
-
end
|
118
|
-
result
|
119
|
-
end
|
120
|
-
|
121
|
-
##
|
122
|
-
# @return [Terminal] terminal object linked with this Plottable object
|
123
|
-
def own_terminal
|
124
|
-
@terminal ||= Terminal.new
|
125
|
-
end
|
126
|
-
|
127
|
-
##
|
128
|
-
# @!method xrange(value = nil)
|
129
|
-
# @!method yrange(value = nil)
|
130
|
-
# @!method title(value = nil)
|
131
|
-
# @!method option_name(value = nil)
|
132
|
-
# Clone existing object and set new options value in created one or just return
|
133
|
-
# existing value if nil given.
|
134
|
-
#
|
135
|
-
# Method is handled by #method_missing.
|
136
|
-
#
|
137
|
-
# You may set options using #option_name(option_value) method.
|
138
|
-
# A new object will be constructed with selected option set.
|
139
|
-
# And finally you can get current value of any option using
|
140
|
-
# #options_name without arguments.
|
141
|
-
#
|
142
|
-
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
|
143
|
-
#
|
144
|
-
# @param value new value for option
|
145
|
-
# @return new object with option_name set to *value* if value given
|
146
|
-
# @return old option value if no value given
|
147
|
-
#
|
148
|
-
# @example
|
149
|
-
# plot = Splot.new
|
150
|
-
# new_plot = plot.title('Awesome plot')
|
151
|
-
# plot.title #=> nil
|
152
|
-
# new_plot.title #=> 'Awesome plot'
|
153
|
-
|
154
|
-
##
|
155
|
-
# @!method xrange!(value)
|
156
|
-
# @!method yrange!(value)
|
157
|
-
# @!method title!(value)
|
158
|
-
# @!method option_name!(value)
|
159
|
-
# Set value for an option.
|
160
|
-
#
|
161
|
-
# Method is handled by #method_missing.
|
162
|
-
#
|
163
|
-
# You may set options using obj.option_name!(option_value) or
|
164
|
-
# obj.option_name = option_value methods.
|
165
|
-
#
|
166
|
-
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
|
167
|
-
#
|
168
|
-
# @param value new value for option
|
169
|
-
# @return self
|
170
|
-
#
|
171
|
-
# @example
|
172
|
-
# plot = Splot.new
|
173
|
-
# plot.title #=> nil
|
174
|
-
# plot.title!('Awesome plot')
|
175
|
-
# plot.title #=> 'Awesome plot'
|
176
|
-
#
|
177
|
-
# @example
|
178
|
-
# plot = Splot.new
|
179
|
-
# plot.title #=> nil
|
180
|
-
# plot.title = 'Awesome plot'
|
181
|
-
# plot.title #=> 'Awesome plot'
|
182
|
-
|
183
|
-
##
|
184
|
-
# @!method to_png(path = nil, **options)
|
185
|
-
# @!method to_svg(path = nil, **options)
|
186
|
-
# @!method to_gif(path = nil, **options)
|
187
|
-
# @!method to_canvas(path = nil, **options)
|
188
|
-
# Output to plot to according image format.
|
189
|
-
#
|
190
|
-
# All of #to_|terminal_name| methods are handled with #method_missing.
|
191
|
-
#
|
192
|
-
# Gnuplot offers possibility to output graphics to many image formats.
|
193
|
-
# The easiest way to to so is to use #to_<plot_name> methods.
|
194
|
-
#
|
195
|
-
# @param path [String] path to save plot file to.
|
196
|
-
# @param options [Hash] specific terminal options like 'size',
|
197
|
-
# 'font' etc
|
198
|
-
#
|
199
|
-
# @return [String] contents of plotted file unless path given
|
200
|
-
# @return self if path given
|
201
|
-
#
|
202
|
-
# @example
|
203
|
-
# # font options specific for png term
|
204
|
-
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
|
205
|
-
# # font options specific for svg term
|
206
|
-
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
|
207
|
-
end
|
208
|
-
end
|
1
|
+
module GnuplotRB
|
2
|
+
##
|
3
|
+
# This module contains methods that should be mixed into
|
4
|
+
# plottable classes. It includes OptionHandling and
|
5
|
+
# implements several plotting methods.
|
6
|
+
module Plottable
|
7
|
+
include OptionHandling
|
8
|
+
|
9
|
+
##
|
10
|
+
# @private
|
11
|
+
# You should implement #plot in classes that are Plottable
|
12
|
+
def plot(*_)
|
13
|
+
fail NotImplementedError, 'You should implement #plot in classes that are Plottable!'
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# In this gem #method_missing is used both to handle
|
18
|
+
# options and to handle plotting to specific terminal.
|
19
|
+
#
|
20
|
+
# == Options handling
|
21
|
+
# === Overview
|
22
|
+
# You may set options using #option_name(option_value) method.
|
23
|
+
# A new object will be constructed with selected option set.
|
24
|
+
# And finally you can get current value of any option using
|
25
|
+
# #options_name without arguments.
|
26
|
+
# === Arguments
|
27
|
+
# * *option_value* - value to set an option. If none given
|
28
|
+
# method will just return current option's value
|
29
|
+
# === Examples
|
30
|
+
# plot = Splot.new
|
31
|
+
# new_plot = plot.title('Awesome plot')
|
32
|
+
# plot.title #=> nil
|
33
|
+
# new_plot.title #=> 'Awesome plot'
|
34
|
+
#
|
35
|
+
# == Plotting to specific term
|
36
|
+
# === Overview
|
37
|
+
# Gnuplot offers possibility to output graphics to many image formats.
|
38
|
+
# The easiest way to to so is to use #to_<plot_name> methods.
|
39
|
+
# === Arguments
|
40
|
+
# * *options* - set of options related to terminal (size, font etc).
|
41
|
+
# Be careful, some terminals have their own specific options.
|
42
|
+
# === Examples
|
43
|
+
# # font options specific for png term
|
44
|
+
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
|
45
|
+
# # font options specific for svg term
|
46
|
+
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
|
47
|
+
def method_missing(meth_id, *args)
|
48
|
+
meth = meth_id.id2name
|
49
|
+
case
|
50
|
+
when meth[0..2] == 'to_'
|
51
|
+
term = meth[3..-1]
|
52
|
+
super unless OptionHandling.valid_terminal?(term)
|
53
|
+
to_specific_term(term, *args)
|
54
|
+
when meth[-1] == '!'
|
55
|
+
option!(meth[0..-2].to_sym, *args)
|
56
|
+
when meth[-1] == '='
|
57
|
+
option!(meth[0..-2].to_sym, *args)
|
58
|
+
option(meth[0..-2].to_sym)
|
59
|
+
else
|
60
|
+
option(meth_id, *args)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# @return [true] for existing methods and
|
66
|
+
# #to_|term_name| when name is a valid terminal type.
|
67
|
+
# @return [false] otherwise
|
68
|
+
def respond_to?(meth_id)
|
69
|
+
# Next line is here to force iRuby use #to_iruby
|
70
|
+
# instead of #to_svg.
|
71
|
+
return super if defined? IRuby
|
72
|
+
meth = meth_id.id2name
|
73
|
+
term = meth[0..2] == 'to_' && OptionHandling.valid_terminal?(meth[3..-1])
|
74
|
+
term || super
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# This method is used to embed plottable objects into iRuby notebooks. There is
|
79
|
+
# {a notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/basic_usage.ipynb]
|
80
|
+
# with examples of its usage.
|
81
|
+
def to_iruby
|
82
|
+
available_terminals = {
|
83
|
+
'png' => 'image/png',
|
84
|
+
'pngcairo' => 'image/png',
|
85
|
+
'jpeg' => 'image/jpeg',
|
86
|
+
'svg' => 'image/svg+xml',
|
87
|
+
'dumb' => 'text/plain'
|
88
|
+
}
|
89
|
+
terminal, options = term.is_a?(Array) ? [term[0], term[1]] : [term, {}]
|
90
|
+
terminal = 'svg' unless available_terminals.keys.include?(terminal)
|
91
|
+
[available_terminals[terminal], send("to_#{terminal}".to_sym, **options)]
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# @private
|
96
|
+
# Output plot to specific terminal (possibly some file).
|
97
|
+
# Explicit use should be avoided. This method is called from #method_missing
|
98
|
+
# when it handles method names like #to_png(options).
|
99
|
+
#
|
100
|
+
# @param trminal [String] terminal name ('png', 'svg' etc)
|
101
|
+
# @param path [String] path to output file, if none given it will output to temp file
|
102
|
+
# and then read it and return binary contents of file
|
103
|
+
# @param options [Hash] used in #plot
|
104
|
+
# @example
|
105
|
+
# ## plot here may be Plot, Splot, Multiplot or any other plottable class
|
106
|
+
# plot.to_png('./result.png', size: [300, 500])
|
107
|
+
# contents = plot.to_svg(size: [100, 100])
|
108
|
+
# plot.to_dumb('./result.txt', size: [30, 15])
|
109
|
+
def to_specific_term(terminal, path = nil, **options)
|
110
|
+
if path
|
111
|
+
result = plot(term: [terminal, options], output: path)
|
112
|
+
else
|
113
|
+
path = Dir::Tmpname.make_tmpname(terminal, 0)
|
114
|
+
plot(term: [terminal, options], output: path)
|
115
|
+
result = File.binread(path)
|
116
|
+
File.delete(path)
|
117
|
+
end
|
118
|
+
result
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# @return [Terminal] terminal object linked with this Plottable object
|
123
|
+
def own_terminal
|
124
|
+
@terminal ||= Terminal.new
|
125
|
+
end
|
126
|
+
|
127
|
+
##
|
128
|
+
# @!method xrange(value = nil)
|
129
|
+
# @!method yrange(value = nil)
|
130
|
+
# @!method title(value = nil)
|
131
|
+
# @!method option_name(value = nil)
|
132
|
+
# Clone existing object and set new options value in created one or just return
|
133
|
+
# existing value if nil given.
|
134
|
+
#
|
135
|
+
# Method is handled by #method_missing.
|
136
|
+
#
|
137
|
+
# You may set options using #option_name(option_value) method.
|
138
|
+
# A new object will be constructed with selected option set.
|
139
|
+
# And finally you can get current value of any option using
|
140
|
+
# #options_name without arguments.
|
141
|
+
#
|
142
|
+
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
|
143
|
+
#
|
144
|
+
# @param value new value for option
|
145
|
+
# @return new object with option_name set to *value* if value given
|
146
|
+
# @return old option value if no value given
|
147
|
+
#
|
148
|
+
# @example
|
149
|
+
# plot = Splot.new
|
150
|
+
# new_plot = plot.title('Awesome plot')
|
151
|
+
# plot.title #=> nil
|
152
|
+
# new_plot.title #=> 'Awesome plot'
|
153
|
+
|
154
|
+
##
|
155
|
+
# @!method xrange!(value)
|
156
|
+
# @!method yrange!(value)
|
157
|
+
# @!method title!(value)
|
158
|
+
# @!method option_name!(value)
|
159
|
+
# Set value for an option.
|
160
|
+
#
|
161
|
+
# Method is handled by #method_missing.
|
162
|
+
#
|
163
|
+
# You may set options using obj.option_name!(option_value) or
|
164
|
+
# obj.option_name = option_value methods.
|
165
|
+
#
|
166
|
+
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
|
167
|
+
#
|
168
|
+
# @param value new value for option
|
169
|
+
# @return self
|
170
|
+
#
|
171
|
+
# @example
|
172
|
+
# plot = Splot.new
|
173
|
+
# plot.title #=> nil
|
174
|
+
# plot.title!('Awesome plot')
|
175
|
+
# plot.title #=> 'Awesome plot'
|
176
|
+
#
|
177
|
+
# @example
|
178
|
+
# plot = Splot.new
|
179
|
+
# plot.title #=> nil
|
180
|
+
# plot.title = 'Awesome plot'
|
181
|
+
# plot.title #=> 'Awesome plot'
|
182
|
+
|
183
|
+
##
|
184
|
+
# @!method to_png(path = nil, **options)
|
185
|
+
# @!method to_svg(path = nil, **options)
|
186
|
+
# @!method to_gif(path = nil, **options)
|
187
|
+
# @!method to_canvas(path = nil, **options)
|
188
|
+
# Output to plot to according image format.
|
189
|
+
#
|
190
|
+
# All of #to_|terminal_name| methods are handled with #method_missing.
|
191
|
+
#
|
192
|
+
# Gnuplot offers possibility to output graphics to many image formats.
|
193
|
+
# The easiest way to to so is to use #to_<plot_name> methods.
|
194
|
+
#
|
195
|
+
# @param path [String] path to save plot file to.
|
196
|
+
# @param options [Hash] specific terminal options like 'size',
|
197
|
+
# 'font' etc
|
198
|
+
#
|
199
|
+
# @return [String] contents of plotted file unless path given
|
200
|
+
# @return self if path given
|
201
|
+
#
|
202
|
+
# @example
|
203
|
+
# # font options specific for png term
|
204
|
+
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
|
205
|
+
# # font options specific for svg term
|
206
|
+
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
|
207
|
+
end
|
208
|
+
end
|