Class SVG::Graph::Schedule
In: lib/SVG/Graph/Schedule.rb
Parent: Graph

For creating SVG plots of scalar temporal data

Synopsis

  require 'SVG/Graph/Schedule'

  # Data sets are label, start, end tripples.
  data1 = [
    "Housesitting", "6/17/04", "6/19/04",
    "Summer Session", "6/15/04", "8/15/04",
  ]

  graph = SVG::Graph::Schedule.new( {
    :width => 640,
    :height => 480,
    :graph_title => title,
    :show_graph_title => true,
    :no_css => true,
    :scale_x_integers => true,
    :scale_y_integers => true,
    :min_x_value => 0,
    :min_y_value => 0,
    :show_data_labels => true,
    :show_x_guidelines => true,
    :show_x_title => true,
    :x_title => "Time",
    :stagger_x_labels => true,
    :stagger_y_labels => true,
    :x_label_format => "%m/%d/%y",
  })

  graph.add_data({
          :data => data1,
    :title => 'Data',
  })

  print graph.burn()

Description

Produces a graph of temporal scalar data.

Examples

www.germane-software/repositories/public/SVG/test/schedule.rb

Notes

The default stylesheet handles upto 10 data sets, if you use more you must create your own stylesheet and add the additional settings for the extra data sets. You will know if you go over 10 data sets as they will have no style and be in black.

Note that multiple data sets within the same chart can differ in length, and that the data in the datasets needn‘t be in order; they will be ordered by the plot along the X-axis.

The dates must be parseable by ParseDate, but otherwise can be any order of magnitude (seconds within the hour, or years)

See also

Author

Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>

Copyright 2004 Sean E. Russell This software is available under the Ruby license

Methods

Attributes

bar_gap  [RW] 
min_x_value  [RW] 
popup_format  [RW]  The formatting used for the popups. See x_label_format
scale_x_divisions  [RW] 
scale_x_integers  [RW] 
timescale_divisions  [RW]  Use this to set the spacing between dates on the axis. The value must be of the form "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?"

EG:

  graph.timescale_divisions = "2 weeks"

will cause the chart to try to divide the X axis up into segments of two week periods.

x_label_format  [RW]  The format string use do format the X axis labels. See Time::strformat

Public Instance methods

Add data to the plot.

  # A data set with 1 point: Lunch from 12:30 to 14:00
  d1 = [ "Lunch", "12:30", "14:00" ]
  # A data set with 2 points: "Cats" runs from 5/11/03 to 7/15/04, and
  #                           "Henry V" runs from 6/12/03 to 8/20/03
  d2 = [ "Cats", "5/11/03", "7/15/04",
         "Henry V", "6/12/03", "8/20/03" ]

  graph.add_data(
    :data => d1,
    :title => 'Meetings'
  )
  graph.add_data(
    :data => d2,
    :title => 'Plays'
  )

Note that the data must be in time,value pairs, and that the date format may be any date that is parseable by ParseDate. Also note that, in this example, we‘re mixing scales; the data from d1 will probably not be discernable if both data sets are plotted on the same graph, since d1 is too granular.

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 146
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

In addition to the defaults set by Graph::initialize and Plot::set_defaults, sets:

x_label_format
’%Y-%m-%d %H:%M:%S‘
popup_format
’%Y-%m-%d %H:%M:%S‘

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 92
 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

Protected Instance methods

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 210
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

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 194
194:       def format x, y
195:         Time.at( x ).strftime( popup_format )
196:       end

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 239
239:       def get_css
240:         return "/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */\n.key1,.fill1{\n        fill: #ff0000;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 0.5px;   \n}\n.key2,.fill2{\n        fill: #0000ff;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key3,.fill3{\n        fill: #00ff00;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key4,.fill4{\n        fill: #ffcc00;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key5,.fill5{\n        fill: #00ccff;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key6,.fill6{\n        fill: #ff00ff;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key7,.fill7{\n        fill: #00ffff;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key8,.fill8{\n        fill: #ffff00;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key9,.fill9{\n        fill: #cc6666;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key10,.fill10{\n        fill: #663399;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key11,.fill11{\n        fill: #339900;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n.key12,.fill12{\n        fill: #9966FF;\n        fill-opacity: 0.5;\n        stroke: none;\n        stroke-width: 1px;     \n}\n"
241:       end

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 198
198:       def get_x_labels
199:         rv = get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
200:       end

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 206
206:       def get_y_labels
207:         @data[2]
208:       end

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 182
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

[Source]

     # File lib/SVG/Graph/Schedule.rb, line 202
202:       def y_label_offset( height )
203:         height / -2.0
204:       end

[Validate]