svg-graph19 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,387 @@
1
+ require 'SVG/Graph/Plot'
2
+ TIME_PARSE_AVAIL = (RUBY_VERSION =~ /1\.9\./) ? true : false
3
+ if not TIME_PARSE_AVAIL then
4
+ require 'parsedate'
5
+ end
6
+
7
+ module SVG
8
+ module Graph
9
+ # === For creating SVG plots of scalar temporal data
10
+ #
11
+ # = Synopsis
12
+ #
13
+ # require 'SVG/Graph/Schedule'
14
+ #
15
+ # # Data sets are label, start, end tripples.
16
+ # data1 = [
17
+ # "Housesitting", "6/17/04", "6/19/04",
18
+ # "Summer Session", "6/15/04", "8/15/04",
19
+ # ]
20
+ #
21
+ # graph = SVG::Graph::Schedule.new( {
22
+ # :width => 640,
23
+ # :height => 480,
24
+ # :graph_title => title,
25
+ # :show_graph_title => true,
26
+ # :no_css => true,
27
+ # :scale_x_integers => true,
28
+ # :scale_y_integers => true,
29
+ # :min_x_value => 0,
30
+ # :min_y_value => 0,
31
+ # :show_data_labels => true,
32
+ # :show_x_guidelines => true,
33
+ # :show_x_title => true,
34
+ # :x_title => "Time",
35
+ # :stagger_x_labels => true,
36
+ # :stagger_y_labels => true,
37
+ # :x_label_format => "%m/%d/%y",
38
+ # })
39
+ #
40
+ # graph.add_data({
41
+ # :data => data1,
42
+ # :title => 'Data',
43
+ # })
44
+ #
45
+ # print graph.burn()
46
+ #
47
+ # = Description
48
+ #
49
+ # Produces a graph of temporal scalar data.
50
+ #
51
+ # = Examples
52
+ #
53
+ # http://www.germane-software/repositories/public/SVG/test/schedule.rb
54
+ #
55
+ # = Notes
56
+ #
57
+ # The default stylesheet handles upto 10 data sets, if you
58
+ # use more you must create your own stylesheet and add the
59
+ # additional settings for the extra data sets. You will know
60
+ # if you go over 10 data sets as they will have no style and
61
+ # be in black.
62
+ #
63
+ # Note that multiple data sets within the same chart can differ in
64
+ # length, and that the data in the datasets needn't be in order;
65
+ # they will be ordered by the plot along the X-axis.
66
+ #
67
+ # The dates must be parseable by ParseDate, but otherwise can be
68
+ # any order of magnitude (seconds within the hour, or years)
69
+ #
70
+ # = See also
71
+ #
72
+ # * SVG::Graph::Graph
73
+ # * SVG::Graph::BarHorizontal
74
+ # * SVG::Graph::Bar
75
+ # * SVG::Graph::Line
76
+ # * SVG::Graph::Pie
77
+ # * SVG::Graph::Plot
78
+ # * SVG::Graph::TimeSeries
79
+ #
80
+ # == Author
81
+ #
82
+ # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
83
+ #
84
+ # Copyright 2004 Sean E. Russell
85
+ # This software is available under the Ruby license[LICENSE.txt]
86
+ #
87
+ class Schedule < Graph
88
+ # In addition to the defaults set by Graph::initialize and
89
+ # Plot::set_defaults, sets:
90
+ # [x_label_format] '%Y-%m-%d %H:%M:%S'
91
+ # [popup_format] '%Y-%m-%d %H:%M:%S'
92
+ def set_defaults
93
+ init_with(
94
+ :x_label_format => '%Y-%m-%d %H:%M:%S',
95
+ :popup_format => '%Y-%m-%d %H:%M:%S',
96
+ :scale_x_divisions => false,
97
+ :scale_x_integers => false,
98
+ :bar_gap => true
99
+ )
100
+ end
101
+
102
+ # The format string use do format the X axis labels.
103
+ # See Time::strformat
104
+ attr_accessor :x_label_format
105
+ # Use this to set the spacing between dates on the axis. The value
106
+ # must be of the form
107
+ # "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?"
108
+ #
109
+ # EG:
110
+ #
111
+ # graph.timescale_divisions = "2 weeks"
112
+ #
113
+ # will cause the chart to try to divide the X axis up into segments of
114
+ # two week periods.
115
+ attr_accessor :timescale_divisions
116
+ # The formatting used for the popups. See x_label_format
117
+ attr_accessor :popup_format
118
+ attr_accessor :min_x_value
119
+ attr_accessor :scale_x_divisions
120
+ attr_accessor :scale_x_integers
121
+ attr_accessor :bar_gap
122
+
123
+ # Add data to the plot.
124
+ #
125
+ # # A data set with 1 point: Lunch from 12:30 to 14:00
126
+ # d1 = [ "Lunch", "12:30", "14:00" ]
127
+ # # A data set with 2 points: "Cats" runs from 5/11/03 to 7/15/04, and
128
+ # # "Henry V" runs from 6/12/03 to 8/20/03
129
+ # d2 = [ "Cats", "5/11/03", "7/15/04",
130
+ # "Henry V", "6/12/03", "8/20/03" ]
131
+ #
132
+ # graph.add_data(
133
+ # :data => d1,
134
+ # :title => 'Meetings'
135
+ # )
136
+ # graph.add_data(
137
+ # :data => d2,
138
+ # :title => 'Plays'
139
+ # )
140
+ #
141
+ # Note that the data must be in time,value pairs, and that the date format
142
+ # may be any date that is parseable by ParseDate.
143
+ # Also note that, in this example, we're mixing scales; the data from d1
144
+ # will probably not be discernable if both data sets are plotted on the same
145
+ # graph, since d1 is too granular.
146
+ def add_data data
147
+ @data = [] unless @data
148
+
149
+ raise "No data provided by #{conf.inspect}" unless data[:data] and
150
+ data[:data].kind_of? Array
151
+ raise "Data supplied must be title,from,to tripples! "+
152
+ "The data provided contained an odd set of "+
153
+ "data points" unless data[:data].length % 3 == 0
154
+ return if data[:data].length == 0
155
+
156
+
157
+ y = []
158
+ x_start = []
159
+ x_end = []
160
+ data[:data].each_index {|i|
161
+ im3 = i%3
162
+ if im3 == 0
163
+ y << data[:data][i]
164
+ else
165
+ if TIME_PARSE_AVAIL then
166
+ arr = DateTime.parse(data[:data][i])
167
+ t = arr.to_time
168
+ else
169
+ arr = ParseDate.parsedate( data[:data][i] )
170
+ t = Time.local( *arr[0,6].compact )
171
+ end
172
+ (im3 == 1 ? x_start : x_end) << t.to_i
173
+ end
174
+ }
175
+ sort( x_start, x_end, y )
176
+ @data = [x_start, x_end, y ]
177
+ end
178
+
179
+
180
+ protected
181
+
182
+ def min_x_value=(value)
183
+ if TIME_PARSE_AVAIL then
184
+ arr = Time.parse(value)
185
+ t = arr.to_time
186
+ else
187
+ arr = ParseDate.parsedate( value )
188
+ t = Time.local( *arr[0,6].compact )
189
+ end
190
+ @min_x_value = t.to_i
191
+ end
192
+
193
+
194
+ def format x, y
195
+ Time.at( x ).strftime( popup_format )
196
+ end
197
+
198
+ def get_x_labels
199
+ rv = get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
200
+ end
201
+
202
+ def y_label_offset( height )
203
+ height / -2.0
204
+ end
205
+
206
+ def get_y_labels
207
+ @data[2]
208
+ end
209
+
210
+ def draw_data
211
+ fieldheight = field_height
212
+ fieldwidth = field_width
213
+
214
+ bargap = bar_gap ? (fieldheight < 10 ? fieldheight / 2 : 10) : 0
215
+ subbar_height = fieldheight - bargap
216
+
217
+ field_count = 1
218
+ y_mod = (subbar_height / 2) + (font_size / 2)
219
+ min,max,div = x_range
220
+ scale = (@graph_width.to_f - font_size*2) / (max-min)
221
+ @data[0].each_index { |i|
222
+ x_start = @data[0][i]
223
+ x_end = @data[1][i]
224
+ y = @graph_height - (fieldheight * field_count)
225
+ bar_width = (x_end-x_start) * scale
226
+ bar_start = x_start * scale - (min * scale)
227
+
228
+ @graph.add_element( "rect", {
229
+ "x" => bar_start.to_s,
230
+ "y" => y.to_s,
231
+ "width" => bar_width.to_s,
232
+ "height" => subbar_height.to_s,
233
+ "class" => "fill#{field_count+1}"
234
+ })
235
+ field_count += 1
236
+ }
237
+ end
238
+
239
+ def get_css
240
+ return <<EOL
241
+ /* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
242
+ .key1,.fill1{
243
+ fill: #ff0000;
244
+ fill-opacity: 0.5;
245
+ stroke: none;
246
+ stroke-width: 0.5px;
247
+ }
248
+ .key2,.fill2{
249
+ fill: #0000ff;
250
+ fill-opacity: 0.5;
251
+ stroke: none;
252
+ stroke-width: 1px;
253
+ }
254
+ .key3,.fill3{
255
+ fill: #00ff00;
256
+ fill-opacity: 0.5;
257
+ stroke: none;
258
+ stroke-width: 1px;
259
+ }
260
+ .key4,.fill4{
261
+ fill: #ffcc00;
262
+ fill-opacity: 0.5;
263
+ stroke: none;
264
+ stroke-width: 1px;
265
+ }
266
+ .key5,.fill5{
267
+ fill: #00ccff;
268
+ fill-opacity: 0.5;
269
+ stroke: none;
270
+ stroke-width: 1px;
271
+ }
272
+ .key6,.fill6{
273
+ fill: #ff00ff;
274
+ fill-opacity: 0.5;
275
+ stroke: none;
276
+ stroke-width: 1px;
277
+ }
278
+ .key7,.fill7{
279
+ fill: #00ffff;
280
+ fill-opacity: 0.5;
281
+ stroke: none;
282
+ stroke-width: 1px;
283
+ }
284
+ .key8,.fill8{
285
+ fill: #ffff00;
286
+ fill-opacity: 0.5;
287
+ stroke: none;
288
+ stroke-width: 1px;
289
+ }
290
+ .key9,.fill9{
291
+ fill: #cc6666;
292
+ fill-opacity: 0.5;
293
+ stroke: none;
294
+ stroke-width: 1px;
295
+ }
296
+ .key10,.fill10{
297
+ fill: #663399;
298
+ fill-opacity: 0.5;
299
+ stroke: none;
300
+ stroke-width: 1px;
301
+ }
302
+ .key11,.fill11{
303
+ fill: #339900;
304
+ fill-opacity: 0.5;
305
+ stroke: none;
306
+ stroke-width: 1px;
307
+ }
308
+ .key12,.fill12{
309
+ fill: #9966FF;
310
+ fill-opacity: 0.5;
311
+ stroke: none;
312
+ stroke-width: 1px;
313
+ }
314
+ EOL
315
+ end
316
+
317
+ private
318
+ def x_range
319
+ max_value = [ @data[0][-1], @data[1].max ].max
320
+ min_value = [ @data[0][0], @data[1].min ].min
321
+ min_value = min_value<min_x_value ? min_value : min_x_value if min_x_value
322
+
323
+ range = max_value - min_value
324
+ right_pad = range == 0 ? 10 : range / 20.0
325
+ scale_range = (max_value + right_pad) - min_value
326
+
327
+ scale_division = scale_x_divisions || (scale_range / 10.0)
328
+
329
+ if scale_x_integers
330
+ scale_division = scale_division < 1 ? 1 : scale_division.round
331
+ end
332
+
333
+ [min_value, max_value, scale_division]
334
+ end
335
+
336
+ def get_x_values
337
+ rv = []
338
+ min, max, scale_division = x_range
339
+ if timescale_divisions
340
+ timescale_divisions =~ /(\d+) ?(days|weeks|months|years|hours|minutes|seconds)?/
341
+ division_units = $2 ? $2 : "days"
342
+ amount = $1.to_i
343
+ if amount
344
+ step = nil
345
+ case division_units
346
+ when "months"
347
+ cur = min
348
+ while cur < max
349
+ rv << cur
350
+ arr = Time.at( cur ).to_a
351
+ arr[4] += amount
352
+ if arr[4] > 12
353
+ arr[5] += (arr[4] / 12).to_i
354
+ arr[4] = (arr[4] % 12)
355
+ end
356
+ cur = Time.local(*arr).to_i
357
+ end
358
+ when "years"
359
+ cur = min
360
+ while cur < max
361
+ rv << cur
362
+ arr = Time.at( cur ).to_a
363
+ arr[5] += amount
364
+ cur = Time.local(*arr).to_i
365
+ end
366
+ when "weeks"
367
+ step = 7 * 24 * 60 * 60 * amount
368
+ when "days"
369
+ step = 24 * 60 * 60 * amount
370
+ when "hours"
371
+ step = 60 * 60 * amount
372
+ when "minutes"
373
+ step = 60 * amount
374
+ when "seconds"
375
+ step = amount
376
+ end
377
+ min.step( max, step ) {|v| rv << v} if step
378
+
379
+ return rv
380
+ end
381
+ end
382
+ min.step( max, scale_division ) {|v| rv << v}
383
+ return rv
384
+ end
385
+ end
386
+ end
387
+ end
@@ -0,0 +1,255 @@
1
+ require 'SVG/Graph/Plot'
2
+ TIME_PARSE_AVAIL = (RUBY_VERSION =~ /1\.9\./) ? true : false
3
+ if not TIME_PARSE_AVAIL then
4
+ require 'parsedate'
5
+ end
6
+
7
+ module SVG
8
+ module Graph
9
+ # === For creating SVG plots of scalar temporal data
10
+ #
11
+ # = Synopsis
12
+ #
13
+ # require 'SVG/Graph/TimeSeriess'
14
+ #
15
+ # # Data sets are x,y pairs
16
+ # data1 = ["6/17/72", 11, "1/11/72", 7, "4/13/04 17:31", 11,
17
+ # "9/11/01", 9, "9/1/85", 2, "9/1/88", 1, "1/15/95", 13]
18
+ # data2 = ["8/1/73", 18, "3/1/77", 15, "10/1/98", 4,
19
+ # "5/1/02", 14, "3/1/95", 6, "8/1/91", 12, "12/1/87", 6,
20
+ # "5/1/84", 17, "10/1/80", 12]
21
+ #
22
+ # graph = SVG::Graph::TimeSeries.new( {
23
+ # :width => 640,
24
+ # :height => 480,
25
+ # :graph_title => title,
26
+ # :show_graph_title => true,
27
+ # :no_css => true,
28
+ # :key => true,
29
+ # :scale_x_integers => true,
30
+ # :scale_y_integers => true,
31
+ # :min_x_value => 0,
32
+ # :min_y_value => 0,
33
+ # :show_data_labels => true,
34
+ # :show_x_guidelines => true,
35
+ # :show_x_title => true,
36
+ # :x_title => "Time",
37
+ # :show_y_title => true,
38
+ # :y_title => "Ice Cream Cones",
39
+ # :y_title_text_direction => :bt,
40
+ # :stagger_x_labels => true,
41
+ # :x_label_format => "%m/%d/%y",
42
+ # })
43
+ #
44
+ # graph.add_data({
45
+ # :data => projection
46
+ # :title => 'Projected',
47
+ # })
48
+ #
49
+ # graph.add_data({
50
+ # :data => actual,
51
+ # :title => 'Actual',
52
+ # })
53
+ #
54
+ # print graph.burn()
55
+ #
56
+ # = Description
57
+ #
58
+ # Produces a graph of temporal scalar data.
59
+ #
60
+ # = Examples
61
+ #
62
+ # http://www.germane-software/repositories/public/SVG/test/timeseries.rb
63
+ #
64
+ # = Notes
65
+ #
66
+ # The default stylesheet handles upto 10 data sets, if you
67
+ # use more you must create your own stylesheet and add the
68
+ # additional settings for the extra data sets. You will know
69
+ # if you go over 10 data sets as they will have no style and
70
+ # be in black.
71
+ #
72
+ # Unlike the other types of charts, data sets must contain x,y pairs:
73
+ #
74
+ # [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
75
+ # [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
76
+ # # ("14:20",6)
77
+ #
78
+ # Note that multiple data sets within the same chart can differ in length,
79
+ # and that the data in the datasets needn't be in order; they will be ordered
80
+ # by the plot along the X-axis.
81
+ #
82
+ # The dates must be parseable by ParseDate, but otherwise can be
83
+ # any order of magnitude (seconds within the hour, or years)
84
+ #
85
+ # = See also
86
+ #
87
+ # * SVG::Graph::Graph
88
+ # * SVG::Graph::BarHorizontal
89
+ # * SVG::Graph::Bar
90
+ # * SVG::Graph::Line
91
+ # * SVG::Graph::Pie
92
+ # * SVG::Graph::Plot
93
+ #
94
+ # == Author
95
+ #
96
+ # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
97
+ #
98
+ # Copyright 2004 Sean E. Russell
99
+ # This software is available under the Ruby license[LICENSE.txt]
100
+ #
101
+ class TimeSeries < Plot
102
+ # In addition to the defaults set by Graph::initialize and
103
+ # Plot::set_defaults, sets:
104
+ # [x_label_format] '%Y-%m-%d %H:%M:%S'
105
+ # [popup_format] '%Y-%m-%d %H:%M:%S'
106
+ def set_defaults
107
+ super
108
+ init_with(
109
+ #:max_time_span => '',
110
+ :x_label_format => '%Y-%m-%d %H:%M:%S',
111
+ :popup_format => '%Y-%m-%d %H:%M:%S'
112
+ )
113
+ end
114
+
115
+ # The format string use do format the X axis labels.
116
+ # See Time::strformat
117
+ attr_accessor :x_label_format
118
+ # Use this to set the spacing between dates on the axis. The value
119
+ # must be of the form
120
+ # "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?"
121
+ #
122
+ # EG:
123
+ #
124
+ # graph.timescale_divisions = "2 weeks"
125
+ #
126
+ # will cause the chart to try to divide the X axis up into segments of
127
+ # two week periods.
128
+ attr_accessor :timescale_divisions
129
+ # The formatting used for the popups. See x_label_format
130
+ attr_accessor :popup_format
131
+
132
+ # Add data to the plot.
133
+ #
134
+ # d1 = [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
135
+ # d2 = [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
136
+ # # ("14:20",6)
137
+ # graph.add_data(
138
+ # :data => d1,
139
+ # :title => 'One'
140
+ # )
141
+ # graph.add_data(
142
+ # :data => d2,
143
+ # :title => 'Two'
144
+ # )
145
+ #
146
+ # Note that the data must be in time,value pairs, and that the date format
147
+ # may be any date that is parseable by ParseDate.
148
+ def add_data data
149
+ @data = [] unless @data
150
+
151
+ raise "No data provided by #{@data.inspect}" unless data[:data] and
152
+ data[:data].kind_of? Array
153
+ raise "Data supplied must be x,y pairs! "+
154
+ "The data provided contained an odd set of "+
155
+ "data points" unless data[:data].length % 2 == 0
156
+ return if data[:data].length == 0
157
+
158
+
159
+ x = []
160
+ y = []
161
+ data[:data].each_index {|i|
162
+ if i%2 == 0
163
+ if TIME_PARSE_AVAIL then
164
+ arr = DateTime.parse(data[:data][i])
165
+ t = arr.to_time
166
+ else
167
+ arr = ParseDate.parsedate( data[:data][i] )
168
+ t = Time.local( *arr[0,6].compact )
169
+ end
170
+ x << t.to_i
171
+ else
172
+ y << data[:data][i]
173
+ end
174
+ }
175
+ sort( x, y )
176
+ data[:data] = [x,y]
177
+ @data << data
178
+ end
179
+
180
+
181
+ protected
182
+
183
+ def min_x_value=(value)
184
+ if TIME_PARSE_AVAIL then
185
+ arr = Time.parse(value)
186
+ t = arr.to_time
187
+ else
188
+ arr = ParseDate.parsedate( value )
189
+ t = Time.local( *arr[0,6].compact )
190
+ end
191
+ @min_x_value = t.to_i
192
+ end
193
+
194
+
195
+ def format x, y
196
+ Time.at( x ).strftime( popup_format )
197
+ end
198
+
199
+ def get_x_labels
200
+ get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
201
+ end
202
+
203
+ private
204
+ def get_x_values
205
+ rv = []
206
+ min, max, scale_division = x_range
207
+ if timescale_divisions
208
+ timescale_divisions =~ /(\d+) ?(day|week|month|year|hour|minute|second)?/
209
+ division_units = $2 ? $2 : "day"
210
+ amount = $1.to_i
211
+ if amount
212
+ step = nil
213
+ case division_units
214
+ when "month"
215
+ cur = min
216
+ while cur < max
217
+ rv << cur
218
+ arr = Time.at( cur ).to_a
219
+ arr[4] += amount
220
+ if arr[4] > 12
221
+ arr[5] += (arr[4] / 12).to_i
222
+ arr[4] = (arr[4] % 12)
223
+ end
224
+ cur = Time.local(*arr).to_i
225
+ end
226
+ when "year"
227
+ cur = min
228
+ while cur < max
229
+ rv << cur
230
+ arr = Time.at( cur ).to_a
231
+ arr[5] += amount
232
+ cur = Time.local(*arr).to_i
233
+ end
234
+ when "week"
235
+ step = 7 * 24 * 60 * 60 * amount
236
+ when "day"
237
+ step = 24 * 60 * 60 * amount
238
+ when "hour"
239
+ step = 60 * 60 * amount
240
+ when "minute"
241
+ step = 60 * amount
242
+ when "second"
243
+ step = amount
244
+ end
245
+ min.step( max, step ) {|v| rv << v} if step
246
+
247
+ return rv
248
+ end
249
+ end
250
+ min.step( max, scale_division ) {|v| rv << v}
251
+ return rv
252
+ end
253
+ end
254
+ end
255
+ end