KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Can't find variable: KoolReport #1828

Closed Venom opened this topic on on Jan 8, 2021 - 2 comments

Venom commented on Jan 8, 2021

I am trying to build a PivotTable by following the years_months sample and provide the Array as Datasource instead of CSV. https://www.koolreport.com/examples/reports/pivot/years_months/

Below is my code

report_pivot_sales.php

<?php
require '../vendor/autoload.php';
require_once "../vendor/koolreport/core/autoload.php";

use \koolreport\processes\ColumnMeta;
use \koolreport\pivot\processes\Pivot;
use \koolreport\processes\Filter;



class SalesPivotReport extends \koolreport\KoolReport
{
    public function settings()
    {
        return array(
            "dataSources"=>array(
                "array_example_datasource"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"table", //Table data format
                    "data"=>array(
                        array("custname","confirm_amount","signed_month","signed_year"),
                        array("A1","8000","10","2020"),
                        array("A2","2000","10","2020"),
                        array("A3","3000","10","2020"),
                        array("A4","4000","11","2020"),
                        array("A5","5000","10","2020"),
                        array("A6","6000","10","2020"),
                        array("A7","7000","11","2020"),
                        array("A8","9000","10","2020"),
                        array("A9","2000","10","2020"),
                        array("A10","3000","10","2020"),
                        array("A11","14000","10","2020"),
                        array("A12","5000","11","2020"),
                        array("A13","8000","11","2020"),
                        array("A14","2000","11","2020"),
                        array("A15","4000","11","2020"),
                        array("A16","15000","11","2020"),
                        array("A17","6000","11","2020"),
                        array("A18","6000","11","2020"),
                    )
                ),
            )
        );
    }

    protected function setup(){
 
        $node= $this->src('array_example_datasource');
        
        $node->pipe(new Filter(array(
            
            array('orderYear', '>', 2003)
        )))
        ->pipe(new ColumnMeta(array(
            "confirm_amount"=>array(
                'type' => 'number',
                "prefix" => "$",
            ),
        )))
       ->pipe(new Pivot(array(
        "dimensions" => array(
            "row" => "custname",
            "column" => "signed_year,signed_month"
        ),
        "aggregates"=>array(
            "sum"=>"confirm_amount",
            "count"=>"confirm_amount",

        )
        )))
      
        ->pipe($this->dataStore('sales'));
       
       }
}

report_pivot_sales.view.php

<?php 
use \koolreport\pivot\widgets\PivotTable;
?>
<div class="report-content">
  <div class="text-center">
    <h1>Sale Report</h1>
    <p class="lead">
      Aggregate sale amount and sale count by years and months.
    </p>
  </div>
  <?php
    $dataStore = $this->dataStore('sales');
    PivotTable::create(array(
      "dataStore"=>$dataStore,
      "columnDimension"=>"column",
      "measures"=>array(
        "confirm_amount - sum", 
      ),
      'rowSort' => array(
        'confirm_amount - count' => 'desc',
      ),
      'columnSort' => array(
        'signed_month' => function($a, $b) {
          return (int)$a < (int)$b;
        },
      ),
      'columnCollapseLevels' => array(1),
      'width' => '100%',
      'nameMap' => array(
        'confirm_amount - sum' => 'Sales (in USD)',
        'confirm_amount - count' => 'Number of Sales',
        '1' => 'January',
        '2' => 'February',
        '3' => 'March',
        '4' => 'April',
        '5' => 'May',
        '6' => 'June',
        '7' => 'July',
        '8' => 'August',
        '9' => 'September',
        '10' => 'October',
        '11' => 'November',
        '12' => 'December',
      ),
    ));
  ?>
  
</div>

report_pivot_sale_test.php

<?php 

require_once("report_pivot_sales.php");

$salesPivotReport = new SalesPivotReport;
$salesPivotReport->run()->render();

However, it doesn't look like the years_months sample and a javascript error is shown.

Screen

Javascript Error

Venom commented on Jan 8, 2021

The problem has been resolved by specifying the "report_view" in $salesPivotReport->run()->render('report_pivot_sales');

Screen shot

KoolReport commented on Jan 8, 2021

That's great that you found the issue :)

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

Pivot