Create DataSource
Standard structure #
class MyNewDataSource extend \koolreport\core\DataSource
{
/**
* Be called when datasource is initiated
*
* @return null
*/
protected function onInit()
{
// This method is called when datasource is initated
// You may get all the parameters of datasource through $this->params
}
/**
* Start piping data
*
* @return null
*/
public function start()
{
// Everything start with sending meta data
$this->sendMeta($metaData,$this);
//Call startInput() to begin data pipe
$this->startInput(null);
//Loop through your data and use next() to send row by row of data
foreach($data as $row)
{
$this->next($row);
}
//At the end, call endInput() to close the data pipe
$this->endInput(null);
}
}
Detail guide #
- All new datasource is extended from
\koolreport\core\DataSource
class - The
onInit()
method will be called after your datasource is initiated. You can get all initiating parameters passed to your datasource through$this->params
property. You use these settings to setup your datasource. - When report runs, the
start()
method will be triggered. In here, you will start piping data to data pipe. - You start with sending meta data with
$this->sendMeta($metaData,$this);
method. The meta data is information about your data. You may view the structure of$metaData
below. - Next you will call
$this->startInput()
to notify the datapipe that you are about to send data - You loop through your data row by row and use
$this->next($row)
to send. The$row
is array data in associate format for example{"name"=>"Peter","age"=>35}
- After sending all data, you call
$this->endInput(null)
to notify data stream that you have sent all data.
Meta data structure #
The structure of meta data is following:
array(
"columns"=>array(
"name"=>array(
"type"=>"string"
),
"age"=>array(
"type"=>"number"
),
"createdAt"=>array(
"type"=>"datetime"
),
"joinedDate"=>array(
"type"=>"date"
),
"updatedTime"=>array(
"type"=>"time"
),
"arrayData"=>array(
"type"=>"array"
),
"unknownData"=>array(
"type"=>"unknown"
)
)
)
Basically the meta data contains list of columns in key and value holding the properties of that columns. In each column, the "type"
settings is the most important property which specifies the type of that column. If you do not know type of your column data, you can just put "unknown"
to the "type"
.
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.