Pivot

Overview #

There are two Pivot widgets called PivotTable and PivotMatrix which are able to display pivot data. PivotTable is a fixed displaying widget for pivot data while PivotMatrix is a dynamic widget which allows users to drag and drop pivot fields, do sorting, etc.

Properties #

Namedescription
dataSourceGet/set the datasource provide to Pivot
processGet/set the pivot process settings. See details
displayGet/set the displaying settings. See details

Note: Those above properties follows this code rules.

Examples #

Create chart from dashboard

use \koolreport\dashboard\Dashboard;

class PivotBoard extends Dashboard
{
    protected function content()
    {
        return [
            Panel::create()->type("danger")->header("<b>PivotTable</b>")->sub([
                CustomersPivotTable::create(),
            ]),

            Panel::create()->type("danger")->header("<b>PivotMatrix</b>")->sub([
                CustomersPivotMatrix::create(),
            ]),

        ];
    }
}

Create a separate widget class

<?php
use \koolreport\dashboard\widgets\pivot\PivotMatrix;
use \koolreport\dashboard\sources\CSV;

class CustomersPivotMatrix extends PivotMatrix
{
    protected function dataSource()
    {
        return CSV::file("data/customer_product_dollarsales2.csv")
                ->fieldSeparator(";")
                ->select('customerName', 'productLine', 'productName','dollar_sales')
                ->where('customerName','<','Am')
                ->where('orderYear','>',2003)
                ->run();
    }

    protected function process()
    {
        return [
            'dimensions'=>array(
                'column'=>'productLine',
                'row'=>'customerName, productName'
            ),
            'aggregates'=>array(
                'sum'=>'dollar_sales',
                'count'=>'dollar_sales'
            ),
        ];
    }

    protected function display()
    {
        return [
            'rowSort' => array(
              'dollar_sales - sum' => 'desc',
            ),
            'rowCollapseLevels' => array(0),
            'map' => array(
                'dataField' => [
                    'dollar_sales - sum' => 'Sales (in USD)',
                    'dollar_sales - count' => 'Number of Sales',
                ]
            )
        ];
    }

}

process() #

process method is similar to \koolreport\pivot\processes\Pivot process which includes "dimensions" and "aggregates" properties:

    protected function process()
    {
        return [
            'dimensions'=>array(
                'column'=>'productLine',
                'row'=>'customerName, productName'
            ),
            'aggregates'=>array(
                'sum'=>'dollar_sales',
                'count'=>'dollar_sales'
            ),
        ];
    }

"dimensions" contains collections of label fields while "aggregates" consists of pairs of aggregate functions and data fields.

For detail description please refer to the KoolReport's Pivot process:

Pivot process

display() #

display method is similar to \koolreport\pivot\widgets\PivotTable and \koolreport\pivot\widgets\PivotMatrix widgets' settings:

    protected function display()
    {
        return [
            'measures' => ...,
            'rowSort' => array(
              'dollar_sales - sum' => 'desc',
            ),
            'columnSort' => ...,
            'rowCollapseLevels' => array(0),
            'colCollapseLevels' => ...,
            'map' => array(
                'dataField' => function($dataField, $fieldInfo) {
                    return $dataField;
                },
                'rowField' => ...,
            ),
            'cssClass' => array(
                'dataField' => function($field, $fieldInfo) {
                    return 'df-' . $field;
                },
                'rowField' => ...,
            },
            'paging' => array(
                'size' => 5,
                'maxDisplayedPages' => 5,
                'sizeSelect' => array(5, 10, 20, 50, 100)
            ),
            'totalName' => 'All',
            'hideTotalRow' => false,
            'hideTotalColumn' => false,
            'hideSubtotalRow' => false,
            'hideSubtotalColumn' => false,
            'showDataHeaders' => false,
        ];
    }

For detail description please refer to the KoolReport's widgets:

PivotTable and PivotMatrix widgets

Traits #

PivotTable and PivotMatrix have been provided with following traits:

  1. TAppLink: Able to refer to application with app() method
  2. TDashboadLink: Able to refer to parent dashboard with dashboard() method
  3. TEnabledPermit: Use enabled() and enabledWhen() to set permission
  4. TParams: Able to get/set parameters with params() method
  5. TWidgetState: Able to get/set persisted state for widget
  6. TParamsPersisted: Able to make params set to widget persisted
  7. TDataSource: Able to receive datasource via dataSource() method
  8. TExportable: Able to export widget to different formats

Get started with KoolReport

KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.