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

Is there a non-animation callback for when the chart completes? #3335

Closed
arxpoetica opened this issue Sep 19, 2016 · 7 comments
Closed

Is there a non-animation callback for when the chart completes? #3335

arxpoetica opened this issue Sep 19, 2016 · 7 comments

Comments

@arxpoetica
Copy link

I recognize that I can hook into various callbacks via a chart.js plugin, i.e., all the following callbacks are made available to a plugin: resize, beforeInit, beforeUpdate, afterScaleUpdate, beforeDatasetsUpdate, afterDatasetsUpdate, afterUpdate, beforeRender, afterInit, beforeDraw, beforeDatasetsDraw, afterDatasetsDraw, afterDraw, destroy...

...but...

What if one wants to hook into (for example) afterInit without going to the trouble of creating a plugin. In other words, it seems like many (or all?) of these callbacks ought to be available on any given chart instance.

Right now the only technique I know for checking if the chart has initialized is to hook into the animation progress callback and check for the first loop on the animation, which is complete hackery. So I built a plugin to hook in.

Is there a way to do this without a plugin?

If not can it be a feature request?

@etimberg
Copy link
Member

The recommended way to do this is to use a plugin. We can add something else if it is heavily requested

@arxpoetica
Copy link
Author

Here's another problem with doing it in a plugin. Let's say I have set up multiple instances of a chart, each one doing different things. Sure, I can create a plugin to hook into the afterInit callback. But that callback will do the same thing for each instance. What if I want a different behavior for different charts on afterInit? Do I create a separate plugin for each behavior? It would be easier to have afterInit available as an option, and set different callbacks on each instance. Imagine now that you have hundreds of chart instances...

I currently have this problem, and I've had to do some pretty hacky things to get different callback behaviors without creating multiple plugins...

@simonbrunel
Copy link
Member

simonbrunel commented Sep 20, 2016

I agree and I was thinking we could implement inline plugins, which might be a solution to your case:

new Chart(ctx, {
    options: {
        plugins: [{
            afterInit: function() {
            }
        }]
    }
});

It could also be refactored and used for multiple charts:

var pluginA = { afterInit: function() { } };
var pluginB = { afterRender: function() {} };

new Chart(ctx, {
    options: {
        plugins: [pluginA, pluginB]
    }
});

new Chart(ctx, {
    options: {
        plugins: [pluginA]
    }
});

To enable plugins for all charts:

Chart.defaults.global.plugins.push(pluginA, pluginB);

@arxpoetica
Copy link
Author

...I suppose that could work...?

@simonbrunel
Copy link
Member

@etimberg @zachpanz88 what do you guys think about inline plugins?

@etimberg
Copy link
Member

I like the idea. Should be relatively easy to implement too since we have a method already for dispatching a generic plugin call

@etimberg
Copy link
Member

etimberg commented Oct 5, 2016

Update on this: started with inline plugins in #3363 but we need to do some thinking on the exact API

@etimberg etimberg added this to the Version 2.5 milestone Oct 23, 2016
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

3 participants