filament icon indicating copy to clipboard operation
filament copied to clipboard

Allow javascript callback for chart options

Open margarizaldi opened this issue 3 years ago • 1 comments

The idea is by wrapping javascript callback inside !! then we remove the rendered "!! and !!" in the frontend.

For example:

public static ?array $options = [
    'scales' => [
        'x' => [
            'ticks' => [
                'callback' => "!!
                    function(value, index, ticks) {
                        return '$' + value;
                    }
               !!"
            ],
        ],
    ],
];

I know the code is ugly but if someone can make better PR please do it. Chart.js brings a lot of functionalities through callbacks.

Anyone can improve if !! or create better PR if this is not acceptable.

margarizaldi avatar Aug 10 '22 14:08 margarizaldi

Well I don't expect too much that this PR will be accepted. At least the idea must go on.

margarizaldi avatar Aug 10 '22 17:08 margarizaldi

Yeah I'm not going to merge this solution, but I can suggest another that you can implement.

Allow the getOptions() method to return a string instead, and then check if it's a string or an array before passing it to json_encode(). That way the user can provide their own pre-encoded JSON with callbacks and everything, that gets passed directly to the view.

danharrin avatar Aug 11 '22 07:08 danharrin