Group Process

This example shows the usage of Group process

itemcost
Accessories $3,000
Human Resource $20,000
Material $50,000
Machine $30,000
Accessories $5,000
Machine $10,000
Material $20,000

->pipe(new Group(array(
    "by"=>"item",
    "sum"=>"cost",
)))
itemcost
Accessories $8,000
Human Resource $20,000
Machine $40,000
Material $70,000

The example demonstrates usage of Group process. The group process is used to group row of data and do aggregation calculation.

Other methods

Beside "sum" aggregation, the group process also support other methods: "avg","min", "max", "min", "first", "last".

For example:

->pipe(new Group(array(
    "by"=>"item",
    "avg"=>"cost"
)))

By list of fields

The Group process can group not only by a field but also by multiple fields:

->pipe(new Group(array(
    "by"=>array("category","line")
    "sum"=>"amount"
)))
<?php
require_once "MyReport.php";

$report = new MyReport;
$report->run()->render();
<?php
//Step 1: Load KoolReport
require_once "../../../../koolreport/autoload.php";

use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Group;

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
    protected function settings()
    {
        return array(
            "dataSources"=>array(
                "data"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"table",
                    "data"=>array(
                        array("item","cost"),
                        array("Accessories",3000),
                        array("Human Resource",20000),
                        array("Material",50000),
                        array("Machine",30000),
                        array("Accessories",5000),
                        array("Machine",10000),
                        array("Material",20000),                
                    )
                )
            )
        );
    }
    protected function setup()
    {
        //Prepare data
        $this->src("data")
        ->pipe(new ColumnMeta(array(
            "cost"=>array(
                "type"=>"number",
                "prefix"=>"$"
            )
        )))
        ->saveTo($source);

        //Save orginal data
        $source->pipe($this->dataStore("origin"));
        
        //Pipe through process to get result
        $source->pipe(new Group(array(
            "by"=>"item",
            "sum"=>"cost"
        )))
        ->pipe($this->dataStore("result"));
    }
}
<?php
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;
?>
<div class="report-content">
    <div class="text-center">
        <h1>Group Process</h1>
        <p class="lead">This example shows the usage of Group process</p>
    </div>
    
    <?php
    Table::create(array(
        "dataSource"=>$this->dataStore("origin"),
        "cssClass"=>array(
            "table"=>"table-bordered table-striped table-hover"
        )
    ));
    ?>

<i class="fa fa-arrow-down" style="font-size:24px;"></i>
<pre style="font-weight:bold"><code>
->pipe(new Group(array(
    "by"=>"item",
    "sum"=>"cost",
)))
</code></pre>
<i class="fa fa-arrow-down" style="font-size:24px;"></i>

    <div style="margin-top:20px;">
    <?php
    Table::create(array(
        "dataSource"=>$this->dataStore("result"),
        "cssClass"=>array(
            "table"=>"table-bordered table-striped table-hover"
        )
    ));
    ?>
    </div>

</div>

What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider

Download KoolReport