Map Process
Map data rows' value and metadata
productName | orderYear | orderQuarter | dollar_sales |
1937 Lincoln Berline |
2003 |
1 |
3726.45 |
1936 Mercedes-Benz 500K Special Roadster |
2003 |
1 |
1768.3300000000002 |
1952 Alpine Renault 1300 |
2003 |
1 |
5571.8 |
1962 LanciaA Delta 16V |
2003 |
1 |
5026.14 |
1958 Setra Bus |
2003 |
1 |
3284.28 |
->pipe(new Map(array(
'{value}' => function($row, $metaData) {
$row['orderQuarter'] = 'Q ' . $row['orderQuarter'];
return array($row);
},
'{meta}' => function($metaData) {
$metaData['columns']['productName'] = array(
'label' => 'Products',
);
$metaData['columns']['orderYear'] = array(
'label' => 'Year',
'type' => 'string',
);
$metaData['columns']['orderQuarter'] = array(
'label' => 'Quarter',
'type' => 'string',
);
$metaData['columns']['dollar_sales'] = array(
'label' => 'Sales',
'type' => 'number',
"prefix" => "$",
);
return $metaData;
},
)))
Products | Year | Quarter | Sales |
1937 Lincoln Berline |
2003 |
Q 1 |
$3,726 |
1936 Mercedes-Benz 500K Special Roadster |
2003 |
Q 1 |
$1,768 |
1952 Alpine Renault 1300 |
2003 |
Q 1 |
$5,572 |
1962 LanciaA Delta 16V |
2003 |
Q 1 |
$5,026 |
1958 Setra Bus |
2003 |
Q 1 |
$3,284 |
This example shows how to use `Map` process of KoolReport. This process is advanced way to transform your data. Using `Map` process, you can controls both meta data of data flow as well as the values. Virtually you can do all most everything with this process. So instead of driving your data through various processes, you may just use `Map` with custom code to reach same result. Of course, since it is more powerful and flexible, it is a little more difficult to use in comparing to other processes. In return, it will make your code shorter and faster and do exactly what you want to do.
In the `"{value}"` property, you define a custom function which recieved `$row` as parameters. Each row of data will be sent to your function before it comes back to the mainstream. Note that the function return an array of rows. If you want to add more rows to data stream, you can do so.
The `"{meta}"` is property which is basically a function that received old meta data. You may process meta data then return a new meta data. Or if there is no change in meta data, you will return exactly the old meta there.