Class: Flightdeck::Metrics::Chart

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

BarChart, LineChart

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

Instance Method Summary collapse

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

#pointsObject (readonly)

Returns the value of attribute points.



20
21
22
# File 'app/models/flightdeck/metrics/chart.rb', line 20

def points
  @points
end

#windowObject (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_maxObject



56
57
58
# File 'app/models/flightdeck/metrics/chart.rb', line 56

def axis_max
  @axis_max ||= nice_ceiling(raw_max)
end

#empty?Boolean

Returns:

  • (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

#gridlinesObject



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

#heightObject



28
# File 'app/models/flightdeck/metrics/chart.rb', line 28

def height = HEIGHT

#plot_bottomObject



32
# File 'app/models/flightdeck/metrics/chart.rb', line 32

def plot_bottom = HEIGHT - PAD_BOTTOM

#plot_heightObject



34
# File 'app/models/flightdeck/metrics/chart.rb', line 34

def plot_height = plot_bottom - plot_top

#plot_leftObject



29
# File 'app/models/flightdeck/metrics/chart.rb', line 29

def plot_left = PAD_LEFT

#plot_rightObject



30
# File 'app/models/flightdeck/metrics/chart.rb', line 30

def plot_right = WIDTH - PAD_RIGHT

#plot_topObject



31
# File 'app/models/flightdeck/metrics/chart.rb', line 31

def plot_top = PAD_TOP

#plot_widthObject



33
# File 'app/models/flightdeck/metrics/chart.rb', line 33

def plot_width = plot_right - plot_left

#widthObject



27
# File 'app/models/flightdeck/metrics/chart.rb', line 27

def width = WIDTH

#x_ticksObject

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