From f11c2d004ad96d9d5556b97760a15579e0f38c63 Mon Sep 17 00:00:00 2001 From: Brandon Stewart Date: Mon, 4 Sep 2017 15:05:04 -0600 Subject: [PATCH] Add by hour of day chart. --- app/controllers/chart_handler.rb | 7 ++++++- app/controllers/main_controller.rb | 4 ++++ app/models/alien_sighting.rb | 3 ++- app/views/main/dashboard.html.erb | 1 + app/views/main/time_of_day.html.erb | 1 + config/routes.rb | 1 + 6 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 app/views/main/time_of_day.html.erb diff --git a/app/controllers/chart_handler.rb b/app/controllers/chart_handler.rb index 9cb56d8..bf96d0c 100644 --- a/app/controllers/chart_handler.rb +++ b/app/controllers/chart_handler.rb @@ -33,7 +33,12 @@ def per_capita_state_chart per_capita_label, per_capita_data = AlienSighting.get_per_capita_sightings_by_state setup_chart(per_capita_label, per_capita_data, "Sightings by state per capita") end - + + def time_of_day_chart + by_hour_label, by_hour_data = AlienSighting.by_hour_of_day + setup_chart(by_hour_label, by_hour_data, "Sightings by hour") + end + def setup_chart(data_label, chart_data, chart_label) { labels: data_label, diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index bffc753..4a2a341 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -31,6 +31,10 @@ def per_capita_by_state @per_capita_by_state_chart = per_capita_state_chart end + def time_of_day + @time_of_day_chart = time_of_day_chart + end + def dashboard @shapes = AlienSighting.all_shapes end diff --git a/app/models/alien_sighting.rb b/app/models/alien_sighting.rb index 6a6ad4e..d77a5eb 100644 --- a/app/models/alien_sighting.rb +++ b/app/models/alien_sighting.rb @@ -42,9 +42,10 @@ def self.shape_by_country(shape) end def self.by_hour_of_day + format_for_chart( group("date_part('hour', date_posted)") .order("count(date_posted) DESC") - .count + .count) end def self.shape_by_state(shape) diff --git a/app/views/main/dashboard.html.erb b/app/views/main/dashboard.html.erb index 75bf75f..dfa07ef 100644 --- a/app/views/main/dashboard.html.erb +++ b/app/views/main/dashboard.html.erb @@ -5,6 +5,7 @@ <%= link_to 'Sightings by year', years_path, class: "dropbtn" %>
<%= link_to 'Sightings by US city', cities_path, class: "dropbtn" %>
<%= link_to 'Total sightings by reported shape', shapes_path, class: "dropbtn" %>
+<%= link_to 'Sightings by hour of day', by_time_of_day_path, class: "dropbtn" %>