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

Add support for dynamic text #5

Closed
jonathancounihan opened this issue Nov 12, 2018 · 9 comments
Closed

Add support for dynamic text #5

jonathancounihan opened this issue Nov 12, 2018 · 9 comments

Comments

@jonathancounihan
Copy link
Contributor

Add the ability for the label text to be created via a function, to support dynamically changing text.

jonathancounihan referenced this issue in jonathancounihan/chartjs-plugin-doughnutlabel Nov 12, 2018
@jonathancounihan
Copy link
Contributor Author

Pull request #6 fixes this issue.

ciprianciurea referenced this issue Nov 12, 2018
Added a check if the label.text property is a function, then call the function and return the value to be used for the text.
@ciprianciurea
Copy link
Owner

Fixes #6 , #5

@jonathancounihan
Copy link
Contributor Author

sure @ciprianciurea, will do so. The basic one I can think of is when the legend is displayed and you toggle a segments visibility as shown here:

chartjsdoughnutlabels

The relevant code, using the changes I made:

text: function() {
  var total = cht.config.data.datasets[0]._meta[0].total;
  return total;
},

@ciprianciurea
Copy link
Owner

Thanks @jonathancounihan , managed to update the samples in the meantime.

@Dave3of5 Dave3of5 mentioned this issue Mar 21, 2019
@JohnArcher
Copy link
Contributor

The code @jonathancounihan posted does not work here (chart.js 2.8.0). _meta[0] is not defined. I had to use this: chart.data.datasets[0]._meta[Object.keys(theChart.data.datasets[0]._meta)[0]].total

My other problem with your example is that I have no access to cht in this scope in my project. So I would like to do a PR which passes the chart object to the text() function (similar to other Chart.js plugins).

@JohnArcher
Copy link
Contributor

JohnArcher commented Apr 15, 2019

You'll find my PR here: #11

@JohnArcher
Copy link
Contributor

As I had several trouble using the internal _meta property you should not use this. As stated in chartjs/Chart.js#4731 (comment) you should use getDatasetMeta () instead.

@vasyl-shumskyi
Copy link

vasyl-shumskyi commented Apr 29, 2020

chart.getDatasetMeta(0).total works nicely

@Eduardops4
Copy link

Add the ability for the label text to be created via a function, to support dynamically changing text.

Olá boa noite

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

No branches or pull requests

5 participants