Prev | Current Page 320 | Next

David Berube

"Practical Reporting with Ruby and Rails"

max { |a,b| a.count.to_i <=> b.count.to_i }.count.to_f
story_data = @stories.map{|x| ["#{x.published_at_formatted} (#{x.count})",
(x.count.to_f / story_count_max)*100]}
%>
<%= complex_bar_graph story_data %>


The first two lines of code in the view prepare the data for the third line of code. The
complex_bar_graph method expects an array whose elements are each two-element arrays.
These two-element arrays consist of a label element followed by a value element. The
value element is expected to be in the range of 0 to 100, so you first calculate the maximum
count value. Then you use Ruby??™s built-in map method to transformthe array of
CHAPTER 11 n TRACKING THE NEWS WITH GOOGLE NEWS 231
Story objects into an array of two-element arrays??”what complex_bar_graph wants as an
argument.
nNote Why isn??™t the data in Listing 11-4 in the controller instead of the view? The reason is that it??™s viewspecific.
If you were using, say, the Open Flash Chart component (introduced in Chapter 5), you would need
the data in a different format.


Pages:
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
programy Aplikacje Internetowe Kredyt hipoteczny kalkulator pozycjonowanie stron freeroll titan poker