Chris@1294
|
1 require 'SVG/Graph/Plot'
|
Chris@1294
|
2
|
Chris@1294
|
3 module SVG
|
Chris@1294
|
4 module Graph
|
Chris@1294
|
5 # === For creating SVG plots of scalar temporal data
|
Chris@1294
|
6 #
|
Chris@1294
|
7 # = Synopsis
|
Chris@1294
|
8 #
|
Chris@1294
|
9 # require 'SVG/Graph/TimeSeriess'
|
Chris@1294
|
10 #
|
Chris@1294
|
11 # # Data sets are x,y pairs
|
Chris@1294
|
12 # data1 = ["6/17/72", 11, "1/11/72", 7, "4/13/04 17:31", 11,
|
Chris@1294
|
13 # "9/11/01", 9, "9/1/85", 2, "9/1/88", 1, "1/15/95", 13]
|
Chris@1294
|
14 # data2 = ["8/1/73", 18, "3/1/77", 15, "10/1/98", 4,
|
Chris@1294
|
15 # "5/1/02", 14, "3/1/95", 6, "8/1/91", 12, "12/1/87", 6,
|
Chris@1294
|
16 # "5/1/84", 17, "10/1/80", 12]
|
Chris@1294
|
17 #
|
Chris@1294
|
18 # graph = SVG::Graph::TimeSeries.new( {
|
Chris@1294
|
19 # :width => 640,
|
Chris@1294
|
20 # :height => 480,
|
Chris@1294
|
21 # :graph_title => title,
|
Chris@1294
|
22 # :show_graph_title => true,
|
Chris@1294
|
23 # :no_css => true,
|
Chris@1294
|
24 # :key => true,
|
Chris@1294
|
25 # :scale_x_integers => true,
|
Chris@1294
|
26 # :scale_y_integers => true,
|
Chris@1294
|
27 # :min_x_value => 0,
|
Chris@1294
|
28 # :min_y_value => 0,
|
Chris@1294
|
29 # :show_data_labels => true,
|
Chris@1294
|
30 # :show_x_guidelines => true,
|
Chris@1294
|
31 # :show_x_title => true,
|
Chris@1294
|
32 # :x_title => "Time",
|
Chris@1294
|
33 # :show_y_title => true,
|
Chris@1294
|
34 # :y_title => "Ice Cream Cones",
|
Chris@1294
|
35 # :y_title_text_direction => :bt,
|
Chris@1294
|
36 # :stagger_x_labels => true,
|
Chris@1294
|
37 # :x_label_format => "%m/%d/%y",
|
Chris@1294
|
38 # })
|
Chris@1294
|
39 #
|
Chris@1294
|
40 # graph.add_data({
|
Chris@1294
|
41 # :data => projection
|
Chris@1294
|
42 # :title => 'Projected',
|
Chris@1294
|
43 # })
|
Chris@1294
|
44 #
|
Chris@1294
|
45 # graph.add_data({
|
Chris@1294
|
46 # :data => actual,
|
Chris@1294
|
47 # :title => 'Actual',
|
Chris@1294
|
48 # })
|
Chris@1294
|
49 #
|
Chris@1294
|
50 # print graph.burn()
|
Chris@1294
|
51 #
|
Chris@1294
|
52 # = Description
|
Chris@1294
|
53 #
|
Chris@1294
|
54 # Produces a graph of temporal scalar data.
|
Chris@1294
|
55 #
|
Chris@1294
|
56 # = Examples
|
Chris@1294
|
57 #
|
Chris@1294
|
58 # http://www.germane-software/repositories/public/SVG/test/timeseries.rb
|
Chris@1294
|
59 #
|
Chris@1294
|
60 # = Notes
|
Chris@1294
|
61 #
|
Chris@1294
|
62 # The default stylesheet handles upto 10 data sets, if you
|
Chris@1294
|
63 # use more you must create your own stylesheet and add the
|
Chris@1294
|
64 # additional settings for the extra data sets. You will know
|
Chris@1294
|
65 # if you go over 10 data sets as they will have no style and
|
Chris@1294
|
66 # be in black.
|
Chris@1294
|
67 #
|
Chris@1294
|
68 # Unlike the other types of charts, data sets must contain x,y pairs:
|
Chris@1294
|
69 #
|
Chris@1294
|
70 # [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
|
Chris@1294
|
71 # [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
|
Chris@1294
|
72 # # ("14:20",6)
|
Chris@1294
|
73 #
|
Chris@1294
|
74 # Note that multiple data sets within the same chart can differ in length,
|
Chris@1294
|
75 # and that the data in the datasets needn't be in order; they will be ordered
|
Chris@1294
|
76 # by the plot along the X-axis.
|
Chris@1294
|
77 #
|
Chris@1294
|
78 # The dates must be parseable by ParseDate, but otherwise can be
|
Chris@1294
|
79 # any order of magnitude (seconds within the hour, or years)
|
Chris@1294
|
80 #
|
Chris@1294
|
81 # = See also
|
Chris@1294
|
82 #
|
Chris@1294
|
83 # * SVG::Graph::Graph
|
Chris@1294
|
84 # * SVG::Graph::BarHorizontal
|
Chris@1294
|
85 # * SVG::Graph::Bar
|
Chris@1294
|
86 # * SVG::Graph::Line
|
Chris@1294
|
87 # * SVG::Graph::Pie
|
Chris@1294
|
88 # * SVG::Graph::Plot
|
Chris@1294
|
89 #
|
Chris@1294
|
90 # == Author
|
Chris@1294
|
91 #
|
Chris@1294
|
92 # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
Chris@1294
|
93 #
|
Chris@1294
|
94 # Copyright 2004 Sean E. Russell
|
Chris@1294
|
95 # This software is available under the Ruby license[LICENSE.txt]
|
Chris@1294
|
96 #
|
Chris@1294
|
97 class TimeSeries < Plot
|
Chris@1294
|
98 # In addition to the defaults set by Graph::initialize and
|
Chris@1294
|
99 # Plot::set_defaults, sets:
|
Chris@1294
|
100 # [x_label_format] '%Y-%m-%d %H:%M:%S'
|
Chris@1294
|
101 # [popup_format] '%Y-%m-%d %H:%M:%S'
|
Chris@1294
|
102 def set_defaults
|
Chris@1294
|
103 super
|
Chris@1294
|
104 init_with(
|
Chris@1294
|
105 #:max_time_span => '',
|
Chris@1294
|
106 :x_label_format => '%Y-%m-%d %H:%M:%S',
|
Chris@1294
|
107 :popup_format => '%Y-%m-%d %H:%M:%S'
|
Chris@1294
|
108 )
|
Chris@1294
|
109 end
|
Chris@1294
|
110
|
Chris@1294
|
111 # The format string use do format the X axis labels.
|
Chris@1294
|
112 # See Time::strformat
|
Chris@1294
|
113 attr_accessor :x_label_format
|
Chris@1294
|
114 # Use this to set the spacing between dates on the axis. The value
|
Chris@1294
|
115 # must be of the form
|
Chris@1294
|
116 # "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?"
|
Chris@1294
|
117 #
|
Chris@1294
|
118 # EG:
|
Chris@1294
|
119 #
|
Chris@1294
|
120 # graph.timescale_divisions = "2 weeks"
|
Chris@1294
|
121 #
|
Chris@1294
|
122 # will cause the chart to try to divide the X axis up into segments of
|
Chris@1294
|
123 # two week periods.
|
Chris@1294
|
124 attr_accessor :timescale_divisions
|
Chris@1294
|
125 # The formatting used for the popups. See x_label_format
|
Chris@1294
|
126 attr_accessor :popup_format
|
Chris@1294
|
127
|
Chris@1294
|
128 # Add data to the plot.
|
Chris@1294
|
129 #
|
Chris@1294
|
130 # d1 = [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
|
Chris@1294
|
131 # d2 = [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
|
Chris@1294
|
132 # # ("14:20",6)
|
Chris@1294
|
133 # graph.add_data(
|
Chris@1294
|
134 # :data => d1,
|
Chris@1294
|
135 # :title => 'One'
|
Chris@1294
|
136 # )
|
Chris@1294
|
137 # graph.add_data(
|
Chris@1294
|
138 # :data => d2,
|
Chris@1294
|
139 # :title => 'Two'
|
Chris@1294
|
140 # )
|
Chris@1294
|
141 #
|
Chris@1294
|
142 # Note that the data must be in time,value pairs, and that the date format
|
Chris@1294
|
143 # may be any date that is parseable by ParseDate.
|
Chris@1294
|
144 def add_data data
|
Chris@1294
|
145 @data = [] unless @data
|
Chris@1294
|
146
|
Chris@1294
|
147 raise "No data provided by #{@data.inspect}" unless data[:data] and
|
Chris@1294
|
148 data[:data].kind_of? Array
|
Chris@1294
|
149 raise "Data supplied must be x,y pairs! "+
|
Chris@1294
|
150 "The data provided contained an odd set of "+
|
Chris@1294
|
151 "data points" unless data[:data].length % 2 == 0
|
Chris@1294
|
152 return if data[:data].length == 0
|
Chris@1294
|
153
|
Chris@1294
|
154
|
Chris@1294
|
155 x = []
|
Chris@1294
|
156 y = []
|
Chris@1294
|
157 data[:data].each_index {|i|
|
Chris@1294
|
158 if i%2 == 0
|
Chris@1294
|
159 t = DateTime.parse( data[:data][i] ).to_time
|
Chris@1294
|
160 x << t.to_i
|
Chris@1294
|
161 else
|
Chris@1294
|
162 y << data[:data][i]
|
Chris@1294
|
163 end
|
Chris@1294
|
164 }
|
Chris@1294
|
165 sort( x, y )
|
Chris@1294
|
166 data[:data] = [x,y]
|
Chris@1294
|
167 @data << data
|
Chris@1294
|
168 end
|
Chris@1294
|
169
|
Chris@1294
|
170
|
Chris@1294
|
171 protected
|
Chris@1294
|
172
|
Chris@1294
|
173 def min_x_value=(value)
|
Chris@1294
|
174 @min_x_value = DateTime.parse( value ).to_time
|
Chris@1294
|
175 end
|
Chris@1294
|
176
|
Chris@1294
|
177
|
Chris@1294
|
178 def format x, y
|
Chris@1294
|
179 Time.at( x ).strftime( popup_format )
|
Chris@1294
|
180 end
|
Chris@1294
|
181
|
Chris@1294
|
182 def get_x_labels
|
Chris@1294
|
183 get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
|
Chris@1294
|
184 end
|
Chris@1294
|
185
|
Chris@1294
|
186 private
|
Chris@1294
|
187 def get_x_values
|
Chris@1294
|
188 rv = []
|
Chris@1294
|
189 min, max, scale_division = x_range
|
Chris@1294
|
190 if timescale_divisions
|
Chris@1294
|
191 timescale_divisions =~ /(\d+) ?(day|week|month|year|hour|minute|second)?/
|
Chris@1294
|
192 division_units = $2 ? $2 : "day"
|
Chris@1294
|
193 amount = $1.to_i
|
Chris@1294
|
194 if amount
|
Chris@1294
|
195 step = nil
|
Chris@1294
|
196 case division_units
|
Chris@1294
|
197 when "month"
|
Chris@1294
|
198 cur = min
|
Chris@1294
|
199 while cur < max
|
Chris@1294
|
200 rv << cur
|
Chris@1294
|
201 arr = Time.at( cur ).to_a
|
Chris@1294
|
202 arr[4] += amount
|
Chris@1294
|
203 if arr[4] > 12
|
Chris@1294
|
204 arr[5] += (arr[4] / 12).to_i
|
Chris@1294
|
205 arr[4] = (arr[4] % 12)
|
Chris@1294
|
206 end
|
Chris@1294
|
207 cur = Time.local(*arr).to_i
|
Chris@1294
|
208 end
|
Chris@1294
|
209 when "year"
|
Chris@1294
|
210 cur = min
|
Chris@1294
|
211 while cur < max
|
Chris@1294
|
212 rv << cur
|
Chris@1294
|
213 arr = Time.at( cur ).to_a
|
Chris@1294
|
214 arr[5] += amount
|
Chris@1294
|
215 cur = Time.local(*arr).to_i
|
Chris@1294
|
216 end
|
Chris@1294
|
217 when "week"
|
Chris@1294
|
218 step = 7 * 24 * 60 * 60 * amount
|
Chris@1294
|
219 when "day"
|
Chris@1294
|
220 step = 24 * 60 * 60 * amount
|
Chris@1294
|
221 when "hour"
|
Chris@1294
|
222 step = 60 * 60 * amount
|
Chris@1294
|
223 when "minute"
|
Chris@1294
|
224 step = 60 * amount
|
Chris@1294
|
225 when "second"
|
Chris@1294
|
226 step = amount
|
Chris@1294
|
227 end
|
Chris@1294
|
228 min.step( max, step ) {|v| rv << v} if step
|
Chris@1294
|
229
|
Chris@1294
|
230 return rv
|
Chris@1294
|
231 end
|
Chris@1294
|
232 end
|
Chris@1294
|
233 min.step( max, scale_division ) {|v| rv << v}
|
Chris@1294
|
234 return rv
|
Chris@1294
|
235 end
|
Chris@1294
|
236 end
|
Chris@1294
|
237 end
|
Chris@1294
|
238 end
|