annotate .svn/pristine/74/74c1617ddcbb99d4a4a61efc78116f68f0818d6b.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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