event, (row.average_time.to_i/1000)
end
bar_chart.labels = {0=>'Time'}
bar_chart.write("#{pic_dir}/player_#{player.id}.png")
end
CHAPTER 3 n CREATING GRAPHS WITH RUBY 41
Save this script as player_bar_charts.rb. You can run the script using the following
command:
ruby player_bar_charts.rb
When you run that command, the script creates a directory called player_graph_pics
and creates one graph for each player. The graphs are saved in PNG format as player_1,
player_2, and so forth. You can see two of the graphs in Figures 3-3 and 3-4.
nNote Chapter 10 includes an example that uses Gruff to graph cost-per-sale figures from Apache web
logs. Chapter 10 also covers using .rpdf views to embed those graphs in a PDF file, which is probably a
more convenient way to e-mail graphs than as PNG files.
Figure 3-3. Graph for one player
CHAPTER 3 n CREATING GRAPHS WITH RUBY 42
Figure 3-4. Graph for another player, with a different build order
Dissecting the Code
The bulk of the program in Listing 3-4 consists of a loop that iterates through each player
in the database, creating a report and writing it to a file:
Player.
Pages:
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83