Skip to content
schacon edited this page Sep 12, 2010 · 16 revisions

This section uses the Munger::Data object data in the MungerData section.

Now that we have a data set in our Munger::Data object, we will format it as a report. This includes sorting and grouping and aggregating and such. This is all in the abstract – then you pass it off to a MungerRender class to actually be rendered.

Specifying Columns

The first thing you will notice in some of the previous examples is that the column order was random. In Munger, you have to specify the columns you want to see and what order they will go in. So without specifying anything, it will use all columns, but that is not fantastic, because it will choose them randomly and they can change each time – hashes don’t have ordered keys. You also don’t have to use every column.

Without formatting, you will get output like this:

|airdate    | advert | rate | clicks | airtime | 
-------------------------------------------------
|2008-01-01 | Spot 1 | 20   | 301    | 15      | 
|2008-01-02 | Spot 1 | 6    | 199    | 30      | 
...

However, if you run this :

report.columns([:advert, :airdate, :airtime, :rate])

|advert | airdate | airtime | rate |
-——————————————————-
|Spot 1 | 2008-01-01 | 15 | 20 |
|Spot 1 | 2008-01-02 | 30 | 6 |

Notice that we now control the order and the ‘clicks’ field is no longer shown.

You can also set prettier titles to be displayed like this:


report.columns(:advert => ‘Spot’, :airdate => ‘Air Date’, :airtime => ‘Airtime’, :rate => ‘Rate’)


|Spot | Rate | Air Date | Airtime |
-——————————————————-
|Spot 1 | 20 | 2008-01-01 | 15 |
|Spot 1 | 6 | 2008-01-02 | 30 |

Sorting

You can sort the values :


report.sort(‘airtime’)

or

report.sort([‘airtime’, ‘rate’])

or

report.sort([['airtime', :desc], ['rate', :asc]])
|Spot   | Rate | Air Date   | Airtime | 
----------------------------------------
|Spot 2 | 11   | 2008-01-03 | 90      | 
|Spot 3 | 8    | 2008-01-01 | 60      | 
|Spot 2 | 5    | 2008-01-01 | 30      | 
|Spot 1 | 6    | 2008-01-02 | 30      | 
|Spot 1 | 7    | 2008-01-03 | 30      | 
|Spot 2 | 10   | 2008-01-04 | 30      | 
|Spot 3 | 27   | 2008-01-02 | 30      | 
|Spot 2 | 14   | 2008-01-02 | 15      | 
|Spot 1 | 20   | 2008-01-01 | 15      | 
|Spot 1 | 22   | 2008-01-04 | 15      | 
|Spot 3 | 22   | 2008-01-03 | 15      | 

Subgrouping

Aggregating

Styling

  • rows
  • cells
Clone this wiki locally