Class: Squid::Axis

Inherits:
Object
  • Object
show all
Includes:
Format
Defined in:
lib/squid/axis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Format

#format_for, #number_to_minutes_and_seconds

Constructor Details

#initialize(data, steps:, stack:, format:, &block) ⇒ Axis

Returns a new instance of Axis


10
11
12
13
# File 'lib/squid/axis.rb', line 10

def initialize(data, steps:, stack:, format:, &block)
  @data, @steps, @stack, @format = data, steps, stack, format
  @width_proc = block if block_given?
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


8
9
10
# File 'lib/squid/axis.rb', line 8

def data
  @data
end

Instance Method Details

#labelsObject


21
22
23
24
25
26
27
28
29
# File 'lib/squid/axis.rb', line 21

def labels
  min, max = minmax
  values = if @data.empty? || @steps.zero?
    []
  else
    max.step(by: (min - max)/@steps.to_f, to: min)
  end
  @labels ||= values.map{|value| format_for value, @format}
end

#minmaxObject


15
16
17
18
19
# File 'lib/squid/axis.rb', line 15

def minmax
  @minmax ||= [min, max].compact.map do |number|
    approximate number
  end
end

#widthObject


31
32
33
# File 'lib/squid/axis.rb', line 31

def width
  @width ||= labels.map{|label| label_width label}.max || 0
end