Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Values on Tooltips? #3

Closed
gegere opened this issue Feb 8, 2018 · 2 comments
Closed

Format Values on Tooltips? #3

gegere opened this issue Feb 8, 2018 · 2 comments
Milestone

Comments

@gegere
Copy link

gegere commented Feb 8, 2018

Hello, great work on this PHP class to help get others up and running faster. Looking over the class and would like to pass in a callback.

Any ideas on how this could be incorporated?
jtblin/angular-chart.js#33

@Ejdamm
Copy link
Owner

Ejdamm commented Feb 9, 2018

Hi,
This was a tough one! I found two possible solutions but honestly I don't know much of what I am doing since I don't know javascript very well.

The suggested method for callbakcs is like this

options: {
    tooltips: {
        callbacks: {
            label: function(tooltipItem, data){
                return "example";
            }
        }
    }
}

The corresponding php version would be

$options = array(
    'tooltips' => array(
        'callbacks' => array(
            'label' => "function(tooltipItem, data){return 'Hello world';}"
        )
    )
);

That will however be parsed as a string and not a function by json_encode(). So I would have to write a function that searches for the 'function' keyword and remove the quotes from the json output.

The second solution is to ignore that this is a PHP wrapper and put the callback function somewhere above loadChartJsPhp() function like this:

<script>
    Chart.defaults.global.tooltips.callbacks.label = function(tooltipItem, data) {
        return "hello world";
    };

    (function () {
        loadChartJsPhp();
    })();
</script>

@Ejdamm Ejdamm added this to the 2.0 milestone Feb 27, 2018
Ejdamm added a commit that referenced this issue Mar 4, 2018
@Ejdamm
Copy link
Owner

Ejdamm commented Mar 4, 2018

It should work now if you get the quotation marks right (not as how I suggested before)

$options = array(
    'responsive' => false,
    'tooltips' => array(
        'callbacks' => array(
            'label' => 'function(tooltipItem, data){return "Hello world";}'
        )
    )
);

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

No branches or pull requests

2 participants