Prev | Current Page 77 | Next

David Berube

"Practical Reporting with Ruby and Rails"


CHAPTER 3 n CREATING GRAPHS WITH RUBY 47
Figure 3-5. Graph of data for all players
Let??™s take a look at the code line by line.
Dissecting the Code
Most of the code in Listing 3-5 is identical to that in Listing 3-4; however, a few lines are
different. For example, the object is a member of the Gruff::Line class, instead of the
Gruff::Bar class:
line_chart = Gruff::Line.new(1024)
The parameter to the call to new is the horizontal size. You can also pass a string containing
a full size parameter, such as 1024x768.
One of the noteworthy changes is inside the main loop, which iterates through all of
the players. Instead of writing each chart to a different file, the data is aggregated into a
single chart, as follows:
CHAPTER 3 n CREATING GRAPHS WITH RUBY 48
data = []
Event.find_by_sql(sql).each do |row|
data << (row.average_time.to_i/1000)
end
line_chart.data(player.nickname, data)
This loop, like the one in Listing 3-4, goes through all of the event types and adds
them into an array, and then adds that array to the chart.


Pages:
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
LMC free mp3 gry na gba osie do przyczep sennik