Extract chart value object

This simplifies the views (slightly).
This commit is contained in:
Katrina Owen 2013-03-01 20:38:24 -05:00
parent 8b0f3e986a
commit 0aa6ea183b
7 changed files with 65 additions and 41 deletions

View file

@ -50,8 +50,7 @@ class StatsController < ApplicationController
def actions_done_last_years def actions_done_last_years
@page_title = t('stats.index_title') @page_title = t('stats.index_title')
@chart_width = 900 @chart = Stats::Chart.new('actions_done_lastyears_data', :height => 400, :width => 900)
@chart_height = 400
end end
def actions_done_lastyears_data def actions_done_lastyears_data
@ -309,7 +308,7 @@ class StatsController < ApplicationController
week_from = params['index'].to_i week_from = params['index'].to_i
week_to = week_from+1 week_to = week_from+1
@chart_name = "actions_visible_running_time_data" @chart = Stats::Chart.new('actions_visible_running_time_data')
@page_title = t('stats.actions_selected_from_week') @page_title = t('stats.actions_selected_from_week')
@further = false @further = false
if params['id'] == 'avrt_end' if params['id'] == 'avrt_end'
@ -334,7 +333,7 @@ class StatsController < ApplicationController
week_from = params['index'].to_i week_from = params['index'].to_i
week_to = week_from+1 week_to = week_from+1
@chart_name = "actions_running_time_data" @chart = Stats::Chart.new('actions_running_time_data')
@page_title = "Actions selected from week " @page_title = "Actions selected from week "
@further = false @further = false
if params['id'] == 'art_end' if params['id'] == 'art_end'
@ -421,12 +420,6 @@ class StatsController < ApplicationController
def init def init
@me = self # for meta programming @me = self # for meta programming
# default chart dimensions
@chart_width=460
@chart_height=250
@pie_width=@chart_width
@pie_height=325
# get the current date wih time set to 0:0 # get the current date wih time set to 0:0
@today = Time.zone.now.utc.beginning_of_day @today = Time.zone.now.utc.beginning_of_day
@ -468,6 +461,27 @@ class StatsController < ApplicationController
# get count of actions done in the past 12 months. # get count of actions done in the past 12 months.
@sum_actions_done_last12months = current_user.todos.completed.completed_after(@cut_off_year).count @sum_actions_done_last12months = current_user.todos.completed.completed_after(@cut_off_year).count
@sum_actions_created_last12months = current_user.todos.created_after(@cut_off_year).count @sum_actions_created_last12months = current_user.todos.created_after(@cut_off_year).count
@completion_charts = %w{
actions_done_last30days_data
actions_done_last12months_data
actions_completion_time_data
}.map do |action|
Stats::Chart.new(action)
end
@timing_charts = %w{
actions_visible_running_time_data
actions_running_time_data
actions_open_per_week_data
actions_day_of_week_all_data
actions_day_of_week_30days_data
actions_time_of_day_all_data
actions_time_of_day_30days_data
}.map do |action|
Stats::Chart.new(action)
end
end end
def get_stats_contexts def get_stats_contexts
@ -496,6 +510,13 @@ class StatsController < ApplicationController
"GROUP BY c.id, c.name ORDER BY total DESC " + "GROUP BY c.id, c.name ORDER BY total DESC " +
"LIMIT 5" "LIMIT 5"
) )
@context_charts = %w{
context_total_actions_data
context_running_actions_data
}.map do |action|
Stats::Chart.new(action, :height => 325)
end
end end
def get_stats_projects def get_stats_projects

18
app/models/stats/chart.rb Normal file
View file

@ -0,0 +1,18 @@
module Stats
class Chart
attr_reader :action, :height, :width
def initialize(action, dimensions = {})
@action = action
@height = dimensions.fetch(:height) { 250 }
@width = dimensions.fetch(:width) { 460 }
end
def dimensions
"#{width}x#{height}"
end
end
end

View file

@ -7,28 +7,13 @@
<%= t('stats.actions_avg_created', :count => (@sum_actions_created_last12months*10.0/12.0).round/10.0 )%> <%= t('stats.actions_avg_created', :count => (@sum_actions_created_last12months*10.0/12.0).round/10.0 )%>
<%= t('stats.actions_avg_completed', :count => (@sum_actions_done_last12months*10.0/12.0).round/10.0 )%></p> <%= t('stats.actions_avg_completed', :count => (@sum_actions_done_last12months*10.0/12.0).round/10.0 )%></p>
<% <% @completion_charts.each do |chart| %><%=
%w{ render :partial => 'chart', :locals => {:chart => chart}
actions_done_last30days_data -%><% end %>
actions_done_last12months_data
actions_completion_time_data
}.each do |action|
%><%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => action)} -%><%
end
%>
<br style="clear:both"> <br style="clear:both">
<% <% @timing_charts.each do |chart| %><%=
%w{ render :partial => 'chart', :locals => {:chart => chart}
actions_visible_running_time_data -%><% end %>
actions_running_time_data
actions_open_per_week_data
actions_day_of_week_all_data
actions_day_of_week_30days_data
actions_time_of_day_all_data
actions_time_of_day_30days_data
}.each do |action|
%><%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => action)} -%><%
end
%>

View file

@ -1,6 +1,6 @@
<% @swf_count ||= 0 -%> <% @swf_count ||= 0 -%>
<div class="open-flash-chart"><%= swf_tag asset_path("open-flash-chart.swf"), <div class="open-flash-chart"><%= swf_tag asset_path("open-flash-chart.swf"),
:flashvars => { 'width' => width, 'height' => height, 'data' => data}, :flashvars => { 'width' => chart.width, 'height' => chart.height, 'data' => url_for(:action => chart.action)},
:parameters => { 'allowScriptAccess' => 'sameDomain', 'wmode' => 'transparent'}, :parameters => { 'allowScriptAccess' => 'sameDomain', 'wmode' => 'transparent'},
:div_id => "chart_#{@swf_count+=1}", :div_id => "chart_#{@swf_count+=1}",
:size => "#{width}x#{height}" %></div> :size => chart.dimensions %></div>

View file

@ -1,6 +1,6 @@
<%= render :partial => 'chart', :locals => {:width => @pie_width, :height => @pie_height, :data => url_for(:action => 'context_total_actions_data')} -%> <% @context_charts.each do |chart| %><%=
render :partial => 'chart', :locals => {:chart => chart}
<%= render :partial => 'chart', :locals => {:width => @pie_width, :height => @pie_height, :data => url_for(:action => 'context_running_actions_data')} -%> -%><% end %>
<br style="clear:both"> <br style="clear:both">
@ -34,4 +34,4 @@
<br/><% <br/><%
end end
-%> -%>
</div> </div>

View file

@ -1,2 +1,2 @@
<%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => :actions_done_lastyears_data)} -%> <%= render :partial => 'chart', :locals => {:chart => @chart} -%>
<%= raw t('stats.click_to_return', :link => link_to(t('stats.click_to_return_link'), stats_path)) %> <%= raw t('stats.click_to_return', :link => link_to(t('stats.click_to_return_link'), stats_path)) %>

View file

@ -1,4 +1,4 @@
<%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => @chart_name)} -%> <%= render :partial => 'chart', :locals => {:chart => @chart} -%>
<br/> <br/>
<p> <p>
<%= t('stats.click_to_update_actions') %> <%= raw t('stats.click_to_return', :link => link_to(t('stats.click_to_return_link'), stats_path)) %> <%= t('stats.click_to_update_actions') %> <%= raw t('stats.click_to_return', :link => link_to(t('stats.click_to_return_link'), stats_path)) %>
@ -22,4 +22,4 @@
</div> </div>
<%= render :partial => "todos/todo", :collection => @selected_actions, :locals => { :parent_container_type => 'stats' } %> <%= render :partial => "todos/todo", :collection => @selected_actions, :locals => { :parent_container_type => 'stats' } %>
</div> </div>
</div> </div>