annotate .svn/pristine/8a/8a5fb0079b7bc3191dfd37b1677f3f459fe6d66e.svn-base @ 1628:9c5f8e24dadc live tip

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