Class: Rubyvis::Scale::Log

Inherits:
Quantitative show all
Defined in:
lib/rubyvis/scale/log.rb

Instance Attribute Summary

Attributes inherited from Quantitative

#l

Instance Method Summary collapse

Methods inherited from Quantitative

#[], #domain, #invert, #new_date, #range, #scale, #tick_format, #to_date, #to_proc, #type

Methods included from Rubyvis::Scale

interpolator, linear, log, ordinal, quantitative

Constructor Details

#initialize(*args) ⇒ Log

Returns a new instance of Log.



4
5
6
7
8
9
10
# File 'lib/rubyvis/scale/log.rb', line 4

def initialize(*args)
  super(*args)
  @b=nil
  @_p=nil
  
  base(10)
end

Instance Method Details

#base(v = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rubyvis/scale/log.rb', line 17

def base(v=nil)
  if v
    @b=v
    @_p=Math.log(@b)
    transform(lambda {|x| log(x)}, lambda {|x| pow(x)})
    return self
  end
  return @b
end

#log(x) ⇒ Object



11
12
13
# File 'lib/rubyvis/scale/log.rb', line 11

def log(x)
  Math.log(x) / @_p.to_f
end

#niceObject



26
27
28
29
# File 'lib/rubyvis/scale/log.rb', line 26

def nice
  d=domain
  domain(Rubyvis.log_floor(d[0],@b), Rubyvis.log_ceil(d[1],@b))
end

#pow(y) ⇒ Object



14
15
16
# File 'lib/rubyvis/scale/log.rb', line 14

def pow(y)
  @b**y
end

#ticksObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubyvis/scale/log.rb', line 30

def ticks
  d = domain
  n = d[0] < 0
  
 # puts "dom: #{d[0]} -> #{n}"
  
  i = (n ? -log(-d[0]) : log(d[0])).floor
  j = (n ? -log(-d[1]) : log(d[1])).ceil
  ticks = [];
  if n
    ticks.push(-pow(-i))
    (i..j).each {|ii|
      ((@b-1)...0).each {|k|
        ticks.push(-pow(-ii) * k)
      }
    }
  else
    (i...j).each {|ii|
      (1...@b).each {|k|
        ticks.push(pow(ii) * k)
      }
    }
    ticks.push(pow(j));
  end
  
  #for (i = 0; ticks[i] < d[0]; i++); // strip small values
  #for (j = ticks.length; ticks[j - 1] > d[1]; j--); // strip big values
  #return ticks.slice(i, j);
  ticks.find_all {|v| v>=d[0] and v<=d[1]}
end