KoolReport's Forum

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

Category Metric chart brokes with run() #2977

Closed GHSix opened this topic on on Feb 8, 2023 - 7 comments

GHSix commented on Feb 8, 2023

If I just get the query response at dataSource method, the donut chart will plot ok.

But, if then I add ->run() to it, the donut gets broken.

And, I would like to use the run() so I can add processes on it to filter out some values.

--

How the donut looks like when adding ->run() method in the dataSource response:

Looks like run() is getting things out of order or something like that.

Any ideas on how to fix it?

KoolReport commented on Feb 10, 2023

Let inspect the data different between "with run()" and "without run()" by adding the event onDataReady() to your category:

class MyCategory extends Category
{
    protected function onDataReady($result)
    {
        echo $result["data"]->toJson();exit;
    }
}

Let see what returned in two cases?

GHSix commented on Feb 13, 2023

Oh.

Looks like ->run() is making fields() method not run, so the data sent to chart is the full data with all fields no grouping or summing.

If this group and sum are getting ignored, the result is supposed to be weird indeed:

protected function fields()
    {
        return [
            $this->group(Text::create('vlr0'))->showTop(5)->andShowOthers(Lang::t('Others')),
            $this->sum(
                Number::create('qtd_avb')->suffix('')->decimals(0)->decimalPoint(Lang::t('.'))->thousandSeparator(Lang::t(','))
            )->showRawValue(true)->suffix('')->decimals(0)->decimalPoint(Lang::t('.'))->thousandSeparator(Lang::t(','))
        ];
    }

So, after ->run(), how do I call fields()?

KoolReport commented on Feb 14, 2023

Could you please post me the data from 2 cases, with and without run()

GHSix commented on Feb 14, 2023

Ok, here it is.

Category class:

class AcceptTopZerado extends Category
{
    protected function onDataReady($result)
    {
        echo $result['data']->toJson();exit;
    }

    protected function onInit()
    {
        $this
            ->title('Top '. Lang::t('Valueless'))
            ->type('success')
            ->lazyLoading(true);
    }
    
    protected function dataSource()
    {
        return $this->dashboard()->getDataG('queryAvbAceitacaoMes', $this->params)
            ->run(); // <-- With and without this one
    }

    protected function fields()
    {
        return [
            $this->group(Text::create('vlr0'))->showTop(5)->andShowOthers(Lang::t('Others')),
            $this->sum(
                Number::create('qtd_avb')->suffix('')->decimals(0)->decimalPoint(Lang::t('.'))->thousandSeparator(Lang::t(','))
            )->showRawValue(true)->suffix('')->decimals(0)->decimalPoint(Lang::t('.'))->thousandSeparator(Lang::t(','))
        ];
    }
}

Result without run() - chart plot ok:

[
  {
    "vlr0": "Doc Cancelado",
    "qtd_avb_sum": "1012"
  },
  {
    "vlr0": "Modalidade do Frete",
    "qtd_avb_sum": "76"
  },
  {
    "vlr0": "Tipo do Doc",
    "qtd_avb_sum": "1127"
  },
  {
    "vlr0": "Tipo do Serviço",
    "qtd_avb_sum": "805"
  },
  {
    "vlr0": "Transportador Incluído",
    "qtd_avb_sum": "948"
  },
  {
    "vlr0": "Valor Integral",
    "qtd_avb_sum": "94710"
  }
]

Result with run() - chart plot broken:

[
  {
    "qtd_seg": "2",
    "ramo": "654",
    "doc": "CT-e",
    "serie": "001",
    "subs": "0",
    "flags": "SNNNNNNNNC",
    "aceito": "Aceito",
    "vlr0": "Doc Cancelado",
    "qtd_avb": "7",
    "val_avb": "0.00",
    "val_benef_int": "0.00",
    "val_benef": "0.00",
    "val_despesas": "0.00",
    "qtd_falha": "0",
    "val_acima_lr": "0.00",
    "qtd_emb_acima_lr": "0",
    "dt": "2021-01-01 00:00:00",
    "ano": "2021",
    "qtd_avb_sum": 1012
  },
  {
    "qtd_seg": "1",
    "ramo": "621",
    "doc": "NF-e",
    "serie": "001",
    "subs": "0",
    "flags": "SNNNNNNNNF",
    "aceito": "Aceito",
    "vlr0": "Modalidade do Frete",
    "qtd_avb": "1",
    "val_avb": "0.00",
    "val_benef_int": "0.00",
    "val_benef": "0.00",
    "val_despesas": "0.00",
    "qtd_falha": "0",
    "val_acima_lr": "0.00",
    "qtd_emb_acima_lr": "0",
    "dt": "2021-01-01 00:00:00",
    "ano": "2021",
    "qtd_avb_sum": 76
  },
  {
    "qtd_seg": "1",
    "ramo": "654",
    "doc": "CT-e",
    "serie": "001",
    "subs": "0",
    "flags": "SNNNNNNNNT",
    "aceito": "Aceito",
    "vlr0": "Tipo do Doc",
    "qtd_avb": "3",
    "val_avb": "0.00",
    "val_benef_int": "0.00",
    "val_benef": "0.00",
    "val_despesas": "0.00",
    "qtd_falha": "0",
    "val_acima_lr": "0.00",
    "qtd_emb_acima_lr": "0",
    "dt": "2021-01-01 00:00:00",
    "ano": "2021",
    "qtd_avb_sum": 1127
  },
  {
    "qtd_seg": "1",
    "ramo": "654",
    "doc": "CT-e",
    "serie": "001",
    "subs": "0",
    "flags": "SNSNNNNNNS",
    "aceito": "Aceito",
    "vlr0": "Tipo do Serviço",
    "qtd_avb": "11",
    "val_avb": "0.00",
    "val_benef_int": "0.00",
    "val_benef": "0.00",
    "val_despesas": "0.00",
    "qtd_falha": "0",
    "val_acima_lr": "0.00",
    "qtd_emb_acima_lr": "0",
    "dt": "2021-01-01 00:00:00",
    "ano": "2021",
    "qtd_avb_sum": 805
  },
  {
    "qtd_seg": "1",
    "ramo": "621",
    "doc": "NF-e",
    "serie": "001",
    "subs": "0",
    "flags": "SNNNNNNNNI",
    "aceito": "Aceito",
    "vlr0": "Transportador Incluído",
    "qtd_avb": "1",
    "val_avb": "0.00",
    "val_benef_int": "0.00",
    "val_benef": "0.00",
    "val_despesas": "0.00",
    "qtd_falha": "0",
    "val_acima_lr": "0.00",
    "qtd_emb_acima_lr": "0",
    "dt": "2021-01-01 00:00:00",
    "ano": "2021",
    "qtd_avb_sum": 948
  },
  {
    "qtd_seg": "2",
    "ramo": "654",
    "doc": "CT-e",
    "serie": "001",
    "subs": "0",
    "flags": "SNNNNNNNNN",
    "aceito": "Aceito",
    "vlr0": "Valor Integral",
    "qtd_avb": "540",
    "val_avb": "93097842.92",
    "val_benef_int": "4087116.68",
    "val_benef": "0.00",
    "val_despesas": "0.00",
    "qtd_falha": "0",
    "val_acima_lr": "0.00",
    "qtd_emb_acima_lr": "0",
    "dt": "2021-01-01 00:00:00",
    "ano": "2021",
    "qtd_avb_sum": 94710
  }
]
KoolReport commented on Feb 15, 2023

The returned data in both cases are fine in term of data. In case of with run(), there are more redundant data only. And this redundancy caused DonutChart to confuse. So I have consulted dev.team for issue of DonutChart and I have found the fix, please apply this fix to the Category.view.php inside the theme that you used. If you use the AppStack then you look for "koolreport/appstack/dashboard/metrics/Category.view.php" and replace the DonutChart with following:

        <?php
            \koolreport\d3\DonutChart::create([
                "dataSource"=>$this->donutData(),
                "colorScheme"=>$this->colors(),
                "height"=>$this->master()->pieSize(),
                "width"=>$this->master()->pieSize(),
                "columns"=>[
                    $this->categoryField()->field()->colName(),
                    $this->measuredField()->colName(),
                ],
                "options"=>[
                    "legend"=>[
                        "show"=>false,
                    ],
                    "tooltip"=>[
                        "format"=>[
                            "title"=>"function(){
                                return '".$this->master()->title()."';
                            }",
                        ]
                    ],
                    "donut"=>[
                        "label"=>[
                            "show"=>false
                        ]
                    ]
                ]
            ]);
        ?>

The fix is that the "columns" part was added to tell DonutChart exactly which data to draw.

Let us know if it fixes the issue.

GHSix commented on Feb 15, 2023

Oh, I see. The fields() get executed but it did not removed the no needed columns from result.

Your fix did worked right. But, I also get the same result using the RemoveColumn / OnlyColumn processes:

protected function dataSource()
    {
        return $this->dashboard()->getDataG('queryAvbAceitacaoMes', $this->params)
            ->run()
            ->process(\koolreport\processes\OnlyColumn::process([
                'vlr0', 'qtd_avb'
            ]));
    }

I guess I will stick to the RemoveColumn / OnlyColumn process fix, until the oficial fix get into a new release, because, to me, to mess with the core code is a pain when I need to regenerate the vendor folder.

Thank you.

KoolReport commented on Feb 15, 2023

Awesome, your solution is brilliant!

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

Dashboard