Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dongnl committed Feb 12, 2019
1 parent c41e3c9 commit 59c6ff7
Show file tree
Hide file tree
Showing 27 changed files with 829 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koolphp/koolreport-examples",
"version":"1.0.0",
"version":"1.1.0",
"description": "Examples for KoolReport Framework.",
"keywords": ["KoolReport php example", "php reporting framework","php reporting tools","data processing","data visualization","charts and graphs"],
"homepage": "https://www.koolreport.com",
Expand Down
7 changes: 6 additions & 1 deletion reports.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"Combo Chart":"/reports/google_charts/combo_chart/",
"Pareto Chart":"/reports/google_charts/pareto_chart/",
"Sankey":"/reports/google_charts/sankey/",
"TreeMap":"/reports/google_charts/treemap/",
"Color Scheme":"/reports/google_charts/color_scheme/"
}
},
Expand Down Expand Up @@ -107,6 +108,9 @@
"Table Sorting":"/reports/datagrid/datatables_sorting/",
"Table Column Reorder":"/reports/datagrid/datatables_colreorder/",
"Server Processing":"/reports/datagrid/server_processing/"
},
"<i class='fa fa-file-excel-o'></i>Excel":{
"Excel export template":"/reports/excel/excel_template/"
},
"<i class='fa fa-file-pdf-o'></i>Export":{
"PDF Exporting":"/reports/export/sakila_rental/"
Expand Down Expand Up @@ -149,7 +153,8 @@
},
"ADVANCED EXAMPLES":{
"Advanced":{
"Input &amp; Export":"/reports/advanced/input_and_export/"
"Input &amp; Export":"/reports/advanced/input_and_export/",
"Multiple Data Filters":"/reports/advanced/multiple_data_filters/"
},
"SubReport":{
"Ajax Loading":"/reports/others/subreport_demo/"
Expand Down
74 changes: 74 additions & 0 deletions reports/advanced/multiple_data_filters/MyReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
//Step 1: Load KoolReport
require_once "../../../../koolreport/autoload.php";

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
use \koolreport\inputs\Bindable;
use \koolreport\inputs\POSTBinding;

protected function defaultParamValues()
{
return array(
"years"=>array(2003),
"customerNames"=>array(),
"productLines"=>array(),
);
}

protected function bindParamsToInputs()
{
return array(
"years",
"customerNames",
"productLines"
);
}

protected function settings()
{
$config = include "../../../config.php";
return array(
"dataSources"=>$config
);
}
protected function setup()
{
$query_params = array();
if($this->params["years"]!=array())
{
$query_params[":years"] = $this->params["years"];
}
if($this->params["customerNames"]!=array())
{
$query_params[":customerNames"] = $this->params["customerNames"];
}
if($this->params["productLines"]!=array())
{
$query_params[":productLines"] = $this->params["productLines"];
}

$this->src('automaker')->query("
select
customerName,
productLine,
YEAR(orderDate) as year,
sum(quantityOrdered*priceEach) as amount
from orders
join customers
on
customers.customerNumber = orders.customerNumber
join orderdetails
on orders.orderNumber = orderdetails.orderNumber
join products
on products.productCode = orderdetails.productCode
where 1=1
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")."
".(($this->params["customerNames"]!=array())?"and customerName in (:customerNames)":"")."
".(($this->params["productLines"]!=array())?"and productLine in (:productLines)":"")."
GROUP BY year, productLine, customerName
")->params($query_params)
->pipe($this->dataStore("orders"));
}
}
109 changes: 109 additions & 0 deletions reports/advanced/multiple_data_filters/MyReport.view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\Select2;
?>
<div class="report-content">
<div class="text-center">
<h1>Multiple Data Filters</h1>
<p class="lead">
The example demonstrate how to build dynamic reports with multiple data filters
</p>
</div>

<form method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<b>Select Years</b>
<?php
Select2::create(array(
"multiple"=>true,
"name"=>"years",
"dataSource"=>$this->src("automaker")->query("
select YEAR(orderDate) as year
from orders
group by year
"),
"attributes"=>array(
"class"=>"form-control"
)
));
?>
</div>

<div class="form-group">
<b>Select Product Lines</b>
<?php
Select2::create(array(
"multiple"=>true,
"name"=>"productLines",
"dataSource"=>$this->src("automaker")->query("
select productLine
from orders
join orderdetails on orders.orderNumber = orderdetails.orderNumber
join products on products.productCode = orderdetails.productCode
".(($this->params["years"]!=array())?"":"where YEAR(orderDate) in (:years")."
group by productLine
")->params(
$this->params["years"]!=array()?
array(":years"=>$this->params["years"]):
array()
),
"attributes"=>array(
"class"=>"form-control"
)
));
?>
</div>
<div class="form-group">
<b>Select Customers</b>
<?php
Select2::create(array(
"multiple"=>true,
"name"=>"customerNames",
"dataSource"=>$this->src("automaker")->query("
select customerName
from orders
join customers on customers.customerNumber = orders.customerNumber
".(($this->params["years"]!=array())?"":"where YEAR(orderDate) in (:years")."
group by customerName
")->params(
$this->params["years"]!=array()?
array(":years"=>$this->params["years"]):
array()
),
"attributes"=>array(
"class"=>"form-control"
)
));
?>
</div>
<div class="form-group">
<button class="btn btn-primary">Submit</button>
</div>
</div>
</div>

</form>
<?php
Table::create(array(
"dataSource"=>$this->dataStore("orders"),
"columns"=>array(
"customerName",
"productLine",
"amount"=>array("prefix"=>"$"),
"year"=>array("format"=>false)
),
"grouping"=>array(
"year",
"productLine"
),
"paging"=>array(
"pageSize"=>25
),
"cssClass"=>array(
"table"=>"table-bordered"
)
));
?>
</div>
80 changes: 80 additions & 0 deletions reports/advanced/multiple_data_filters/_MyReport.php.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
//Step 1: Load KoolReport
require_once "../../../../koolreport/autoload.php";

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
use \koolreport\inputs\Bindable;
use \koolreport\inputs\POSTBinding;

protected function defaultParamValues()
{
return array(
"years"=>array(2003),
"customerNames"=>array(),
"productLines"=>array(),
);
}

protected function bindParamsToInputs()
{
return array(
"years",
"customerNames",
"productLines"
);
}

protected function settings()
{
return array(
"dataSources"=>array(
"automaker"=>array(
"connectionString"=>"mysql:host=localhost;dbname=automaker",
"username"=>"root",
"password"=>"",
"charset"=>"utf8"
),
)
);
}
protected function setup()
{
$query_params = array();
if($this->params["years"]!=array())
{
$query_params[":years"] = $this->params["years"];
}
if($this->params["customerNames"]!=array())
{
$query_params[":customerNames"] = $this->params["customerNames"];
}
if($this->params["productLines"]!=array())
{
$query_params[":productLines"] = $this->params["productLines"];
}

$this->src('automaker')->query("
select
customerName,
productLine,
YEAR(orderDate) as year,
sum(quantityOrdered*priceEach) as amount
from orders
join customers
on
customers.customerNumber = orders.customerNumber
join orderdetails
on orders.orderNumber = orderdetails.orderNumber
join products
on products.productCode = orderdetails.productCode
where 1=1
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")."
".(($this->params["customerNames"]!=array())?"and customerName in (:customerNames)":"")."
".(($this->params["productLines"]!=array())?"and productLine in (:productLines)":"")."
GROUP BY year, productLine, customerName
")->params($query_params)
->pipe($this->dataStore("orders"));
}
}
80 changes: 80 additions & 0 deletions reports/advanced/multiple_data_filters/_description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
The example demonstrate how to build a dynamic reports with multiple data filters. In this example, the data selection can be filter by `year`, `productLine` and `customerName`. Those data filters can be multi-selected meaning that you can select more than 1 years, more than 1 product lines or more than 1 customer to view.

The example use `Select2` from [inputs package](https://www.koolreport.com/packages/inputs) to construct the parameters selection.

We use the [Row Group] feature of Table for better data visualization. This grouping features support unlimited levels of grouping. There is very limited data table in market supporting this features. This feature is totally free with KoolReport.

__Code Explanation__:

The report use `\koolreport\inputs\Bindable` and `\koolreport\inputs\POSTBinding` services which will allows data binding between the report parameters and inputs controls. So the selection from select2 controls like years,productLines and customerNames will be bound to the corresponding report params.

We use the `defaultParamValues()` methods in the report to define default starting selection of users. As you can see from the code, we pre-select the year `2003`. You may add different year, or set preselected for customerNames or productLines

We use the `bindParamsToInputs()` to bind the name of report parameters to the name of the input controls. To keep things simple, the report parameters and name of input controls are the same:

```
protected function bindParamsToInputs()
{
return array(
"years",
"customerNames",
"productLines"
);
}
```

It is equivalent to

```
protected function bindParamsToInputs()
{
return array(
"years"=>"years",
"customerNames"=>"customerNames",
"productLines"=>"productLines"
);
}
```

In `setup()` function, base on the selection of users we add custom condition to the SQL query. Below code means that do not add the year condition if user does not select year.

```
".(($this->params["years"]!=array())?"and YEAR(orderDate) in (:years)":"")."
```

And also, we have this line of code:

```
$query_params = array();
if($this->params["years"]!=array())
{
$query_params[":years"] = $this->params["years"];
}
```

meaning that if user select year, then we add the year to variable `$query_params` to be used as parameters for sql query.

In the view, we have some advance code for `Select2` widget:

```
<?php
Select2::create(array(
"multiple"=>true,
"name"=>"years",
"dataSource"=>$this->src("automaker")->query("
select YEAR(orderDate) as year
from orders
group by year
"),
"attributes"=>array(
"class"=>"form-control"
)
));
?>
```

As you may notice, in `"dataSource"` of Select2 we use directly SQL command to query the available year.

So as user selects years, productLines and customerNames, we will execute and store result to `"orders"` dataStore later be visualized in `Table`.

__Enjoy the example!__
Loading

0 comments on commit 59c6ff7

Please sign in to comment.