Chris@1294: require 'SVG/Graph/Plot' Chris@1294: Chris@1294: module SVG Chris@1294: module Graph Chris@1294: # === For creating SVG plots of scalar temporal data Chris@1294: # Chris@1294: # = Synopsis Chris@1294: # Chris@1294: # require 'SVG/Graph/TimeSeriess' Chris@1294: # Chris@1294: # # Data sets are x,y pairs Chris@1294: # data1 = ["6/17/72", 11, "1/11/72", 7, "4/13/04 17:31", 11, Chris@1294: # "9/11/01", 9, "9/1/85", 2, "9/1/88", 1, "1/15/95", 13] Chris@1294: # data2 = ["8/1/73", 18, "3/1/77", 15, "10/1/98", 4, Chris@1294: # "5/1/02", 14, "3/1/95", 6, "8/1/91", 12, "12/1/87", 6, Chris@1294: # "5/1/84", 17, "10/1/80", 12] Chris@1294: # Chris@1294: # graph = SVG::Graph::TimeSeries.new( { Chris@1294: # :width => 640, Chris@1294: # :height => 480, Chris@1294: # :graph_title => title, Chris@1294: # :show_graph_title => true, Chris@1294: # :no_css => true, Chris@1294: # :key => true, Chris@1294: # :scale_x_integers => true, Chris@1294: # :scale_y_integers => true, Chris@1294: # :min_x_value => 0, Chris@1294: # :min_y_value => 0, Chris@1294: # :show_data_labels => true, Chris@1294: # :show_x_guidelines => true, Chris@1294: # :show_x_title => true, Chris@1294: # :x_title => "Time", Chris@1294: # :show_y_title => true, Chris@1294: # :y_title => "Ice Cream Cones", Chris@1294: # :y_title_text_direction => :bt, Chris@1294: # :stagger_x_labels => true, Chris@1294: # :x_label_format => "%m/%d/%y", Chris@1294: # }) Chris@1294: # Chris@1294: # graph.add_data({ Chris@1294: # :data => projection Chris@1294: # :title => 'Projected', Chris@1294: # }) Chris@1294: # Chris@1294: # graph.add_data({ Chris@1294: # :data => actual, Chris@1294: # :title => 'Actual', Chris@1294: # }) Chris@1294: # Chris@1294: # print graph.burn() Chris@1294: # Chris@1294: # = Description Chris@1294: # Chris@1294: # Produces a graph of temporal scalar data. Chris@1294: # Chris@1294: # = Examples Chris@1294: # Chris@1294: # http://www.germane-software/repositories/public/SVG/test/timeseries.rb Chris@1294: # Chris@1294: # = Notes Chris@1294: # Chris@1294: # The default stylesheet handles upto 10 data sets, if you Chris@1294: # use more you must create your own stylesheet and add the Chris@1294: # additional settings for the extra data sets. You will know Chris@1294: # if you go over 10 data sets as they will have no style and Chris@1294: # be in black. Chris@1294: # Chris@1294: # Unlike the other types of charts, data sets must contain x,y pairs: Chris@1294: # Chris@1294: # [ "12:30", 2 ] # A data set with 1 point: ("12:30",2) Chris@1294: # [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and Chris@1294: # # ("14:20",6) Chris@1294: # Chris@1294: # Note that multiple data sets within the same chart can differ in length, Chris@1294: # and that the data in the datasets needn't be in order; they will be ordered Chris@1294: # by the plot along the X-axis. Chris@1294: # Chris@1294: # The dates must be parseable by ParseDate, but otherwise can be Chris@1294: # any order of magnitude (seconds within the hour, or years) Chris@1294: # Chris@1294: # = See also Chris@1294: # Chris@1294: # * SVG::Graph::Graph Chris@1294: # * SVG::Graph::BarHorizontal Chris@1294: # * SVG::Graph::Bar Chris@1294: # * SVG::Graph::Line Chris@1294: # * SVG::Graph::Pie Chris@1294: # * SVG::Graph::Plot Chris@1294: # Chris@1294: # == Author Chris@1294: # Chris@1294: # Sean E. Russell Chris@1294: # Chris@1294: # Copyright 2004 Sean E. Russell Chris@1294: # This software is available under the Ruby license[LICENSE.txt] Chris@1294: # Chris@1294: class TimeSeries < Plot Chris@1294: # In addition to the defaults set by Graph::initialize and Chris@1294: # Plot::set_defaults, sets: Chris@1294: # [x_label_format] '%Y-%m-%d %H:%M:%S' Chris@1294: # [popup_format] '%Y-%m-%d %H:%M:%S' Chris@1294: def set_defaults Chris@1294: super Chris@1294: init_with( Chris@1294: #:max_time_span => '', Chris@1294: :x_label_format => '%Y-%m-%d %H:%M:%S', Chris@1294: :popup_format => '%Y-%m-%d %H:%M:%S' Chris@1294: ) Chris@1294: end Chris@1294: Chris@1294: # The format string use do format the X axis labels. Chris@1294: # See Time::strformat Chris@1294: attr_accessor :x_label_format Chris@1294: # Use this to set the spacing between dates on the axis. The value Chris@1294: # must be of the form Chris@1294: # "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?" Chris@1294: # Chris@1294: # EG: Chris@1294: # Chris@1294: # graph.timescale_divisions = "2 weeks" Chris@1294: # Chris@1294: # will cause the chart to try to divide the X axis up into segments of Chris@1294: # two week periods. Chris@1294: attr_accessor :timescale_divisions Chris@1294: # The formatting used for the popups. See x_label_format Chris@1294: attr_accessor :popup_format Chris@1294: Chris@1294: # Add data to the plot. Chris@1294: # Chris@1294: # d1 = [ "12:30", 2 ] # A data set with 1 point: ("12:30",2) Chris@1294: # d2 = [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and Chris@1294: # # ("14:20",6) Chris@1294: # graph.add_data( Chris@1294: # :data => d1, Chris@1294: # :title => 'One' Chris@1294: # ) Chris@1294: # graph.add_data( Chris@1294: # :data => d2, Chris@1294: # :title => 'Two' Chris@1294: # ) Chris@1294: # Chris@1294: # Note that the data must be in time,value pairs, and that the date format Chris@1294: # may be any date that is parseable by ParseDate. Chris@1294: def add_data data Chris@1294: @data = [] unless @data Chris@1294: Chris@1294: raise "No data provided by #{@data.inspect}" unless data[:data] and Chris@1294: data[:data].kind_of? Array Chris@1294: raise "Data supplied must be x,y pairs! "+ Chris@1294: "The data provided contained an odd set of "+ Chris@1294: "data points" unless data[:data].length % 2 == 0 Chris@1294: return if data[:data].length == 0 Chris@1294: Chris@1294: Chris@1294: x = [] Chris@1294: y = [] Chris@1294: data[:data].each_index {|i| Chris@1294: if i%2 == 0 Chris@1294: t = DateTime.parse( data[:data][i] ).to_time Chris@1294: x << t.to_i Chris@1294: else Chris@1294: y << data[:data][i] Chris@1294: end Chris@1294: } Chris@1294: sort( x, y ) Chris@1294: data[:data] = [x,y] Chris@1294: @data << data Chris@1294: end Chris@1294: Chris@1294: Chris@1294: protected Chris@1294: Chris@1294: def min_x_value=(value) Chris@1294: @min_x_value = DateTime.parse( value ).to_time Chris@1294: end Chris@1294: Chris@1294: Chris@1294: def format x, y Chris@1294: Time.at( x ).strftime( popup_format ) Chris@1294: end Chris@1294: Chris@1294: def get_x_labels Chris@1294: get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) } Chris@1294: end Chris@1294: Chris@1294: private Chris@1294: def get_x_values Chris@1294: rv = [] Chris@1294: min, max, scale_division = x_range Chris@1294: if timescale_divisions Chris@1294: timescale_divisions =~ /(\d+) ?(day|week|month|year|hour|minute|second)?/ Chris@1294: division_units = $2 ? $2 : "day" Chris@1294: amount = $1.to_i Chris@1294: if amount Chris@1294: step = nil Chris@1294: case division_units Chris@1294: when "month" Chris@1294: cur = min Chris@1294: while cur < max Chris@1294: rv << cur Chris@1294: arr = Time.at( cur ).to_a Chris@1294: arr[4] += amount Chris@1294: if arr[4] > 12 Chris@1294: arr[5] += (arr[4] / 12).to_i Chris@1294: arr[4] = (arr[4] % 12) Chris@1294: end Chris@1294: cur = Time.local(*arr).to_i Chris@1294: end Chris@1294: when "year" Chris@1294: cur = min Chris@1294: while cur < max Chris@1294: rv << cur Chris@1294: arr = Time.at( cur ).to_a Chris@1294: arr[5] += amount Chris@1294: cur = Time.local(*arr).to_i Chris@1294: end Chris@1294: when "week" Chris@1294: step = 7 * 24 * 60 * 60 * amount Chris@1294: when "day" Chris@1294: step = 24 * 60 * 60 * amount Chris@1294: when "hour" Chris@1294: step = 60 * 60 * amount Chris@1294: when "minute" Chris@1294: step = 60 * amount Chris@1294: when "second" Chris@1294: step = amount Chris@1294: end Chris@1294: min.step( max, step ) {|v| rv << v} if step Chris@1294: Chris@1294: return rv Chris@1294: end Chris@1294: end Chris@1294: min.step( max, scale_division ) {|v| rv << v} Chris@1294: return rv Chris@1294: end Chris@1294: end Chris@1294: end Chris@1294: end