emn178 / chartjs-plugin-labels

Plugin for Chart.js to display percentage, value or label in Pie or Doughnut.
MIT License
614 stars 219 forks source link

Whole numbers don't show precision / decimal place #97

Open flyarrowplane opened 5 years ago

flyarrowplane commented 5 years ago

Hello,

My client wants one decimal place of data showing in their pie chart. This works when the percentage is not a whole number. In a given example, there is the number 19% on one of the slices. I would expect that if precision is set to 1, that it would say 19.0% in order to be consistent with all of the other numbers, which do go to 1 decimal place. The client wants it this way and I'm not sure if there's anything I can do. Is this a bug? Could the ".0" be added to whole numbers?

Thank you.

eric-hoppenworth commented 5 years ago

I just happened to stumble upon this, but perhaps you could use a function in the 'render' option, rather than the default 'percentage'.

new Chart(ctx, {
  type: type,
  data: data,
  options: {
    plugins: {
      labels: {
        // render 'label', 'value', 'percentage', 'image' or custom function, default is 'percentage'
        render: function(options){
            var value = options.value; // something like a floating point number, possibly an integer
            return value.toFixed(1) + "%";  // this is probably enough
        },
        // other options...