annotate lib/SVG/Graph/TimeSeries.rb @ 1267:b587499d50a9 redmine-2.2-integration

{{}} -> %{}
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 02 May 2013 12:48:34 +0100
parents 433d4f72a19b
children 3e4c3460b6ca
rev   line source
Chris@0 1 require 'SVG/Graph/Plot'
Chris@0 2
Chris@0 3 module SVG
Chris@0 4 module Graph
Chris@0 5 # === For creating SVG plots of scalar temporal data
Chris@0 6 #
Chris@0 7 # = Synopsis
Chris@0 8 #
Chris@0 9 # require 'SVG/Graph/TimeSeriess'
Chris@0 10 #
Chris@0 11 # # Data sets are x,y pairs
Chris@0 12 # data1 = ["6/17/72", 11, "1/11/72", 7, "4/13/04 17:31", 11,
Chris@0 13 # "9/11/01", 9, "9/1/85", 2, "9/1/88", 1, "1/15/95", 13]
Chris@0 14 # data2 = ["8/1/73", 18, "3/1/77", 15, "10/1/98", 4,
Chris@0 15 # "5/1/02", 14, "3/1/95", 6, "8/1/91", 12, "12/1/87", 6,
Chris@0 16 # "5/1/84", 17, "10/1/80", 12]
Chris@0 17 #
Chris@0 18 # graph = SVG::Graph::TimeSeries.new( {
Chris@0 19 # :width => 640,
Chris@0 20 # :height => 480,
Chris@0 21 # :graph_title => title,
Chris@0 22 # :show_graph_title => true,
Chris@0 23 # :no_css => true,
Chris@0 24 # :key => true,
Chris@0 25 # :scale_x_integers => true,
Chris@0 26 # :scale_y_integers => true,
Chris@0 27 # :min_x_value => 0,
Chris@0 28 # :min_y_value => 0,
Chris@0 29 # :show_data_labels => true,
Chris@0 30 # :show_x_guidelines => true,
Chris@0 31 # :show_x_title => true,
Chris@0 32 # :x_title => "Time",
Chris@0 33 # :show_y_title => true,
Chris@0 34 # :y_title => "Ice Cream Cones",
Chris@0 35 # :y_title_text_direction => :bt,
Chris@0 36 # :stagger_x_labels => true,
Chris@0 37 # :x_label_format => "%m/%d/%y",
Chris@0 38 # })
Chris@0 39 #
Chris@0 40 # graph.add_data({
Chris@0 41 # :data => projection
Chris@0 42 # :title => 'Projected',
Chris@0 43 # })
Chris@0 44 #
Chris@0 45 # graph.add_data({
Chris@0 46 # :data => actual,
Chris@0 47 # :title => 'Actual',
Chris@0 48 # })
Chris@0 49 #
Chris@0 50 # print graph.burn()
Chris@0 51 #
Chris@0 52 # = Description
Chris@0 53 #
Chris@0 54 # Produces a graph of temporal scalar data.
Chris@0 55 #
Chris@0 56 # = Examples
Chris@0 57 #
Chris@0 58 # http://www.germane-software/repositories/public/SVG/test/timeseries.rb
Chris@0 59 #
Chris@0 60 # = Notes
Chris@0 61 #
Chris@0 62 # The default stylesheet handles upto 10 data sets, if you
Chris@0 63 # use more you must create your own stylesheet and add the
Chris@0 64 # additional settings for the extra data sets. You will know
Chris@0 65 # if you go over 10 data sets as they will have no style and
Chris@0 66 # be in black.
Chris@0 67 #
Chris@0 68 # Unlike the other types of charts, data sets must contain x,y pairs:
Chris@0 69 #
Chris@0 70 # [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
Chris@0 71 # [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
Chris@0 72 # # ("14:20",6)
Chris@0 73 #
Chris@0 74 # Note that multiple data sets within the same chart can differ in length,
Chris@0 75 # and that the data in the datasets needn't be in order; they will be ordered
Chris@0 76 # by the plot along the X-axis.
Chris@0 77 #
Chris@0 78 # The dates must be parseable by ParseDate, but otherwise can be
Chris@0 79 # any order of magnitude (seconds within the hour, or years)
Chris@0 80 #
Chris@0 81 # = See also
Chris@0 82 #
Chris@0 83 # * SVG::Graph::Graph
Chris@0 84 # * SVG::Graph::BarHorizontal
Chris@0 85 # * SVG::Graph::Bar
Chris@0 86 # * SVG::Graph::Line
Chris@0 87 # * SVG::Graph::Pie
Chris@0 88 # * SVG::Graph::Plot
Chris@0 89 #
Chris@0 90 # == Author
Chris@0 91 #
Chris@0 92 # Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
Chris@0 93 #
Chris@0 94 # Copyright 2004 Sean E. Russell
Chris@0 95 # This software is available under the Ruby license[LICENSE.txt]
Chris@0 96 #
Chris@0 97 class TimeSeries < Plot
Chris@0 98 # In addition to the defaults set by Graph::initialize and
Chris@0 99 # Plot::set_defaults, sets:
Chris@0 100 # [x_label_format] '%Y-%m-%d %H:%M:%S'
Chris@0 101 # [popup_format] '%Y-%m-%d %H:%M:%S'
Chris@0 102 def set_defaults
Chris@0 103 super
Chris@0 104 init_with(
Chris@0 105 #:max_time_span => '',
Chris@0 106 :x_label_format => '%Y-%m-%d %H:%M:%S',
Chris@0 107 :popup_format => '%Y-%m-%d %H:%M:%S'
Chris@0 108 )
Chris@0 109 end
Chris@0 110
Chris@0 111 # The format string use do format the X axis labels.
Chris@0 112 # See Time::strformat
Chris@0 113 attr_accessor :x_label_format
Chris@0 114 # Use this to set the spacing between dates on the axis. The value
Chris@0 115 # must be of the form
Chris@0 116 # "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?"
Chris@0 117 #
Chris@0 118 # EG:
Chris@0 119 #
Chris@0 120 # graph.timescale_divisions = "2 weeks"
Chris@0 121 #
Chris@0 122 # will cause the chart to try to divide the X axis up into segments of
Chris@0 123 # two week periods.
Chris@0 124 attr_accessor :timescale_divisions
Chris@0 125 # The formatting used for the popups. See x_label_format
Chris@0 126 attr_accessor :popup_format
Chris@0 127
Chris@0 128 # Add data to the plot.
Chris@0 129 #
Chris@0 130 # d1 = [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
Chris@0 131 # d2 = [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
Chris@0 132 # # ("14:20",6)
Chris@0 133 # graph.add_data(
Chris@0 134 # :data => d1,
Chris@0 135 # :title => 'One'
Chris@0 136 # )
Chris@0 137 # graph.add_data(
Chris@0 138 # :data => d2,
Chris@0 139 # :title => 'Two'
Chris@0 140 # )
Chris@0 141 #
Chris@0 142 # Note that the data must be in time,value pairs, and that the date format
Chris@0 143 # may be any date that is parseable by ParseDate.
Chris@0 144 def add_data data
Chris@0 145 @data = [] unless @data
Chris@0 146
Chris@0 147 raise "No data provided by #{@data.inspect}" unless data[:data] and
Chris@0 148 data[:data].kind_of? Array
Chris@0 149 raise "Data supplied must be x,y pairs! "+
Chris@0 150 "The data provided contained an odd set of "+
Chris@0 151 "data points" unless data[:data].length % 2 == 0
Chris@0 152 return if data[:data].length == 0
Chris@0 153
Chris@0 154
Chris@0 155 x = []
Chris@0 156 y = []
Chris@0 157 data[:data].each_index {|i|
Chris@0 158 if i%2 == 0
Chris@1115 159 t = DateTime.parse( data[:data][i] ).to_time
Chris@0 160 x << t.to_i
Chris@0 161 else
Chris@0 162 y << data[:data][i]
Chris@0 163 end
Chris@0 164 }
Chris@0 165 sort( x, y )
Chris@0 166 data[:data] = [x,y]
Chris@0 167 @data << data
Chris@0 168 end
Chris@0 169
Chris@0 170
Chris@0 171 protected
Chris@0 172
Chris@0 173 def min_x_value=(value)
Chris@1115 174 @min_x_value = DateTime.parse( value ).to_time
Chris@0 175 end
Chris@0 176
Chris@0 177
Chris@0 178 def format x, y
Chris@0 179 Time.at( x ).strftime( popup_format )
Chris@0 180 end
Chris@0 181
Chris@0 182 def get_x_labels
Chris@0 183 get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
Chris@0 184 end
Chris@0 185
Chris@0 186 private
Chris@0 187 def get_x_values
Chris@0 188 rv = []
Chris@0 189 min, max, scale_division = x_range
Chris@0 190 if timescale_divisions
Chris@0 191 timescale_divisions =~ /(\d+) ?(day|week|month|year|hour|minute|second)?/
Chris@0 192 division_units = $2 ? $2 : "day"
Chris@0 193 amount = $1.to_i
Chris@0 194 if amount
Chris@0 195 step = nil
Chris@0 196 case division_units
Chris@0 197 when "month"
Chris@0 198 cur = min
Chris@0 199 while cur < max
Chris@0 200 rv << cur
Chris@0 201 arr = Time.at( cur ).to_a
Chris@0 202 arr[4] += amount
Chris@0 203 if arr[4] > 12
Chris@0 204 arr[5] += (arr[4] / 12).to_i
Chris@0 205 arr[4] = (arr[4] % 12)
Chris@0 206 end
Chris@0 207 cur = Time.local(*arr).to_i
Chris@0 208 end
Chris@0 209 when "year"
Chris@0 210 cur = min
Chris@0 211 while cur < max
Chris@0 212 rv << cur
Chris@0 213 arr = Time.at( cur ).to_a
Chris@0 214 arr[5] += amount
Chris@0 215 cur = Time.local(*arr).to_i
Chris@0 216 end
Chris@0 217 when "week"
Chris@0 218 step = 7 * 24 * 60 * 60 * amount
Chris@0 219 when "day"
Chris@0 220 step = 24 * 60 * 60 * amount
Chris@0 221 when "hour"
Chris@0 222 step = 60 * 60 * amount
Chris@0 223 when "minute"
Chris@0 224 step = 60 * amount
Chris@0 225 when "second"
Chris@0 226 step = amount
Chris@0 227 end
Chris@0 228 min.step( max, step ) {|v| rv << v} if step
Chris@0 229
Chris@0 230 return rv
Chris@0 231 end
Chris@0 232 end
Chris@0 233 min.step( max, scale_division ) {|v| rv << v}
Chris@0 234 return rv
Chris@0 235 end
Chris@0 236 end
Chris@0 237 end
Chris@0 238 end