Prev | Current Page 72 | Next

David Berube

"Practical Reporting with Ruby and Rails"

find(:all).each do |player|
bar_chart = Gruff::Bar.new(1024)
bar_chart.legend_font_size = 12
The script creates a new Gruff::Bar object. The legend_font_size attribute is the font
size of the chart??™s legend in points, The default is 20 points, but this graph has too much
information for the larger text size, so you reduce it to 12 points.
CHAPTER 3 n CREATING GRAPHS WITH RUBY 43
Next, the script sets the title of the chart:
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)"
The title of the chart is set to the player??™s name, followed by a win/loss ratio. The calculation
uses the count aggregate function (discussed in Chapter 2).
Next, you set the scale of the chart:
bar_chart.


Pages:
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
Morcheeba Marc et Claude Grażyna Łobaszewska Monty Python Sergio Mendes