diff --git a/Pearlception/Gemfile b/Pearlception/Gemfile index e4b3cfc..07f5318 100644 --- a/Pearlception/Gemfile +++ b/Pearlception/Gemfile @@ -20,7 +20,8 @@ gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.2' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby - +gem 'chart-js-rails' +gem 'jquery-ui-rails' # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks diff --git a/Pearlception/Gemfile.lock b/Pearlception/Gemfile.lock index eb8e8fc..3370f70 100644 --- a/Pearlception/Gemfile.lock +++ b/Pearlception/Gemfile.lock @@ -53,6 +53,8 @@ GEM bootstrap-popover-rails (0.1.0) builder (3.2.2) byebug (9.0.6) + chart-js-rails (0.1.2) + railties (> 3.1) coderay (1.1.1) coffee-rails (4.2.1) coffee-script (>= 2.2.0) @@ -87,6 +89,8 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + jquery-ui-rails (6.0.1) + railties (>= 3.2.16) kaminari (1.0.1) activesupport (>= 4.1.0) kaminari-actionview (= 1.0.1) @@ -215,6 +219,7 @@ DEPENDENCIES bcrypt (~> 3.1.7) bootstrap-popover-rails byebug + chart-js-rails coffee-rails (~> 4.2) devise high_voltage (~> 3.0.0) @@ -222,6 +227,7 @@ DEPENDENCIES hirb-unicode jbuilder (~> 2.5) jquery-rails + jquery-ui-rails kaminari listen (~> 3.0.5) mysql2 (>= 0.3.18, < 0.5) diff --git a/Pearlception/app/assets/javascripts/application.js b/Pearlception/app/assets/javascripts/application.js index fe47c91..1f319ca 100644 --- a/Pearlception/app/assets/javascripts/application.js +++ b/Pearlception/app/assets/javascripts/application.js @@ -15,3 +15,5 @@ //= require turbolinks //= require bootstrap/bootstrap-rails-tooltip //= require bootstrap/bootstrap-rails-popover +//= require Chart +//= require jquery-ui diff --git a/Pearlception/app/assets/javascripts/dashboard.coffee b/Pearlception/app/assets/javascripts/dashboard.coffee deleted file mode 100644 index 24f83d1..0000000 --- a/Pearlception/app/assets/javascripts/dashboard.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/Pearlception/app/assets/javascripts/dashboard.js b/Pearlception/app/assets/javascripts/dashboard.js new file mode 100644 index 0000000..e69de29 diff --git a/Pearlception/app/assets/javascripts/statistics.js b/Pearlception/app/assets/javascripts/statistics.js new file mode 100644 index 0000000..1911156 --- /dev/null +++ b/Pearlception/app/assets/javascripts/statistics.js @@ -0,0 +1,102 @@ +/* +Set up both the datepicker tabs with this +*/ +$('#from_datepicker').datepicker() +$('#to_datepicker').datepicker() + +$('#dateFilterButton').on('click',function(){ + var fromDate = $('#from_datepicker')[0].value; + var toDate = $('#to_datepicker')[0].value; + var queryString = ""; + if(fromDate != "" && toDate != ""){ + queryString = "statistics.json/?from_date="+encodeURI(fromDate)+"&to_date="+encodeURI(toDate); + } + else{ + queryString = "statistics.json" + } + $.get(queryString, function(gradeData){ + constructGradesGraph(gradeData); + }) +}); + +$(document).ready( + $.get("statistics.json", function(gradeData){ + constructGradesGraph(gradeData); + }) +) + +function constructGradesGraph(gradeData){ + console.log(gradeData) + var labels = generateLabels(gradeData); + var colors = generateColors(gradeData); + var gradeCounts = getGradeCounts(labels, gradeData); + var ctx = document.getElementById("gradesChart"); + var myChart = new Chart(ctx, { + type: 'bar', + data: { + labels: labels, + datasets: [{ + label: 'Count for each grade', + data: gradeCounts, + backgroundColor: colors, + borderColor: colors, + borderWidth: 1 + }] + }, + options: { + scales: { + yAxes: [{ + ticks: { + beginAtZero:true + } + }] + } + } + }); +} + +function getGradeCounts(labels, gradeData){ + var counts = initCountArray(labels); + var oysters = gradeData.allOysters; + for(var i = 0; i < oysters.length; i++){ + var grade = oysters[i].grade; + counts[labels.indexOf(grade)]++; + } + console.log(counts); + return counts; +} + +function initCountArray(labels){ + counts = []; + for(var i = 0; i < labels.length; i++){ + counts.push(0) + } + return counts; +} + +function generateLabels(gradeData){ + let grades = gradeData.grades; + labels = []; + for(var i = 0; i < grades.length; i++){ + labels.push(grades[i].Full_name) + } + return labels; +} + +function generateColors(gradeData){ + colors = []; + availableColors = [ + 'rgba(255, 99, 132, 0.2)', + 'rgba(54, 162, 235, 0.2)', + 'rgba(255, 206, 86, 0.2)', + 'rgba(75, 192, 192, 0.2)', + 'rgba(153, 102, 255, 0.2)', + 'rgba(255, 159, 64, 0.2)' + ]; + for(var i = 0; i < gradeData.grades.length; i++){ + var index = Math.round(Math.random() * (availableColors.length - 1)); + var color = availableColors[index] + colors.push(color); + } + return colors; +} \ No newline at end of file diff --git a/Pearlception/app/assets/stylesheets/application.css b/Pearlception/app/assets/stylesheets/application.css index a75d597..8b971c4 100644 --- a/Pearlception/app/assets/stylesheets/application.css +++ b/Pearlception/app/assets/stylesheets/application.css @@ -14,4 +14,5 @@ *= require_self *= require bootstrap/bootstrap-rails-tooltip *= require bootstrap/bootstrap-rails-popover + *= require jquery-ui */ diff --git a/Pearlception/app/assets/stylesheets/statistics.scss b/Pearlception/app/assets/stylesheets/statistics.scss new file mode 100644 index 0000000..b05be63 --- /dev/null +++ b/Pearlception/app/assets/stylesheets/statistics.scss @@ -0,0 +1,7 @@ +// Place all the styles related to the statistics controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ +#myChart{ + width:100%; + height: 100%; +} diff --git a/Pearlception/app/controllers/dashboard_controller.rb b/Pearlception/app/controllers/dashboard_controller.rb index 02d741b..e51857e 100644 --- a/Pearlception/app/controllers/dashboard_controller.rb +++ b/Pearlception/app/controllers/dashboard_controller.rb @@ -8,7 +8,8 @@ class DashboardController < ApplicationController puts params @results = Hash.new names = (Company.pluck :company_name) - ["IVA"] - names.map!{|tenant| tenant.gsub(/'/,'').gsub(/\s/,'')} + binding.pry + names.map!{|tenant| tenant.gsub(/'/,'').gsub(/\s/,'')} names.each do |name| @@ -32,4 +33,8 @@ class DashboardController < ApplicationController redirect_to "/signin" unless user_signed_in? end + def statistics + + end + end diff --git a/Pearlception/app/controllers/statistics_controller.rb b/Pearlception/app/controllers/statistics_controller.rb new file mode 100644 index 0000000..10b2ce4 --- /dev/null +++ b/Pearlception/app/controllers/statistics_controller.rb @@ -0,0 +1,25 @@ +class StatisticsController < ApplicationController + + def index + if params[:from_date] != nil && params[:to_date] != nil + from_date = Date.strptime(params[:from_date], '%m/%d/%Y') + to_date = Date.strptime(params[:to_date], '%m/%d/%Y') + runs = Run.where(:runDate => from_date.beginning_of_day..to_date.end_of_day) + else + runs = Run.all + end + puts runs.inspect + oysterData = [] + all_oysters = [] + runs.each do |run| + oysters = Oyster.where(run_id: run.id).to_a + data = {"run": run, "oysters": oysters} + oysterData.push(data) + all_oysters.concat(oysters) + end + respond_to do |f| + f.html + f.json {render :json => {grades:Grade.all,oysterData:oysterData,allOysters:all_oysters}} + end + end +end diff --git a/Pearlception/app/helpers/statistics_helper.rb b/Pearlception/app/helpers/statistics_helper.rb new file mode 100644 index 0000000..2d25d41 --- /dev/null +++ b/Pearlception/app/helpers/statistics_helper.rb @@ -0,0 +1,2 @@ +module StatisticsHelper +end diff --git a/Pearlception/app/views/dashboard/index.html.erb b/Pearlception/app/views/dashboard/index.html.erb index 21783b8..87791de 100644 --- a/Pearlception/app/views/dashboard/index.html.erb +++ b/Pearlception/app/views/dashboard/index.html.erb @@ -40,6 +40,7 @@ <% if current_user.admin? %>
From:
+To:
+