ciprianciurea / chartjs-plugin-doughnutlabel

Chart.js plugin for doughnut chart to display text in the center
https://ciprianciurea.github.io/chartjs-plugin-doughnutlabel/samples/index.html
MIT License
65 stars 24 forks source link

Add support for dynamic text #5

Closed jonathancounihan closed 5 years ago

jonathancounihan commented 5 years ago

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

jonathancounihan commented 5 years ago

Pull request #6 fixes this issue.

ciprianciurea commented 5 years ago

Fixes #6 , #5

jonathancounihan commented 5 years ago

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 commented 5 years ago

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

JohnArcher commented 5 years ago

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 commented 5 years ago

You'll find my PR here: #11

JohnArcher commented 5 years ago

As I had several trouble using the internal _meta property you should not use this. As stated in https://github.com/chartjs/Chart.js/issues/4731#issuecomment-328279321 you should use getDatasetMeta () instead.

vasyl-shumskyi commented 4 years ago

chart.getDatasetMeta(0).total works nicely

Eduardops4 commented 2 years ago

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

Olá boa noite