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

Document method of truncating decimals #4992

Closed
emilhem opened this issue Nov 24, 2017 · 3 comments
Closed

Document method of truncating decimals #4992

emilhem opened this issue Nov 24, 2017 · 3 comments

Comments

@emilhem
Copy link

emilhem commented Nov 24, 2017

Expected Behavior

An option to truncate decimals.

Current Behavior

Currently many decimals are being displayed when hovering over a datapoint. I have no problem of them being used in calculating positions but for display purposes it would be nice to be able to truncate some decimals.
chart_js_significant_digits

Possible Solution

Implement an option to allow truncation of decimals.

Context

Many decimals are not always useful when showing a graph to another person.

Environment

  • Chart.js version: 2.7.1
  • Browser name and version: Firefox 52.4.0 ESR
@jcopperfield
Copy link
Contributor

You could add a personalized callback to the tooltip labels, where you round the values
e.g.

  1. tooltipItem.yLabel.toFixed(2) would return a value with 2 decimal places.
2.123.toFixed(2)
>> "2.12"
2.0001.toFixed(2)
>> "2.00"
  1. Math.round(tooltipItem.yLabel * 100) / 100 would return a value rounded to the nearest 2nd decimal place.
Math.round(2.123 * 100) / 100
>> 2.12
Math.round(2.00001 * 100) / 100
>> 2
tooltips: {
    callbacks: {
        label: function(tooltipItem, data) {
            var label = data.datasets[tooltipItem.datasetIndex].label || '';

            if (label) {
                label += ': ';
            }
            label += tooltipItem.yLabel.toFixed(2);
            return label;
        }
    }
}

@etimberg
Copy link
Member

@jcopperfield has the recommend solution. I think this is a good opportunity to update our documentation with this example to help future users.

@D0nVitalio
Copy link

For version 3.8, I needed 5 decimals:

options: { 
                title: { ... },
                plugins: { ... },
                tooltip: {
                    callbacks: {
                        label: function(context) {
                            let label = context.dataset.label || '';
                            if (label) {
                                label += ': ';
                            }
                            if (context.parsed.y !== null) {
                                label += Math.round( context.parsed.y * 100000) / 100000;
                            }
                            return label;
                        }
                    }
                }
            },

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

No branches or pull requests

5 participants