Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Applying callback functions #32

Open
adummy832 opened this issue Oct 17, 2017 · 7 comments
Open

Applying callback functions #32

adummy832 opened this issue Oct 17, 2017 · 7 comments
Labels

Comments

@adummy832
Copy link

I have this bar chart, just a standard one and I want to modify how yAxes labels are showing without decimals, however I have this weird thing going on. Here's my config options. Can you explain why is this happening & what is the possible workaround for this? Thank you.

'clientOptions' => [
                    'scales' => [
                        'yAxes' => [[
                            'ticks' => [
                                'beginAtZero' => 'true', # Works
                                'callback' => function($label, $index, $labels) { // Breaks.
                                    if (floor($label) === $label) return $label;
                                }
                                # Console : TypeError: ({}) is not a function[Learn More] ???
                                # Errors  : TypeError: element._view is undefined ???
                            ]
                        ]],
                    ],
                ], 
@adummy832
Copy link
Author

I just found out the answer myself . For those who are struggling with this. import JsExpression. :-)

use \yii\web\JsExpression;

'prop' => new JsExpression(' // your JS code here. ')

@tonydspaniard
Copy link
Member

@adummy832 sorry for the delay. I was about to write that and you were faster :)

The JsExpression was built for that purpose and should be remembered not only for this widget but for all that allow the writing of a js callback function (ie ActiveForm)

Thank you for your time @adummy832 very kind of you to expose your findings.

@adummy832
Copy link
Author

Still thanks for the reply the dude, you are awesome! Been stuck on this for hours XD. Are there still some hidden 'Class' treasure in Yii like this one? If so, could you recommend that so that I can check that out? BTW, thanks for this awesome plug-in dude. Godspeed! :-)

@tonydspaniard
Copy link
Member

Yii is full of goodies... I highly recommend you to read its base classes code (don't forget its classes).

I always do that with every framework I work with. Best way to get the hidden wonders of other people's brains.

@tonydspaniard
Copy link
Member

Yii is full of goodies... I highly recommend you to read its base classes code (don't forget its HELPER classes)

@andrerahardjo97
Copy link

andrerahardjo97 commented Feb 25, 2020

How you use it?
I want to do the same thing.
But I don't know how where I should place the JsExpression.
The code I want to implement looks like this:

options: {
    scales: {
        yAxes: [{
            ticks: {
                callback: function (value) {
                    return numeral(value).format('$ 0,0')
                }
            }
        }]
    }
}

And this is my code in PHP:

echo ChartJs::widget(
    [
        'type' => 'bar',
        'options' => [
            'height' => 600,
            'width' => 1400,
            'scales' => [
                'yAxes' => new JsExpression(
                    "
                    [{
                        ticks: {
                            callback: function (value) {
                                return numeral(value).format('$ 0,0')
                            }
                        }
                    }]
                    "
                )
            ],
        ],
        'data' => [
            'labels' => $arrayChart['label'],
            'datasets' => [
                [
                    'label' => 'Total Pendapatan',
                    'backgroundColor' => '#38a9ff',
                    'data' => $arrayChart['isi'],
                ],
            ],
        ],
    ]
);

It doesn't work, the number is still not formatted, and I don't know the right way to do it.
Sorry if my english is bad.

edit:
It's printed like this on the HTML, and I don't know what's wrong. (I beautified the code)

<canvas id="w0"
    width="1222"
    height="523"
    scales="{"yAxes":
                    [{
                        ticks: {
                            callback: function (value) {
                                return numeral(value).format(" $="" 0,0')="" }="" }]="" }'=""
    style="display: block; width: 1222px; height: 523px;"
    class="chartjs-render-monitor"></canvas>

@cristian-dev-it
Copy link

I just found out the answer myself . For those who are struggling with this. import JsExpression. :-)

use \yii\web\JsExpression;

'prop' => new JsExpression(' // your JS code here. ')

This works for me! I was trying to change the tooltips content

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants