Skip to content

Commit

Permalink
Add by hour of day chart.
Browse files Browse the repository at this point in the history
  • Loading branch information
boveus committed Sep 4, 2017
1 parent 677234d commit f11c2d0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/controllers/chart_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/models/alien_sighting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/views/main/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<%= link_to 'Sightings by year', years_path, class: "dropbtn" %> <br />
<%= link_to 'Sightings by US city', cities_path, class: "dropbtn" %> <br />
<%= link_to 'Total sightings by reported shape', shapes_path, class: "dropbtn" %> <br />
<%= link_to 'Sightings by hour of day', by_time_of_day_path, class: "dropbtn" %> <br />
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Shape count by state</button>
<div id="myDropdown" class="dropdown-content">
Expand Down
1 change: 1 addition & 0 deletions app/views/main/time_of_day.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= pie_chart @time_of_day_chart %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'main#index'
get 'dashboard/bytimeofday', :to => 'main#time_of_day', :as => 'by_time_of_day'
get 'dashboard/shapes', :to => 'main#shapes', :as => 'shapes'
get 'dashboard/cities', :to => 'main#cities', :as => 'cities'
get 'dashboard/years', :to => 'main#years', :as => 'years'
Expand Down

0 comments on commit f11c2d0

Please sign in to comment.