Aggregated Methods

Learn about DataStore's aggregated methods

We have $this->dataStore("data") like this:

nameincome
John $50,000
Marry $60,000
Peter $100,000
Donald $80,000

$count = $this->dataStore("data")->count();
$total_income = $this->dataStore("data")->sum("income");
$average_income = $this->dataStore("data")->avg("income");
$min_income = $this->dataStore("data")->min("income");
$max_income = $this->dataStore("data")->max("income");
$mode_income = $this->dataStore("data")->mode("income");
Count rows 4
Total income 290000
Average Income 72500
Min Income 50000
Max Income 100000
Mode Income 50000

The dataStore of KoolReport supports the most used aggregated functions count(), sum(), avg(), min(), max(), mode(). Those methods take column name as its parameter.

<?php
require_once "MyReport.php";

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

use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Filter;

//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("name","income"),
                        array("John",50000),
                        array("Marry",60000),
                        array("Peter",100000),
                        array("Donald",80000),
                    )
                )
            )
        );
    }
    protected function setup()
    {
        //Prepare data
        $this->src("data")
        ->pipe(new ColumnMeta(array(
            "income"=>array(
                "type"=>"number",
                "prefix"=>"$"
            )
        )))
        ->pipe($this->dataStore("data"));
    }
}
<?php
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;
?>
<div class="report-content">
    <div class="text-center">
        <h1>Aggregated Methods</h1>
        <p class="lead">Learn about DataStore's aggregated methods</p>
    </div>
    
    <p>We have <b>$this->dataStore("data")</b> like this:</p>
    <?php
    Table::create(array(
        "dataSource"=>$this->dataStore("data"),
        "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>
$count = $this->dataStore("data")->count();
$total_income = $this->dataStore("data")->sum("income");
$average_income = $this->dataStore("data")->avg("income");
$min_income = $this->dataStore("data")->min("income");
$max_income = $this->dataStore("data")->max("income");
$mode_income = $this->dataStore("data")->mode("income");
</code></pre>
<i class="fa fa-arrow-down" style="font-size:24px;"></i>


    <div class="row" style="margin-top:20px;">
        <div class="col-md-6">
            <table class="table table-bordered table-striped">
                <tbody>
                    <tr>
                        <td>
                            Count rows
                        </td>
                        <td><?php echo $this->dataStore("data")->count(); ?></td>
                    </tr>
                    <tr>
                        <td>
                            Total income
                        </td>
                        <td><?php echo $this->dataStore("data")->sum("income"); ?></td>
                    </tr>
                    <tr>
                        <td>
                            Average Income
                        </td>
                        <td><?php echo $this->dataStore("data")->avg("income"); ?></td>
                    </tr>
                    <tr>
                        <td>
                            Min Income
                        </td>
                        <td><?php echo $this->dataStore("data")->min("income"); ?></td>
                    </tr>
                    <tr>
                        <td>
                            Max Income
                        </td>
                        <td><?php echo $this->dataStore("data")->max("income"); ?></td>
                    </tr>
                    <tr>
                        <td>
                            Mode Income
                        </td>
                        <td><?php echo $this->dataStore("data")->mode("income"); ?></td>
                    </tr>
                </tbody>
            </table>        
        </div>
    </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 Get KoolReport Pro