class Squid::Axis

@private

Attributes

data[R]

Public Class Methods

new(data, steps:, stack:, format:, &block) click to toggle source
# 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

Public Instance Methods

labels() click to toggle source
# File lib/squid/axis.rb, line 21
def labels
  min, max = minmax
  values = if min.nil? || max.nil? || @steps.zero?
    []
  else
    max.step(by: (min - max)/@steps.to_f, to: min)
  end
  @labels ||= values.map{|value| format_for value, @format}
end
minmax() click to toggle source
# File lib/squid/axis.rb, line 15
def minmax
  @minmax ||= [min, max].compact.map do |number|
    approximate number
  end
end
width() click to toggle source
# File lib/squid/axis.rb, line 31
def width
  @width ||= labels.map{|label| label_width label}.max || 0
end

Private Instance Methods

approximate(number) click to toggle source
# File lib/squid/axis.rb, line 69
def approximate(number)
  number_to_rounded(number, significant: true, precision: 2).to_f
end
closest_step_to(value) click to toggle source
# File lib/squid/axis.rb, line 53
def closest_step_to(value)
  if @format == :integer
    ((value - min) / @steps + 1) * @steps + min
  else
    [value, @steps].max
  end
end
label_width(label) click to toggle source
# File lib/squid/axis.rb, line 37
def label_width(label)
  @width_proc.call label if @width_proc
end
max() click to toggle source
# File lib/squid/axis.rb, line 47
def max
  if @data.any? && values.last && values.last.any?
    closest_step_to values.last.max
  end
end
min() click to toggle source
# File lib/squid/axis.rb, line 41
def min
  if @data.any? && values.first && values.first.any?
    [values.first.min, 0].min
  end
end
values() click to toggle source
# File lib/squid/axis.rb, line 61
def values
  @values ||= if @stack
    @data.transpose.map{|a| a.compact.partition{|n| n < 0}.map(&:sum)}.transpose
  else
    [@data.flatten.compact]
  end
end