Chris@0: require 'SVG/Graph/Plot' Chris@0: require 'parsedate' Chris@0: Chris@0: module SVG Chris@0: module Graph Chris@0: # === For creating SVG plots of scalar temporal data Chris@0: # Chris@0: # = Synopsis Chris@0: # Chris@0: # require 'SVG/Graph/Schedule' Chris@0: # Chris@0: # # Data sets are label, start, end tripples. Chris@0: # data1 = [ Chris@0: # "Housesitting", "6/17/04", "6/19/04", Chris@0: # "Summer Session", "6/15/04", "8/15/04", Chris@0: # ] Chris@0: # Chris@0: # graph = SVG::Graph::Schedule.new( { Chris@0: # :width => 640, Chris@0: # :height => 480, Chris@0: # :graph_title => title, Chris@0: # :show_graph_title => true, Chris@0: # :no_css => true, Chris@0: # :scale_x_integers => true, Chris@0: # :scale_y_integers => true, Chris@0: # :min_x_value => 0, Chris@0: # :min_y_value => 0, Chris@0: # :show_data_labels => true, Chris@0: # :show_x_guidelines => true, Chris@0: # :show_x_title => true, Chris@0: # :x_title => "Time", Chris@0: # :stagger_x_labels => true, Chris@0: # :stagger_y_labels => true, Chris@0: # :x_label_format => "%m/%d/%y", Chris@0: # }) Chris@0: # Chris@0: # graph.add_data({ Chris@0: # :data => data1, Chris@0: # :title => 'Data', Chris@0: # }) Chris@0: # Chris@0: # print graph.burn() Chris@0: # Chris@0: # = Description Chris@0: # Chris@0: # Produces a graph of temporal scalar data. Chris@0: # Chris@0: # = Examples Chris@0: # Chris@0: # http://www.germane-software/repositories/public/SVG/test/schedule.rb Chris@0: # Chris@0: # = Notes Chris@0: # Chris@0: # The default stylesheet handles upto 10 data sets, if you Chris@0: # use more you must create your own stylesheet and add the Chris@0: # additional settings for the extra data sets. You will know Chris@0: # if you go over 10 data sets as they will have no style and Chris@0: # be in black. Chris@0: # Chris@0: # Note that multiple data sets within the same chart can differ in Chris@0: # length, and that the data in the datasets needn't be in order; Chris@0: # they will be ordered by the plot along the X-axis. Chris@0: # Chris@0: # The dates must be parseable by ParseDate, but otherwise can be Chris@0: # any order of magnitude (seconds within the hour, or years) Chris@0: # Chris@0: # = See also Chris@0: # Chris@0: # * SVG::Graph::Graph Chris@0: # * SVG::Graph::BarHorizontal Chris@0: # * SVG::Graph::Bar Chris@0: # * SVG::Graph::Line Chris@0: # * SVG::Graph::Pie Chris@0: # * SVG::Graph::Plot Chris@0: # * SVG::Graph::TimeSeries Chris@0: # Chris@0: # == Author Chris@0: # Chris@0: # Sean E. Russell Chris@0: # Chris@0: # Copyright 2004 Sean E. Russell Chris@0: # This software is available under the Ruby license[LICENSE.txt] Chris@0: # Chris@0: class Schedule < Graph Chris@0: # In addition to the defaults set by Graph::initialize and Chris@0: # Plot::set_defaults, sets: Chris@0: # [x_label_format] '%Y-%m-%d %H:%M:%S' Chris@0: # [popup_format] '%Y-%m-%d %H:%M:%S' Chris@0: def set_defaults Chris@0: init_with( Chris@0: :x_label_format => '%Y-%m-%d %H:%M:%S', Chris@0: :popup_format => '%Y-%m-%d %H:%M:%S', Chris@0: :scale_x_divisions => false, Chris@0: :scale_x_integers => false, Chris@0: :bar_gap => true Chris@0: ) Chris@0: end Chris@0: Chris@0: # The format string use do format the X axis labels. Chris@0: # See Time::strformat Chris@0: attr_accessor :x_label_format Chris@0: # Use this to set the spacing between dates on the axis. The value Chris@0: # must be of the form Chris@0: # "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?" Chris@0: # Chris@0: # EG: Chris@0: # Chris@0: # graph.timescale_divisions = "2 weeks" Chris@0: # Chris@0: # will cause the chart to try to divide the X axis up into segments of Chris@0: # two week periods. Chris@0: attr_accessor :timescale_divisions Chris@0: # The formatting used for the popups. See x_label_format Chris@0: attr_accessor :popup_format Chris@0: attr_accessor :min_x_value Chris@0: attr_accessor :scale_x_divisions Chris@0: attr_accessor :scale_x_integers Chris@0: attr_accessor :bar_gap Chris@0: Chris@0: # Add data to the plot. Chris@0: # Chris@0: # # A data set with 1 point: Lunch from 12:30 to 14:00 Chris@0: # d1 = [ "Lunch", "12:30", "14:00" ] Chris@0: # # A data set with 2 points: "Cats" runs from 5/11/03 to 7/15/04, and Chris@0: # # "Henry V" runs from 6/12/03 to 8/20/03 Chris@0: # d2 = [ "Cats", "5/11/03", "7/15/04", Chris@0: # "Henry V", "6/12/03", "8/20/03" ] Chris@0: # Chris@0: # graph.add_data( Chris@0: # :data => d1, Chris@0: # :title => 'Meetings' Chris@0: # ) Chris@0: # graph.add_data( Chris@0: # :data => d2, Chris@0: # :title => 'Plays' Chris@0: # ) Chris@0: # Chris@0: # Note that the data must be in time,value pairs, and that the date format Chris@0: # may be any date that is parseable by ParseDate. Chris@0: # Also note that, in this example, we're mixing scales; the data from d1 Chris@0: # will probably not be discernable if both data sets are plotted on the same Chris@0: # graph, since d1 is too granular. Chris@0: def add_data data Chris@0: @data = [] unless @data Chris@0: Chris@0: raise "No data provided by #{conf.inspect}" unless data[:data] and Chris@0: data[:data].kind_of? Array Chris@0: raise "Data supplied must be title,from,to tripples! "+ Chris@0: "The data provided contained an odd set of "+ Chris@0: "data points" unless data[:data].length % 3 == 0 Chris@0: return if data[:data].length == 0 Chris@0: Chris@0: Chris@0: y = [] Chris@0: x_start = [] Chris@0: x_end = [] Chris@0: data[:data].each_index {|i| Chris@0: im3 = i%3 Chris@0: if im3 == 0 Chris@0: y << data[:data][i] Chris@0: else Chris@0: arr = ParseDate.parsedate( data[:data][i] ) Chris@0: t = Time.local( *arr[0,6].compact ) Chris@0: (im3 == 1 ? x_start : x_end) << t.to_i Chris@0: end Chris@0: } Chris@0: sort( x_start, x_end, y ) Chris@0: @data = [x_start, x_end, y ] Chris@0: end Chris@0: Chris@0: Chris@0: protected Chris@0: Chris@0: def min_x_value=(value) Chris@0: arr = ParseDate.parsedate( value ) Chris@0: @min_x_value = Time.local( *arr[0,6].compact ).to_i Chris@0: end Chris@0: Chris@0: Chris@0: def format x, y Chris@0: Time.at( x ).strftime( popup_format ) Chris@0: end Chris@0: Chris@0: def get_x_labels Chris@0: rv = get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) } Chris@0: end Chris@0: Chris@0: def y_label_offset( height ) Chris@0: height / -2.0 Chris@0: end Chris@0: Chris@0: def get_y_labels Chris@0: @data[2] Chris@0: end Chris@0: Chris@0: def draw_data Chris@0: fieldheight = field_height Chris@0: fieldwidth = field_width Chris@0: Chris@0: bargap = bar_gap ? (fieldheight < 10 ? fieldheight / 2 : 10) : 0 Chris@0: subbar_height = fieldheight - bargap Chris@0: Chris@0: field_count = 1 Chris@0: y_mod = (subbar_height / 2) + (font_size / 2) Chris@0: min,max,div = x_range Chris@0: scale = (@graph_width.to_f - font_size*2) / (max-min) Chris@0: @data[0].each_index { |i| Chris@0: x_start = @data[0][i] Chris@0: x_end = @data[1][i] Chris@0: y = @graph_height - (fieldheight * field_count) Chris@0: bar_width = (x_end-x_start) * scale Chris@0: bar_start = x_start * scale - (min * scale) Chris@0: Chris@0: @graph.add_element( "rect", { Chris@0: "x" => bar_start.to_s, Chris@0: "y" => y.to_s, Chris@0: "width" => bar_width.to_s, Chris@0: "height" => subbar_height.to_s, Chris@0: "class" => "fill#{field_count+1}" Chris@0: }) Chris@0: field_count += 1 Chris@0: } Chris@0: end Chris@0: Chris@0: def get_css Chris@0: return < 12 Chris@0: arr[5] += (arr[4] / 12).to_i Chris@0: arr[4] = (arr[4] % 12) Chris@0: end Chris@0: cur = Time.local(*arr).to_i Chris@0: end Chris@0: when "years" Chris@0: cur = min Chris@0: while cur < max Chris@0: rv << cur Chris@0: arr = Time.at( cur ).to_a Chris@0: arr[5] += amount Chris@0: cur = Time.local(*arr).to_i Chris@0: end Chris@0: when "weeks" Chris@0: step = 7 * 24 * 60 * 60 * amount Chris@0: when "days" Chris@0: step = 24 * 60 * 60 * amount Chris@0: when "hours" Chris@0: step = 60 * 60 * amount Chris@0: when "minutes" Chris@0: step = 60 * amount Chris@0: when "seconds" Chris@0: step = amount Chris@0: end Chris@0: min.step( max, step ) {|v| rv << v} if step Chris@0: Chris@0: return rv Chris@0: end Chris@0: end Chris@0: min.step( max, scale_division ) {|v| rv << v} Chris@0: return rv Chris@0: end Chris@0: end Chris@0: end Chris@0: end