class SVG::Graph::Schedule

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

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(data) click to toggle source

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.

# File lib/SVG/Graph/Schedule.rb, line 146
def add_data data
  @data = [] unless @data
 
  raise "No data provided by #{conf.inspect}" unless data[:data] and
                                              data[:data].kind_of? Array
  raise "Data supplied must be title,from,to tripples!  "+
    "The data provided contained an odd set of "+
    "data points" unless data[:data].length % 3 == 0
  return if data[:data].length == 0


  y = []
  x_start = []
  x_end = []
  data[:data].each_index {|i|
    im3 = i%3
    if im3 == 0
      y << data[:data][i]
    else
      if TIME_PARSE_AVAIL then
        arr = DateTime.parse(data[:data][i])
        t = arr.to_time
      else
        arr = ParseDate.parsedate( data[:data][i] )
        t = Time.local( *arr[0,6].compact )
      end
      (im3 == 1 ? x_start : x_end) << t.to_i
    end
  }
  sort( x_start, x_end, y )
  @data = [x_start, x_end, y ]
end
set_defaults() click to toggle source

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'

# File lib/SVG/Graph/Schedule.rb, line 92
def set_defaults
  init_with(
    :x_label_format     => '%Y-%m-%d %H:%M:%S',
    :popup_format       => '%Y-%m-%d %H:%M:%S',
    :scale_x_divisions  => false,
    :scale_x_integers   => false,
    :bar_gap            => true
  )
end

Protected Instance Methods

draw_data() click to toggle source
# File lib/SVG/Graph/Schedule.rb, line 210
def draw_data
  fieldheight = field_height
  fieldwidth = field_width

  bargap = bar_gap ? (fieldheight < 10 ? fieldheight / 2 : 10) : 0
  subbar_height = fieldheight - bargap
  
  field_count = 1
  y_mod = (subbar_height / 2) + (font_size / 2)
  min,max,div = x_range
  scale = (@graph_width.to_f - font_size*2) / (max-min)
  @data[0].each_index { |i|
    x_start = @data[0][i]
    x_end = @data[1][i]
    y = @graph_height - (fieldheight * field_count)
    bar_width = (x_end-x_start) * scale
    bar_start = x_start * scale - (min * scale)
  
    @graph.add_element( "rect", {
      "x" => bar_start.to_s,
      "y" => y.to_s,
      "width" => bar_width.to_s,
      "height" => subbar_height.to_s,
      "class" => "fill#{field_count+1}"
    })
    field_count += 1
  }
end
format(x, y) click to toggle source
# File lib/SVG/Graph/Schedule.rb, line 194
def format x, y
  Time.at( x ).strftime( popup_format )
end
get_css() click to toggle source
# File lib/SVG/Graph/Schedule.rb, line 239
      def get_css
        return <<EOL
/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
.key1,.fill1{
        fill: #ff0000;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 0.5px;   
}
.key2,.fill2{
        fill: #0000ff;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key3,.fill3{
        fill: #00ff00;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key4,.fill4{
        fill: #ffcc00;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key5,.fill5{
        fill: #00ccff;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key6,.fill6{
        fill: #ff00ff;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key7,.fill7{
        fill: #00ffff;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key8,.fill8{
        fill: #ffff00;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key9,.fill9{
        fill: #cc6666;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key10,.fill10{
        fill: #663399;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key11,.fill11{
        fill: #339900;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
.key12,.fill12{
        fill: #9966FF;
        fill-opacity: 0.5;
        stroke: none;
        stroke-width: 1px;     
}
EOL
      end
get_x_labels() click to toggle source
# File lib/SVG/Graph/Schedule.rb, line 198
def get_x_labels
  rv = get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
end
get_y_labels() click to toggle source
# File lib/SVG/Graph/Schedule.rb, line 206
def get_y_labels
  @data[2]
end
min_x_value=(value) click to toggle source
# File lib/SVG/Graph/Schedule.rb, line 182
def min_x_value=(value)
  if TIME_PARSE_AVAIL then
    arr = Time.parse(value)
    t = arr.to_time
  else
    arr = ParseDate.parsedate( value )
    t = Time.local( *arr[0,6].compact )
  end
  @min_x_value = t.to_i
end
y_label_offset( height ) click to toggle source
# File lib/SVG/Graph/Schedule.rb, line 202
def y_label_offset( height )
  height / -2.0
end