The KoolReport Blog

Announcements, discussions, and more for KoolReport and its extended packages.

Time Series Aggregation

In many cases, we have data in time series for example sales or costs. We would like to summarize them by time series such as sale by day, week, month or year. In this tutorial, I would like to show you simple way to build a sale report by quarter.

Database table

Suppose we have the table sales below:

idcustomerIdproductIdamountdate
1131202017-01-01
2211502017-01-02
3212002017-01-02
...............
1230223122002017-12-31

Setup processing part

require_once "../koolreport/autoload.php";

use \koolreport\processes\TimeBucket;
use \koolreport\processes\Group;

class QuarterSaleReport extends \koolreport\KoolReport
{
    function settings()
    {
        return array(
            "dataSources"=>array(
                "mydata"=>array(
                    'connectionString' => 'mysql:host=localhost;dbname=mydata',
                    'username' => 'root',
                    'password' => '',
                    'charset' => 'utf8',
                )
            )
        )
    }
    function setup()
    {
        $this->src("mydata")
        ->query("select date,amount from sales")
        ->pipe(new TimeBucket(array(
            "bucket"=>"quarter"
        )))
        ->pipe(new Group(array(
            "by"=>"date",
            "sum"=>"amount"
        )))
        ->pipe($this->dataStore("sale_by_quarter"));
    }
}

In above processing, we use the TimeBucket process. This special process will categorize the date into quarter in the year. In the next step, will pipe data into Group process and group by date which is now contain quarter data. The amount will be sumed by quarter is what we wanted.

Setup view part

We would like to use the Column Chart to display the sales of 4 quarter

<?php 
    use \koolreport\widgets\google\ColumnChart;
?>
<html>
    <head>
        <title>Quarter Sale Report</title>
    </head>
    <body>
        <?php 
        ColumnChart::create(array(
            "dataStore"=>$this->dataStore("sale_by_quarter")
        ));
        ?>
    </body>
</html>

Summary

In above tutorial, we learn how to deal with time series data, how to divide time series into chunks and group data by them.

If you have any question, you may reply to us of this email.

See you in the next tutorial.

Resources

  1. TimeBucket
  2. Group
  3. TimeBucket's Example

<3 koolreport team


KoolReport helps to analyze your data and ultimately turn them into visual reports or dynamic dashboards.

"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 product and amazing."

Dr. Lew Choy Onn

"Fantastic framework for reporting!"

Greg Schneider
Get KoolReport Now, It's FREE!