KoolReport's Forum

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

Dashboard: Panel with dataSource #2309

Closed Andre Van Der Walt opened this topic on on Aug 30, 2021 - 15 comments

Andre Van Der Walt commented on Aug 30, 2021

Hi,

Is it possible to echo a value from dataSource() in a panel and what class should we extends to achieve this?

Thank you,

KoolReport commented on Aug 30, 2021

Could you please explain your case? what do you try to do? Why there is the need to echo value

Andre Van Der Walt commented on Aug 30, 2021

We want to display dynamic values on a dashboard which include time format values such as hh:mm:ss. It can also be outside of a panel.

KoolReport commented on Aug 30, 2021

You can extend the Text widget and query under onInit event, then set the text() value:

class MyText extends \koolreport\dashboard\widgets\Text
{
    protected function onInit()
    {
        $dataStore = AutoMaker::table("select something")->run(); //Run query to get data
        //Now you get a datastore, you can get value from it something like this:
        $value = $dataStore->get(0,"columnName"); //get columnName at first row
        $this->text($value);
    }
}

then you register the MyText in your dashboard.

Hope that helps.

Andre Van Der Walt commented on Aug 31, 2021

Thank you, is it possible to add a filter in there?

class DateRange extends DateRangePicker
{
    protected function actionChange($request,$response)
    {
        $this->sibling("MyText")->update();
    }
}

// MyText.php
class MyText extends \koolreport\dashboard\widgets\Text
{
    protected function onInit()
    {
        //Get value from the date range picker
        $range = $this->sibling("DateRange")->value();


        $dataStore = AutoMaker::table("select something")->whereBetween("date",$range)->run(); //Run query to get data
        //Now you get a datastore, you can get value from it something like this:
        $value = $dataStore->get(0,"columnName"); //get columnName at first row
        $this->text($value);
    }
}
KoolReport commented on Aug 31, 2021

Nice question, actually it is not possible to use onInit event because onInit the DateRange value may not be available. So better we use a later event which is onRendering, this event happen just before text is rendered.

    protected function onRendering()
    {
        //Get value from the date range picker
        $range = $this->sibling("DateRange")->value();


        $dataStore = AutoMaker::table("select something")->whereBetween("date",$range)->run(); //Run query to get data
        //Now you get a datastore, you can get value from it something like this:
        $value = $dataStore->get(0,"columnName"); //get columnName at first row
        $this->text($value);
        return true; //<---This is important to allow the text to be rendered
    }

Remember to return true; at the end of onRendering event.

Let me know if it works.

Andre Van Der Walt commented on Sep 1, 2021

When the report first render with the default value (defaultValue($this::last30days())) the value is populated in MyText. However when filtering using the DateRangePicker getting below error:

Message: Call to a member function update() on null
Line: 19
File: C:\...\Filters\DateRange.php
 Collapse
#0: C:\...\vendor\koolreport\dashboard\TAction.php Line 35 : actionChange([{},{}])

(path hidden)

KoolReport commented on Sep 1, 2021

When adding MyText to Dashboard, you do this:

return [
    ...
    MyText::create("MyText")
    ...
];

Let see if it works.

Mathieu Viennet commented on Sep 1, 2021

Hi! I don't want to hijack this thread, but I'm looking to do the exact same thing, but i'm not sure how to execute your last comment on adding the MyText to the dashboard. Could you please show a complete example? Here's my code. Note that i'm using RAW SQL.

Here's my code :

SalesBoard.php

<?php

use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;
use \koolreport\dashboard\containers\Html;

class SalesBoard extends Dashboard
{SalesBoard
    protected function widgets()
    {
        return [
            Row::create()->sub([
                Row::create(),
                SalesDateRange::create()->width(1/3),
            ]),
           Row::create()->sub([
                Panel::create()->sub([
                    SalesSalesMetric::create(),
                ])->width(1/3),
            ]),
            Row::create()->sub([
                Panel::create()->sub([
                    SalesByDate::create(),
                ])->width(1/1),
            ]),
			Row::create([
                Panel::create()->width(1/1)
					->sub([ Html::h2("Daily Sales Details") ])
					->cssClass("trans-row")
                ]),
            SalesTable::create(),
        ];
    }
}

SalesSalesMetric.php

<?php

use \koolreport\dashboard\metrics\Value;
use \koolreport\dashboard\fields\Date;
use \koolreport\dashboard\fields\Text;
use \koolreport\dashboard\fields\Currency;
use \koolreport\dashboard\containers\Panel;

class SalesSalesMetric extends Text
{

    protected function onRendering()
    {
        //Get value from the date range picker
        $range = $this->sibling("SalesDateRange")->value();


        $dataStore = Incognito::rawSQL("
		select *,
		cartDiscount + cartDiscountTax AS orderDiscount,
		orderTotal - orderTax - orderShipping - orderShippingTax - cartDiscount - cartDiscountTax - refundAmount AS orderNet
        FROM viuInvoicesRefundsByDate
        WHERE transactionDateTimestamp between '" . $range[0] . "' and '" . $range[1] . "'	     
		");
        //Now you get a datastore, you can get value from it something like this:
        $value = $dataStore->get(0,"orderNet"); //get columnName at first row
        $this->text($value);
        return true; //<---This is important to allow the text to be rendered
    }
}
KoolReport commented on Sep 1, 2021

Mathieu, Just put in inside the widgets() function:

<?php

use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;
use \koolreport\dashboard\containers\Html;

class SalesBoard extends Dashboard
{
    protected function widgets()
    {
        return [
            MyText::create("MyText"), //<-- Like this
            Row::create()->sub([
                Row::create(),
                SalesDateRange::create()->width(1/3),
            ]),
           Row::create()->sub([
                Panel::create()->sub([
                    SalesSalesMetric::create(),
                ])->width(1/3),
            ]),
            Row::create()->sub([
                Panel::create()->sub([
                    SalesByDate::create(),
                ])->width(1/1),
            ]),
			Row::create([
                Panel::create()->width(1/1)
					->sub([ Html::h2("Daily Sales Details") ])
					->cssClass("trans-row")
                ]),
            SalesTable::create(),
        ];
    }
}

Hope that helps.

Tip: if you find a topic is related to your, you can create new topic of your own and put reference link to that topic, and in your topic, you are freely to describe your own issue, we can give more precise answer to your case.

KoolReport commented on Sep 1, 2021

@Mathieu, by the way I see your SalesSalesMetric::create() inside widgets(), it seems in good place, you can change it to SalesSalesMetric::create("SalesSalesMetric")

Mathieu Viennet commented on Sep 1, 2021

OK i'm getting Message: Call to a member function state() on null Line: 20 File: /*path/reports/koolreport/dashboard/TWidgetState.php

Is it because how i select the value in $value = $dataStore->get(0,"orderNet"); ??

And thanks for the tip i'll start a new topic next time.

KoolReport commented on Sep 2, 2021

...

Mathieu Viennet commented on Sep 2, 2021

Sorry i'm not sure to follow your answer? Do you want me to start another topic now? Why the [...] ? Did I ask something stupid?

KoolReport commented on Sep 3, 2021

Sorry for the dots which cause misunderstanding, I was supposed to answer your another post but wrongly post here, I was not able to delete the post so I changed it to "...", meaning no content. Sorry for that. So the error still persist?. I have not been able to guess what went wrong. May be you can start a new topic with more detail explanation, including screenshot of the error and list of code.

Andre Van Der Walt commented on Sep 3, 2021

Nice thank you ! This solved the problem:

When adding MyText to Dashboard, you do this:

return [
    ...
    MyText::create("MyText")
    ...
];
Let see if it works.

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
help needed
solved

Dashboard