chartjs-plugin-labels icon indicating copy to clipboard operation
chartjs-plugin-labels copied to clipboard

Whole numbers don't show precision / decimal place

Open flyarrowplane opened this issue 6 years ago • 1 comments

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.

flyarrowplane avatar Jun 13 '19 17:06 flyarrowplane

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...

eric-hoppenworth avatar Jul 17 '19 13:07 eric-hoppenworth