Class: Flightdeck::Metrics::Sparkline

Inherits:
Object
  • Object
show all
Defined in:
app/models/flightdeck/metrics/sparkline.rb

Overview

Tiny area+line for the queue cards and the Overview mini-table.

Constant Summary collapse

WIDTH =
100
HEIGHT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Sparkline

Returns a new instance of Sparkline.



12
13
14
# File 'app/models/flightdeck/metrics/sparkline.rb', line 12

def initialize(values)
  @values = Array(values).map(&:to_i)
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



10
11
12
# File 'app/models/flightdeck/metrics/sparkline.rb', line 10

def values
  @values
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


18
# File 'app/models/flightdeck/metrics/sparkline.rb', line 18

def any? = values.any? && values.max.positive?

#area_pathObject



36
37
38
39
40
# File 'app/models/flightdeck/metrics/sparkline.rb', line 36

def area_path
  return "" if coordinates.empty?

  "#{line_path} L #{coordinates.last[:x]} #{HEIGHT} L #{coordinates.first[:x]} #{HEIGHT} Z"
end

#coordinatesObject



20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/flightdeck/metrics/sparkline.rb', line 20

def coordinates
  @coordinates ||= begin
    max = [ values.max.to_i, 1 ].max
    step = values.size > 1 ? WIDTH.to_f / (values.size - 1) : WIDTH.to_f

    values.each_with_index.map do |value, index|
      { x: (index * step).round(2),
        y: (HEIGHT - 2 - ((value.to_f / max) * (HEIGHT - 4))).round(2) }
    end
  end
end

#heightObject



17
# File 'app/models/flightdeck/metrics/sparkline.rb', line 17

def height = HEIGHT

#last_pointObject



42
# File 'app/models/flightdeck/metrics/sparkline.rb', line 42

def last_point = coordinates.last

#line_pathObject



32
33
34
# File 'app/models/flightdeck/metrics/sparkline.rb', line 32

def line_path
  coordinates.each_with_index.map { |c, i| "#{i.zero? ? "M" : "L"} #{c[:x]} #{c[:y]}" }.join(" ")
end

#widthObject



16
# File 'app/models/flightdeck/metrics/sparkline.rb', line 16

def width = WIDTH