Class: Flightdeck::Metrics::Chart
- Inherits:
-
Object
- Object
- Flightdeck::Metrics::Chart
- Defined in:
- app/models/flightdeck/metrics/chart.rb
Overview
Geometry for the server-rendered SVG charts.
All the arithmetic lives here so the ERB partials stay readable: they place elements and nothing else. Colours are never computed here either — the partials reference the CSS custom properties, so charts re-theme with the rest of the page and there is no inline colour to get out of step.
Constant Summary collapse
- WIDTH =
720- HEIGHT =
190- PAD_LEFT =
40- PAD_RIGHT =
10- PAD_TOP =
12- PAD_BOTTOM =
22- GRID_INTERVALS =
3
Instance Attribute Summary collapse
-
#points ⇒ Object
readonly
Returns the value of attribute points.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
- #axis_max ⇒ Object
- #empty? ⇒ Boolean
- #format_time(time, pattern) ⇒ Object
- #full_time(time) ⇒ Object
- #gridlines ⇒ Object
- #height ⇒ Object
-
#initialize(points:, window:) ⇒ Chart
constructor
A new instance of Chart.
- #plot_bottom ⇒ Object
- #plot_height ⇒ Object
- #plot_left ⇒ Object
- #plot_right ⇒ Object
- #plot_top ⇒ Object
- #plot_width ⇒ Object
- #width ⇒ Object
-
#x_ticks ⇒ Object
Ticks are placed on bucket boundaries so a label always names a bucket that is actually on screen.
Constructor Details
#initialize(points:, window:) ⇒ Chart
Returns a new instance of Chart.
22 23 24 25 |
# File 'app/models/flightdeck/metrics/chart.rb', line 22 def initialize(points:, window:) @points = points @window = window end |
Instance Attribute Details
#points ⇒ Object (readonly)
Returns the value of attribute points.
20 21 22 |
# File 'app/models/flightdeck/metrics/chart.rb', line 20 def points @points end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
20 21 22 |
# File 'app/models/flightdeck/metrics/chart.rb', line 20 def window @window end |
Instance Method Details
#axis_max ⇒ Object
56 57 58 |
# File 'app/models/flightdeck/metrics/chart.rb', line 56 def axis_max @axis_max ||= nice_ceiling(raw_max) end |
#empty? ⇒ Boolean
35 |
# File 'app/models/flightdeck/metrics/chart.rb', line 35 def empty? = points.empty? |
#format_time(time, pattern) ⇒ Object
60 61 62 |
# File 'app/models/flightdeck/metrics/chart.rb', line 60 def format_time(time, pattern) time.in_time_zone(Flightdeck.config.display_timezone).strftime(pattern) end |
#full_time(time) ⇒ Object
64 65 66 |
# File 'app/models/flightdeck/metrics/chart.rb', line 64 def full_time(time) format_time(time, "%Y-%m-%d %H:%M") end |
#gridlines ⇒ Object
37 38 39 40 41 42 |
# File 'app/models/flightdeck/metrics/chart.rb', line 37 def gridlines (0..GRID_INTERVALS).map do |i| value = axis_max * (GRID_INTERVALS - i) / GRID_INTERVALS.to_f { y: round(plot_top + (plot_height * i / GRID_INTERVALS.to_f)), value: value, label: format_value(value) } end end |
#height ⇒ Object
28 |
# File 'app/models/flightdeck/metrics/chart.rb', line 28 def height = HEIGHT |
#plot_bottom ⇒ Object
32 |
# File 'app/models/flightdeck/metrics/chart.rb', line 32 def plot_bottom = HEIGHT - PAD_BOTTOM |
#plot_height ⇒ Object
34 |
# File 'app/models/flightdeck/metrics/chart.rb', line 34 def plot_height = plot_bottom - plot_top |
#plot_left ⇒ Object
29 |
# File 'app/models/flightdeck/metrics/chart.rb', line 29 def plot_left = PAD_LEFT |
#plot_right ⇒ Object
30 |
# File 'app/models/flightdeck/metrics/chart.rb', line 30 def plot_right = WIDTH - PAD_RIGHT |
#plot_top ⇒ Object
31 |
# File 'app/models/flightdeck/metrics/chart.rb', line 31 def plot_top = PAD_TOP |
#plot_width ⇒ Object
33 |
# File 'app/models/flightdeck/metrics/chart.rb', line 33 def plot_width = plot_right - plot_left |
#width ⇒ Object
27 |
# File 'app/models/flightdeck/metrics/chart.rb', line 27 def width = WIDTH |
#x_ticks ⇒ Object
Ticks are placed on bucket boundaries so a label always names a bucket that is actually on screen.
46 47 48 49 50 51 52 53 54 |
# File 'app/models/flightdeck/metrics/chart.rb', line 46 def x_ticks every = [ window.tick_every.to_i, 1 ].max points.each_with_index.filter_map do |point, index| next unless (index % every).zero? { x: round(tick_x(index)), label: format_time(point.at, window.tick_format) } end end |