Class: GerbilCharts::Models::RoundRange

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

Overview

A rounded range from a raw range Example : RawRange (102,8991) = RoundRange (100,10K)

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) ⇒ RoundRange

given a raw range, we calculate a round range



11
12
13
14
15
16
# File 'lib/gerbilcharts/models/round_range.rb', line 11

def initialize(raw_range)
  super()
  @rmin=round_min(raw_range.rmin) if not raw_range.rmin.nil?
  @rmax=round_max(raw_range.rmax) if not raw_range.rmax.nil?
  @lmax=label_step  if not raw_range.rmax.nil?
end

Instance Attribute Details

#lmaxObject (readonly)

Returns the value of attribute lmax.



8
9
10
# File 'lib/gerbilcharts/models/round_range.rb', line 8

def lmax
  @lmax
end

Instance Method Details

#each_labelObject

provide labels yields two items (value,string label)

Usage example: r.each_label do |v,s| p “Value = #v Label String = #s” end



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gerbilcharts/models/round_range.rb', line 26

def each_label

  return if not @lmax
  
 raise "Range not aligned with presets (call round range first)" if not @lmax
 raise "No data points in model" if @rmax == -1  
 return  if @lmax == 0
 
 if @rmin % @lmax != 0
   v = (@rmin+@lmax) - (@rmin%@lmax)
 else
   v = @rmin
 end
 while (v<=@rmax) do
   yield v, format_suffix(v)    
   v = v+@lmax
 end
end

#each_tick(tpl) ⇒ Object

provide ticks (per label interval)



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

def each_tick(tpl)
  
    return  "Range not aligned with presets (call round range first)" if not @lmax  

    return if @lmax==0
 return if tpl==0
    
    lint = @lmax/tpl
 return if lint==0
    if @rmin % lint != 0
      v = (@rmin+lint) - (@rmin%lint)
    else
      v = @rmin
    end
    while (v<@rmax) do
      yield v
      v = v+lint
    end
end