Skip to content

Commit

Permalink
Add actor genres-statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
syhsyh9696 committed Nov 20, 2017
1 parent 5278364 commit 6a15a92
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
7 changes: 6 additions & 1 deletion app/controllers/actors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ def show
@actor = Actor.find(params[:id])
end

def dataset
def output
@actor = Actor.find(params[:id])
render :json => @actor.videos_dataset
end

def genres
@actor = Actor.find(params[:id])
render :json => @actor.genres_dataset
end
end
7 changes: 6 additions & 1 deletion app/models/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ def videos_dataset
end

def genres_dataset
result = Hash.new
result = Hash.new; name = Array.new; json = Hash.new

self.videos.each do |video|
video.genres.each do |genre|
result[genre.name] = 1 if result[genre.name] == nil
result[genre.name] += 1
end
end

result.each { |index, value| name << index }
json['genres'] = name; json['values'] = result.values
json
end

end
13 changes: 9 additions & 4 deletions app/views/actors/_chart.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<div id="chart-wrapper">
<canvas id="myChart"></canvas>
<div class="panel panel-primary">
<div class="panel-heading text-center">Annual Output</div>
<div class="panel-body">
<div id="chart-wrapper">
<canvas id="output"></canvas>
</div>
</div>
</div>

<script>
var actor_label;
var actor_dataset;
var url = window.location.href;
url = url + "/dataset"
url = url + "/output"

$.ajax({
url: url,
Expand All @@ -19,7 +24,7 @@ $.ajax({
}
});

var ctx = document.getElementById("myChart").getContext('2d');
var ctx = document.getElementById("output").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
Expand Down
41 changes: 41 additions & 0 deletions app/views/actors/_genres.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<div class="panel panel-primary">
<div class="panel-heading text-center">Genres Statistics</div>
<div class="panel-body">
<div id="chart-wrapper">
<canvas id="genres"></canvas>
</div>
</div>
</div>

<script>
var genres;
var values;
var url = window.location.href;
url = url + "/genres"

$.ajax({
url: url,
dataType: 'JSON',
type: 'GET',
async: false,
success: function(result) {
genres = result.genres;
values = result.values;
}
});

var ctx = document.getElementById("genres").getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels : genres,
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
data : values
}
]
}
});
</script>
7 changes: 1 addition & 6 deletions app/views/actors/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@
</div>
<div class="tab-pane fade" id="dropdown1">
<br>
<div class="panel panel-primary">
<div class="panel-heading text-center">Annual Output</div>
<div class="panel-body">
<%= render "chart" %>
</div>
</div>
<%= render "chart" %>
</div>
<div class="tab-pane fade" id="dropdown2">
<p>Trust fund seitan letterpress, keytar raw denim keffiyeh etsy art party before they sold out master cleanse gluten-free squid scenester freegan cosby sweater. Fanny pack portland seitan DIY, art party locavore wolf cliche high life echo park Austin. Cred vinyl keffiyeh DIY salvia PBR, banh mi before they sold out farm-to-table VHS viral locavore cosby sweater.</p>
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

resources :actors, only:[:index, :show] do
member do
get 'dataset'
get 'output'
get 'genres'
end
end

Expand Down

0 comments on commit 6a15a92

Please sign in to comment.