annotate .svn/pristine/99/999fdff2052b71cf3f29b97c581e2f895bcdf162.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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