diff --git a/app/controllers/chart_handler.rb b/app/controllers/chart_handler.rb index 5aff963..7be4757 100644 --- a/app/controllers/chart_handler.rb +++ b/app/controllers/chart_handler.rb @@ -4,6 +4,11 @@ def shape_chart setup_chart(shape_label, shape_data, 'Reported Shapes') end + def country_chart + shape_label, shape_data = AlienSighting.count_by_country + setup_chart(shape_label, shape_data, 'Sightings by country') + 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 25b7839..bbd7067 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -4,7 +4,7 @@ def index end def dashboard - @country_counts = AlienSighting.count_by_country + @country_chart = country_chart @city_counts = AlienSighting.count_by_cities @state_counts = AlienSighting.count_by_state @shape_chart = shape_chart diff --git a/app/models/alien_sighting.rb b/app/models/alien_sighting.rb index 9988f9f..f362a4d 100644 --- a/app/models/alien_sighting.rb +++ b/app/models/alien_sighting.rb @@ -34,10 +34,12 @@ def self.shape_by_country(shape) end def self.count_by_country + format_for_chart( group(:country) .order("count(country) DESC") .count .first(5) + ) end def self.count_by_cities(country = 'us', number = 10) diff --git a/app/views/main/dashboard.html.erb b/app/views/main/dashboard.html.erb index 9ee4883..d96db85 100644 --- a/app/views/main/dashboard.html.erb +++ b/app/views/main/dashboard.html.erb @@ -1,13 +1,7 @@

ITS A DASHBOARD

-<%= pie_chart @shape_chart, {:width => 100, :height => 100} %> - -
    - Sightings by Country - <% @country_counts.each do |country, count| %> -
  1. Country: <%= country %>, <%= count %>
  2. - <% end %> -
+<%= pie_chart @shape_chart, {:width => 50, :height => 100} %> +<%= bar_chart @country_chart %>
    Sightings by US city