Class: Flightdeck::Metrics::Sparkline
- Inherits:
-
Object
- Object
- Flightdeck::Metrics::Sparkline
- 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
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #any? ⇒ Boolean
- #area_path ⇒ Object
- #coordinates ⇒ Object
- #height ⇒ Object
-
#initialize(values) ⇒ Sparkline
constructor
A new instance of Sparkline.
- #last_point ⇒ Object
- #line_path ⇒ Object
- #width ⇒ Object
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
#values ⇒ Object (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
18 |
# File 'app/models/flightdeck/metrics/sparkline.rb', line 18 def any? = values.any? && values.max.positive? |
#area_path ⇒ Object
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 |
#coordinates ⇒ Object
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 |
#height ⇒ Object
17 |
# File 'app/models/flightdeck/metrics/sparkline.rb', line 17 def height = HEIGHT |
#last_point ⇒ Object
42 |
# File 'app/models/flightdeck/metrics/sparkline.rb', line 42 def last_point = coordinates.last |
#line_path ⇒ Object
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 |
#width ⇒ Object
16 |
# File 'app/models/flightdeck/metrics/sparkline.rb', line 16 def width = WIDTH |