KoolReport's Forum

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

How to get raw data from ArrayDataSource? #1806

Closed Venom opened this topic on on Dec 29, 2020 - 5 comments

Venom commented on Dec 29, 2020

Hi,

I am building a Report using ArrayDataSource. However, I could not get the raw data. Below is my code.

class MyReport extends \koolreport\KoolReport
{
    public function settings()
    {
    if(array_key_exists('dataset',$this->params)){
     
        return array(
            "dataSources"=>array(
                "array_example_datasource"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"associate",
                    "data"=>$this->params['dataset']
                ),
            )
        );
      }
     else{
            return array(
            "dataSources"=>array(
                "array_example_datasource"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"associate", 
                    "data"=>array()
                ),
            )
        );
    }
}

 protected function setup(){
 
  $this->src('array_example_datasource')
  ->pipe($this->dataStore("datax"));
 
 }
}


$data = array(
    array("country"=>"US","amount"=>50000),
    array("country"=>"Canada","amount"=>31000),
    array("country"=>"Mexico","amount"=>42000),
);


$myReport = new MyReport(array("dataset"=>$data));

print_r( $myReport->dataStore('datax')->data());  //empty Array() is shown

KoolReport commented on Dec 30, 2020

You need to run report first, please try

$myReport = new MyReport(array("dataset"=>$data));

$myReport->run();

print_r( $myReport->dataStore('datax')->data());  //empty Array() is shown
Venom commented on Dec 30, 2020

Hi Team,

Great. I get the raw data now. Can I further pipe the datastore to Processes flow and get the Raw data again?

$myReport = new MyReport(array("dataset"=>$data));

$myReport->run();
 
 $myReport->dataStore('datax')
  ->pipe(new CalculatedColumn(array(
            "amount"=>"{amount}*2",
            "recievedAmount"=>"{amount}-100",
            "power"=>"pow({amount},10)"
        )))
 ->pipe($myReport->dataStore('calculatedData'));


$calculated =$myReport->dataStore("calculatedData")->data();

print_r( $calculated);  //Empty array is shown
KoolReport commented on Dec 30, 2020

You should pipe() inside the setup() function:

 protected function setup(){
 
  $this->src('array_example_datasource')
   ->pipe(new CalculatedColumn(array(
            "amount"=>"{amount}*2",
            "recievedAmount"=>"{amount}-100",
            "power"=>"pow({amount},10)"
        )))
  ->pipe($this->dataStore("calculatedData"));
 
 }
$myReport = new MyReport(array("dataset"=>$data));

$myReport->run();

print_r( $myReport->dataStore('calculatedData')->data());  //empty Array() is shown
Venom commented on Dec 30, 2020

Thank you for your help. I managed to get the raw data from processed flow now. It means after running the run() on the report, no further pipe process can be applied on the report.

KoolReport commented on Dec 30, 2020

Yes, dataStore should be the end destination containing data ready to be visualized.

Although it is possible to continue processing from dataStore like below, but it is not recommended as it is quite memory consuming especially when you have large data.


$calculatedStore = $myReport->dataStore("dataX")->process(
    new CalculatedColumn(array(
            "amount"=>"{amount}*2",
            "recievedAmount"=>"{amount}-100",
            "power"=>"pow({amount},10)"
        ))
);
print_r($calculatedStore->data());

Hope that helps.

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

None