Good day,
Can you please assist?
We are trying to create a dynamic dashboard based on query output numbers.
The headings are already dynamic as we can pass them through with the header property.
The idea is to pass the variable to the various queries which will return the values for each sub panel in each panel.
Some values are hardcoded or commented out to illustrate an output.
DynamicWidgetBoard.php:
<?php
namespace ...;
use ...\AutoMaker;
use ...\DynamicWidgetOne;
use ...\DynamicWidgetTwo;
use ...\DynamicWidgetThree;
use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;
use koolreport\dashboard\containers\Tabs;
class DynamicWidgetBoard extends Dashboard
{
private function getFarmData($region)
{
// Get Farm Data for the region
$query = AutoMaker::rawSQL("SELECT
1 FarmId, 'FarmOne' FarmName
WHERE
$region = $region
UNION
SELECT
2 FarmId, 'FarmTwo' FarmName
WHERE
$region = $region
UNION
SELECT
3 FarmId, 'FarmThree' FarmName
WHERE
$region = $region
")
->run();
$farmOutput = $query->toJson();
return ($farmOutput);
}
protected function widgets()
{
$region = 2; //User input
$dynamicRows = [];
$dynamicPanel = [];
$farmData[] = ($this->getFarmData($region));
$panelCount = 3;//Count of farms in the region;
for ($i = 0; $i < $panelCount; $i++) {
$fId = json_decode($farmData[0], true)[$i]["FarmId"];
$farmData[] = ($this->getFarmData($fId, $region));
$farmName = ((json_decode($farmData[$i], true)[$i]["FarmName"]));
$dynamicPanel[] =
Panel::create()
->header("<b>$farmName</b>")
->width(1 / 2)
->sub([
Panel::create()
->header("<span style='font-weight: 700;'></i> Pigs</span>")
->cssClass("panel-card")
->sub([
DynamicWidgetOne::create()
]),
Panel::create()
->header("<span style='font-weight: 700;'></i> Cows</span>")
->cssClass("panel-card")
->sub([
DynamicWidgetTwo::create(),
]),
Panel::create()
->header("<span style='font-weight: 700;'></i> Sheep</span>")
->cssClass("panel-card")
->sub([
DynamicWidgetThree::create(),
])
]);
}
for ($i = 0; $i < 1; $i++) {
$dynamicRows[] = Row::create(
$dynamicPanel,
Text::create()->text("<br>")->asHtml(true)
);
}
return [
Text::create()->text("<h3>Farm Info</h3>")->asHtml(true),
Text::create()->text("<br>")->asHtml(true),
Tabs::create()
->addTab("Region 1",[
Row::create([
Panel::create()
->width(1 / 1)
->sub([
Panel::create()
->sub([
Row::create([
$dynamicRows,
]),
]),
]),
]),
]),
];
}
}
DynamicDashboardOne:
<?php
namespace ...;
use ...\AutoMaker;
class DynamicWidgetOne extends \koolreport\dashboard\widgets\Text
{
protected function onRendering()
{
//Received from DynamicWidgetBoard
$farmId = 1;
// $query = AutoMaker::table("someTable")
// ->count("DISTINCT value")->alias("Total")
// ->whereRaw("id = $farmId")
// ->run();
//Mimic query output
$query = 0;
if ($farmId == 1) {
$query = 88;
}
elseif ($farmId == 2) {
$query = 25;
}
elseif ($farmId == 3) {
$query = 29;
}
$value = $query;//->get(0,"Total");
$value = number_format($value);
$this->text($value);
return true;
}
}
DynamicDashboardTwo:
<?php
namespace ...;
use ...\AutoMaker;
class DynamicWidgetTwo extends \koolreport\dashboard\widgets\Text
{
protected function onCreated()
{
$this
->cssStyle("font-size: 1.5rem; color: #36a9e1; font-weight: 500;");
}
protected function onRendering()
{
//Received from DynamicWidgetBoard
$farmId = 1;
// $query = AutoMaker::table("someTable")
// ->count("DISTINCT value")->alias("Total")
// ->whereRaw("id = $farmId")
// ->run();
//Mimic query output
$query = 0;
if ($farmId == 1) {
$query = 26;
}
elseif ($farmId == 2) {
$query = 19;
}
elseif ($farmId == 3) {
$query = 22;
}
$value = $query;//->get(0,"Total");
$value = number_format($value);
$this->text($value);
return true;
}
}
DynamicDashboardThree:
<?php
namespace ...;
use ...\AutoMaker;
class DynamicWidgetThree extends \koolreport\dashboard\widgets\Text
{
protected function onRendering()
{
//Received from DynamicWidgetBoard
$farmId = 1;
// $query = AutoMaker::table("someTable")
// ->count("DISTINCT value")->alias("Total")
// ->whereRaw("id = $farmId")
// ->run();
//Mimic query output
$query = 0;
if ($farmId == 1) {
$query = 5;
}
elseif ($farmId == 2) {
$query = 10;
}
elseif ($farmId == 3) {
$query = 8;
}
$value = $query;//->get(0,"Total");
$value = number_format($value);
$this->text($value);
return true;
}
}