Class SVG::Graph::Schedule
In: lib/SVG/Graph/Schedule.rb
Parent: Graph
Graph Pie Plot BarBase Schedule Line TimeSeries Bar BarHorizontal REXML Graph SVG dot/f_0.png

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 143
      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
            arr = ParseDate.parsedate( data[:data][i] )
            t = Time.local( *arr[0,6].compact )
            (im3 == 1 ? x_start : x_end) << t.to_i
          end
        }
        sort( x_start, x_end, y )
        @data = [x_start, x_end, y ]
      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 89
      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

[Source]

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

[Source]

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

[Source]

# File lib/SVG/Graph/Schedule.rb, line 225
      def get_css
        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"
      end

[Source]

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

[Source]

# File lib/SVG/Graph/Schedule.rb, line 192
      def get_y_labels
        @data[2]
      end

[Source]

# File lib/SVG/Graph/Schedule.rb, line 174
      def min_x_value=(value)
        arr = ParseDate.parsedate( value )
        @min_x_value = Time.local( *arr[0,6].compact ).to_i
      end

[Source]

# File lib/SVG/Graph/Schedule.rb, line 188
      def y_label_offset( height )
        height / -2.0
      end

[Validate]