KoolReport's Forum

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

Draw multiple dynamic graphics anync #2636

Open Joaquin Duro opened this topic on on Apr 11, 2022 - 8 comments

Joaquin Duro commented on Apr 11, 2022

Hello everyone.

I hava a question related to load multiple graphics in a same page. I am working using Koolreport in Symfony. I have a class "Report" which has references to various graphics which are stored in DDBB and then, we take this information and draw the graphic.

When page is loaded, start AJAX request that response HTML code generated by Koolreport and then put it on a grid at corresponding position. Depending on DB values, I generate different SQL queries to get the graphic (using twig at personalized view).

My question: Is there anyway that several graphics are loaded at same time? I make AJAX request in a loop for each corresponding graphic, and it seems that AJAX request start at same time, but until first is not finished it looks like next doesn't start Koolreport job and it takes so long if first is slow but other are fast, because they have to "wait" the first. I have read about SubReport but i can't define those subreport statically beacuse depends on each Report and how many graphics it has.

Is it posible or I am doing something wrong?

If you need more information just ask for it.

Thank you very much.

Joaquin Duro commented on Apr 11, 2022

Doing it with for loop, they do not draw in order (AJAX are called at near same time and each one return in different moment), but they continue to take a long time compared to the time of the each query, it looks like the process of get the graphic cannot be done in parallel.

Here you can see, they are called at same point, and not return in order, but times are not the ideal. Really, the time for each one is the differnece between his time and the previous. I mean, third really is not 121408ms long, it is 121408-89296ms aprox. It is the main problem I have.

Thank you.

Sebastian Morales commented on Apr 12, 2022

Hi Joaquin, each KoolReport report class loading requires multiple resources (js, css, etc files) so it would not be the most optimized to ajax load multiple reports per page because of multiple abundant file requests. Instead it's advisable to use SubReport trait to load multiple reports as they would share the same resource files. Here's a standard example of using SubReport to load multiple widgets (chart, table, etc):

https://www.koolreport.com/examples/reports/others/async_loading/

Pls try to copy and modify its source code for your page and let us know if you have any question/difficulty. We would help you to solve them. Tks,

Joaquin Duro commented on Apr 12, 2022

Hi Sebastian. Thank you for your answer.

SubReport was the first thing I found at research but I do not know how to implement in my particular case. I have different pages (let's call it "BigReport"). Each BigReport has variable graphic (Koolreport report) number, and all graphics are different instances of same Koolreport class using different MySQL query to get correct data in each case; so I am not sure about how to define the "MainReport".

Here you can see two examples. All graphics use the same Koolreport class:

So my problem is how to declare MainReport (to use SubReports) if graphic number is dynamic for each page.

Thank you.

Sebastian Morales commented on Apr 12, 2022

How do you decide the number of graphics for a page? Can you use that logic to define the main report's sub reports as well each sub report's data?

Joaquin Duro commented on Apr 12, 2022

The users add graphics, one-by-one, to each BigReport. At photo's page there is a button to "Add graphic". It goes to a page where you configure values for that graphic query and save it at Database table. Then, when you come back to BigReport page, graphics should be loaded using database info; and it does, but not asynchronously. I could know how many gaphics a BigReport has, but it will be changing.

Thank you.

Sebastian Morales commented on Apr 12, 2022

I imagine we could configure your BigReport's sub reports upon users' graphic adding like this:

//BigReport.php

    function settings()
    {
        if (isset($_POST["addGraphic"])) {
            $graphicName = ...;
            $subreportContent = ...;
            $subreportViewContent = ...;
            $subReportPath = $path . "/" . $grapicName . ".php";
            $subReportViewPath = $path . "/" . $grapicName . ".view.php";
            file_put_content($subReportPath, $subreportContent);
            file_put_content($subReportViewPath, $subreportViewContent);
        }
        $listOfSubReports = ...;
        $listOfSubReports[$graphicName] = $subReportPath;
        $this->listOfSubReports = $listOfSubReports;
        return array(
            "subReports"=>$listOfSubReports
        );
    } 

//BigReport.view.php
// render sub reports based on $this->listOfSubReports

It's a general idea. If you have any difficulty in detail implementation let us know. Tks,

Joaquin Duro commented on Apr 12, 2022

Thank you for the answer, it looks really interesting.

But using this, do I need to have a .php and .view.php file for each graphic? Actually all graphics use same files and by params I set values to define correct SQL query. And using this, would I need to generate dynamic .php files? I do not know it could be possible...

BigReports are dynamic too, it means, user can create BigReports and graphics for it.

Thank you very much for your time.

Sebastian Morales commented on Apr 13, 2022

Oh, if all of your graphics are generated using the same report file it's even easier. Just set your BigReport's list of sub reports to the same file path, but load each sub report with their own parameters for different graphics:

//BigReport.php
    $pathToSameSubReport = ...;
    require_once $pathToSameSubReport;
    function settings()
    {
        $listOfSubReports = ...;
        if (isset($_POST["addGraphic"])) {
            $graphicName = ...;
            $listOfSubReports[$graphicName] = $pathToSameSubReport;
        }
        $this->listOfSubReports = $listOfSubReports;
        return array(
            "subReports"=>$listOfSubReports
        );
    }  

//BigReport.view.php
<!--
    Add sub report dom for each sub reports, for example:
    foreach ($this->listOfSubReports as $subReportName => $subReportPath) {
        echo "<sub-report name='TopCustomersReport' id='{$subReportName}'></sub-report>";
    }
-->
<script>
        function loadSubReports()
        {
            for (var subReport in allSubReports) {
                var subReportName = ...;
                var subReportParam = ...; // subreport param defined by users
                subReport.update(subReportName, subReportParam);
            }            
        }
        loadSubReports();
</script>

//SubReport.php
public function setup()
{
    $params = $this->params; // $this->params would have the value of client-side subReportParam
    // use $params to build sql query and datastore
}

If you have any difficulty with this approach let us know. Tks,

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

None