KoolReport's Forum

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

Dashboard fields vs Koolreport columns #2795

Open GHSix opened this topic on on Aug 16 - 10 comments

GHSix commented on Aug 16

What is the equivalent of Dashboard protected function fields(){...} formating content in Koolreport ->columns([...])?

Currency::create('val')
                ->label('Value')
                ->BRL()
                ->symbol()
                ->decimals(2)

In other words, how to use Currency class inside columns to format values, if possible?

KoolReport commented on Aug 16

Could you please further explain? I have not fully understood.

GHSix commented on Aug 16

On dashboard, if I want formatted fields, for example, date and money, I'll do like:

...
protected function fields()
    {
        return [
            DateTime::create('dt')->displayFormat('m/y'),
            Currency::create('val')
                ->label('Value')
                ->BRL()
                ->symbol()
                ->decimals(2)
        ];
    }

On pure KR style inside dashboard, how do I do the same formatting? I have tried this, but can't get it to work:

...
                   ->dataSource(...->run())
                    ->columns([
                        'dt' => ['type' => 'string'],
                        'val' => ['type' => 'number', 'formatValue'=>function($value, $row){
                            return Currency::create($value)
                                ->label('Value')
                                ->BRL()
                                ->symbol()
                                ->decimals(2);
                        }]
                    ])

Is it possible to pass DataTime and Currency classes to formatValue or column to get the same formatting as in fields function?

The reason I'm at it now is because I was tring hard to not mix pure KR into dashboard but I guess I'm givin it up.

KoolReport commented on Aug 16

I see, you do:

->columns([
    'dt' => ['type' => 'string'],
    'val' => ['type' => 'number',"decimals"=>2, "prefix"=>"BRL","label"=>"Value"]
])

Beside prefix the column option has suffix as well.

GHSix commented on Aug 16

It will not render the same result. It will render R$1,234,567.00 and Currency will render R$1.234.567,00 (different pontuation format).

I guess I can use formatValue and money_format to get what I want, I was just thinking that if it was possible to directly use dashboard formatting classes it would be easyer.

KoolReport commented on Aug 16

I get it, you do this:

'val' => [
        "label"=>"Value",
        'type' => 'number',
        "decimals"=>2, 
        "prefix"=>'R$',
        "thousandSeparator"=>".",
        "decimalPoint"=>",",
]
GHSix commented on Aug 16

I see.

There are docs for this? I will need to format dates as well.

GHSix commented on Aug 16

I did like:

->columns([
        'dt' => [
                'formatValue' => function($val) {
                        return (new \DateTime($val))->format('m/Y');
                }
        ],
        'val' => [...]
])

It did work for Google Charts but, for D3 it will keep showing dates as "31/12/2022 00:00:00". Why?

KoolReport commented on Aug 17

You try this:

        'dt' => [
            "type"=>"datetime",
            "format"=>"Y-m-d H:i:s",
            "displayFormat"=>"m/Y",
        ],

Let me know if it works.

GHSix commented on Aug 17

No.

But it changed the display from "2022-12-31 00:00:00" to "2022/31/12" and just "31/12" for the 2nd result and on when it's more than 4 results.

GHSix commented 7 days ago

Any news on the right way to format dates on D3 charts?

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