Module: UnicodePlot::Utils

Defined in:
lib/unicode_plot/utils.rb

Class Method Summary collapse

Class Method Details

.ceil_neg_log10(x) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/unicode_plot/utils.rb', line 49

def ceil_neg_log10(x)
  if roundable?(-Math.log10(x))
    (-Math.log10(x)).ceil
  else
    (-Math.log10(x)).floor
  end
end

.extend_limits(values, limits) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/unicode_plot/utils.rb', line 5

def extend_limits(values, limits)
  mi, ma = limits.minmax.map(&:to_f)
  if mi == 0 && ma == 0
    mi, ma = values.minmax.map(&:to_f)
  end
  diff = ma - mi
  if diff == 0
    ma = mi + 1
    mi = mi - 1
  end
  if limits == [0, 0]
    plotting_range_narrow(mi, ma)
  else
    [mi, ma]
  end
end

.plotting_range_narrow(xmin, xmax) ⇒ Object



22
23
24
25
26
27
# File 'lib/unicode_plot/utils.rb', line 22

def plotting_range_narrow(xmin, xmax)
  diff = xmax - xmin
  xmax = round_up_subtick(xmax, diff)
  xmin = round_down_subtick(xmin, diff)
  [xmin.to_f, xmax.to_f]
end

.round_down_subtick(x, m) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/unicode_plot/utils.rb', line 39

def round_down_subtick(x, m)
  if x == 0
    0.0
  elsif x > 0
    x.floor(ceil_neg_log10(m) + 1)
  else
    -(-x).ceil(ceil_neg_log10(m) + 1)
  end
end

.round_up_subtick(x, m) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/unicode_plot/utils.rb', line 29

def round_up_subtick(x, m)
  if x == 0
    0.0
  elsif x > 0
    x.ceil(ceil_neg_log10(m) + 1)
  else
    -(-x).floor(ceil_neg_log10(m) + 1)
  end
end

.roundable?(x) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/unicode_plot/utils.rb', line 57

def roundable?(x)
  x.to_i == x
end