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