forked from rdp/ruby_gnuplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.rb
More file actions
154 lines (123 loc) · 3.76 KB
/
Copy pathplot.rb
File metadata and controls
154 lines (123 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module Gnuplot
# 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.
class Plot
attr_accessor :cmd, :data, :settings
QUOTED = %w(title output xlabel x2label ylabel y2label clabel cblabel zlabel)
def initialize (io = nil, cmd = "plot")
@cmd = cmd
@settings = []
@arbitrary_lines = []
@data = []
@styles = []
yield self if block_given?
puts "writing this to gnuplot:\n" + to_gplot + "\n" if $VERBOSE
if io
io << to_gplot
io << store_datasets
end
end
attr_accessor :arbitrary_lines
# 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.
def method_missing( methId, *args )
set methId.id2name, *args
end
# 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.
def set ( var, value = "" )
value = "\"#{value}\"" if QUOTED.include? var unless value =~ /^'.*'$/
@settings << [ :set, var, value ]
end
# Unset a variable. +Var+ must be a gnuplot variable.
def unset ( var )
@settings << [ :unset, var ]
end
# 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.
def [] ( var )
v = @settings.rassoc( var )
if v.nil? or v.first == :unset
nil
else
v[2]
end
end
class Style
attr_accessor :linestyle, :linetype, :linewidth, :linecolor,
:pointtype, :pointsize, :fill, :index
alias :ls :linestyle
alias :lt :linetype
alias :lw :linewidth
alias :lc :linecolor
alias :pt :pointtype
alias :ps :pointsize
alias :fs :fill
alias :ls= :linestyle=
alias :lt= :linetype=
alias :lw= :linewidth=
alias :lc= :linecolor=
alias :pt= :pointtype=
alias :ps= :pointsize=
alias :fs= :fill=
STYLES = [:ls, :lt, :lw, :lc, :pt, :ps, :fs]
def Style.increment_index
@index ||= 0
@index += 1
@index
end
def initialize
STYLES.each do |s|
send("#{s}=", nil)
end
yield self if block_given?
# only set the index if the user didn't do it
@index = Style::increment_index if index.nil?
end
def to_s
str = "set style line #{index}"
STYLES.each do |s|
style = send(s)
if not style.nil?
str << " #{s} #{style}"
end
end
str
end
end
# Create a gnuplot linestyle
def style &blk
s = Style.new &blk
@styles << s
s
end
def add_data ( ds )
@data << ds
end
def to_gplot (io = "")
@settings.each do |setting|
io << setting.map(&:to_s).join(" ") << "\n"
end
@styles.each{|s| io << s.to_s << "\n"}
@arbitrary_lines.each{|line| io << line << "\n" }
io
end
def store_datasets (io = "")
if @data.size > 0
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
end
end