Class: GerbilCharts::Models::RoundTimeRange

Inherits:
RawRange show all
Defined in:
lib/gerbilcharts/models/round_time_range.rb

Overview

rounded time range - Rounds the max to a nice preset

This class allows for labels at clean intervals instead of
"Jan 17 2008 11:27:92"

Constant Summary

Constants inherited from Presets

Presets::PRESETS, Presets::PRESET_LABEL_POS, Presets::PRESET_VAL_POS, Presets::TIMEPRESETS, Presets::UNITPRESETS

Instance Attribute Summary collapse

Attributes inherited from RawRange

#rmax, #rmin

Instance Method Summary collapse

Methods inherited from RawRange

#delta, #format_value, #invalid?, #min_max, #reset, #scale_factor, #to_s, #update, #update_r, #zeromin

Methods inherited from Presets

#format_suffix, #format_timeval

Constructor Details

#initialize(raw_range) ⇒ RoundTimeRange

given a raw range, we calculate a round range



18
19
20
21
22
23
24
25
26
# File 'lib/gerbilcharts/models/round_time_range.rb', line 18

def initialize(raw_range)
  super()
  @rdelta,@ldelta=round_delta(raw_range.delta)
  @rmin=raw_range.rmin

  # this can be rdelta or ldelta for wider or tighter
  #@rmax=round_max(raw_range.rmax,@rdelta)
  @rmax=round_max(raw_range.rmax,@ldelta)
end

Instance Attribute Details

#ldeltaObject (readonly)

label delta seconds (eg label every = 2 hrs)



14
15
16
# File 'lib/gerbilcharts/models/round_time_range.rb', line 14

def ldelta
  @ldelta
end

#rdeltaObject (readonly)

range delta seconds (eg chart span = 6 hrs)



15
16
17
# File 'lib/gerbilcharts/models/round_time_range.rb', line 15

def rdelta
  @rdelta
end

Instance Method Details

#each_labelObject

provide labels Yields two items (value - seconds since Jan 1 1970, string label)



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gerbilcharts/models/round_time_range.rb', line 30

def each_label
  
 raise "Range not aligned with presets (call round range first)" if not @ldelta
 
 return if @ldelta == 0

  v = get_label_start_time

 while (v<=@rmax) do
   yield v, format_timeval(v,@rdelta)    
   v = v+@ldelta
 end
end

#each_tick(tpl) ⇒ Object

provide ticks (per label interval) note the tpl is not used !



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gerbilcharts/models/round_time_range.rb', line 46

def each_tick(tpl)
  raise "Range not aligned with presets (call round range first)" if not @ldelta
  
  return if @ldelta == 0

  subtick_delta = ideal_subtick_interval(@ldelta)
    
  v = get_label_start_time

  while (v<=@rmax) do
    yield v    
    v = v+subtick_delta
  end

end

#format_min_valueObject

format min value completely



63
64
65
66
# File 'lib/gerbilcharts/models/round_time_range.rb', line 63

def format_min_value
    t = Time.at(@rmin).getlocal
    return t.to_s
end