The Wayback Machine - https://web.archive.org/web/20200606063052/https://github.com/chartjs/Chart.js/issues/5104
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

[BUG] Can't create custom callback on legend click #5104

Open
giovanniciriello opened this issue Jan 3, 2018 · 3 comments
Open

[BUG] Can't create custom callback on legend click #5104

giovanniciriello opened this issue Jan 3, 2018 · 3 comments

Comments

@giovanniciriello
Copy link

@giovanniciriello giovanniciriello commented Jan 3, 2018

Expected Behavior

I wish to set a callback function on legend click that hide/show the dataset (currently the default action) and that do something more.

Current Behavior

I've tried same codes e.g:

    var default = Chart.defaults.global.legend.onClick;
    Chart.defaults.global.legend.onClick = function(e, legendItem) {
        console.log("hello");
        default.call(this, e, legendItem);
    };

but this code does't override the callback.

Environment

  • Chart.js version: 2.6.0
  • Browser name and version: Chrome
@simonbrunel
Copy link
Member

@simonbrunel simonbrunel commented Jan 3, 2018

@giovanniciriello can you build a fiddle that reproduces that issue?

@jcopperfield
Copy link
Contributor

@jcopperfield jcopperfield commented Jan 5, 2018

@giovanniciriello I think you are using a doughnut or polarArea chart. These unfortunately use namespaced onClick callbacks.

Chart.defaults.global.legend.onClick = onClickCallback;
Chart.defaults.doughnut.legend.onClick = onClickCallback;
Chart.defaults.polarArea.legend.onClick = onClickCallback;
@giovanniciriello
Copy link
Author

@giovanniciriello giovanniciriello commented Jan 5, 2018

@jcopperfield I'm using a doughnut chart, so the second line worked for me!
The official chart.js documentation is not clear about that.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.