Class: Squid::Point

Inherits:
Object
  • Object
show all
Extended by:
Format
Defined in:
lib/squid/point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Format

format_for, number_to_float, number_to_minutes_and_seconds

Constructor Details

#initialize(y:, height:, index:, label:, negative:) ⇒ Point

Returns a new instance of Point.



25
26
27
# File 'lib/squid/point.rb', line 25

def initialize(y:, height:, index:, label:, negative:)
  @y, @height, @index, @label, @negative = y, height, index, label, negative
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



23
24
25
# File 'lib/squid/point.rb', line 23

def height
  @height
end

#indexObject (readonly)

Returns the value of attribute index.



23
24
25
# File 'lib/squid/point.rb', line 23

def index
  @index
end

#labelObject (readonly)

Returns the value of attribute label.



23
24
25
# File 'lib/squid/point.rb', line 23

def label
  @label
end

#negativeObject (readonly)

Returns the value of attribute negative.



23
24
25
# File 'lib/squid/point.rb', line 23

def negative
  @negative
end

#yObject (readonly)

Returns the value of attribute y.



23
24
25
# File 'lib/squid/point.rb', line 23

def y
  @y
end

Class Method Details

.for(series, minmax:, height:, labels:, stack:, formats:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/squid/point.rb', line 7

def self.for(series, minmax:, height:, labels:, stack:, formats:)
  @min = Hash.new 0
  @max = Hash.new 0
  min, max = minmax
  offset = -> (value) { value * height.to_f / (max-min) }
  series.map.with_index do |values, series_i|
    values.map.with_index do |value, i|
      h = y_for value, index: i, stack: false, &offset if value
      y = y_for value, index: i, stack: stack, &offset if value
      y = y - offset.call([min, 0].min) if value
      label = format_for value, formats[series_i] if labels[series_i]
      new y: y, height: h, index: i, label: label, negative: value.to_f < 0
    end
  end
end