Skip to content

Fix rounding big numbers in pie & sunburst traces #5152

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

Merged
merged 2 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/traces/pie/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ exports.formatPiePercent = function formatPiePercent(v, separators) {

exports.formatPieValue = function formatPieValue(v, separators) {
var vRounded = v.toPrecision(10);
if(vRounded.lastIndexOf('.') !== -1) {
if(vRounded.indexOf('e+') !== -1) {
vRounded = Math.round(v);
} else if(vRounded.lastIndexOf('.') !== -1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also e- if the number is <1e-6

Can we do something like:

if(vRounded.indexOf('e') !== -1) {
    vRounded = vRounded.replace(/[.]?0+e/, 'e');
} else if(vRounded.indexOf('.') !== -1) { // why did we have lastIndexOf here? indexOf should be the same, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Revised in 5891651.

vRounded = vRounded.replace(/[.]?0+$/, '');
}
return Lib.numSeparate(vRounded, separators);
Expand Down
69 changes: 69 additions & 0 deletions test/jasmine/tests/pie_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2064,3 +2064,72 @@ describe('pie uniformtext', function() {
.then(done);
});
});

describe('pie value format', function() {
'use strict';

var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(destroyGraphDiv);

it('should handle rounding big & small numbers', function(done) {
Plotly.newPlot(gd, [{
type: 'pie',
textinfo: 'value',
values: [
123456789012,
12345678901.2,
1234567890.12,
123456789.012,
12345678.9012,
1234567.89012,
123456.789012,
12345.6789012,
1234.56789012,
123.456789012,
12.3456789012,
1.23456789012,
0.123456789012,
0.0123456789012,
0.00123456789012,
0.000123456789012,
0.0000123456789012,
0.00000123456789012,
]
}])
.then(function() {
var exp = [
'123,456,789,012',
'12,345,678,901',
'1,234,567,890',
'123,456,789',
'12,345,678.9',
'1,234,567.89',
'123,456.789',
'12,345.6789',
'1,234.56789',
'123.456789',
'12.3456789',
'1.23456789',
'0.123456789',
'0.0123456789',
'0.00123456789',
'0.000123456789',
'0.0000123456789',
'0.00000123456789'
];

var selection = d3.selectAll(SLICES_TEXT_SELECTOR);
for(var i = 0; i < selection[0].length; i++) {
var text = selection[0][i].getAttribute('data-unformatted');
expect(text).toBe(exp[i]);
}
})
.catch(failTest)
.then(done);
});
});