Prev | Current Page 70 | Next

David Berube

"Practical Reporting with Ruby and Rails"

find(:all, :group=>'event DESC')
pic_dir='./player_graph_pics' #Used to store the graph pictures.
Dir.mkdir(pic_dir) unless File.exists?(pic_dir)
CHAPTER 3 n CREATING GRAPHS WITH RUBY 40
Player.find(:all).each do |player|
bar_chart = Gruff::Bar.new(1024)
bar_chart.legend_font_size = 12
total_games = Play.count(:conditions=>['game_id = ? ' <<
'AND player_id = ?',
game_id_to_analyze,
player.id]).to_f
total_wins = Play.count(:conditions=>['game_id = ? ' <<
'AND player_id = ? ' <<
'AND won=1',
game_id_to_analyze,
player.id]).to_f
win_ratio = (total_wins / total_games * 100).to_i unless total_games == 0
win_ratio ||= 0
bar_chart.title = "#{player.name} " <<
"(#{win_ratio}% won)"
bar_chart.minimum_value = 0
bar_chart.maximum_value = 110
sql = "SELECT event, AVG(time) as average_time
FROM events AS e
INNER JOIN
plays AS p
ON e.play_id=p.id
WHERE p.game_id='#{game_id_to_analyze}'
AND
p.player_id='#{player.id}'
GROUP
BY e.event DESC;"
data = []
Event.find_by_sql(sql).each do |row|
bar_chart.data row.


Pages:
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
szkoła narciarska wyrejestrowanie samochodu legnica obciążniki Wczasy nad morzem serwis drukarek katowice