Skip to content

Commit 938dabe

Browse files
committed
fix issue 5122 - revise rounding big numbers
1 parent 94b31a8 commit 938dabe

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/traces/pie/helpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ exports.formatPiePercent = function formatPiePercent(v, separators) {
2020

2121
exports.formatPieValue = function formatPieValue(v, separators) {
2222
var vRounded = v.toPrecision(10);
23-
if(vRounded.lastIndexOf('.') !== -1) {
23+
if(vRounded.indexOf('e+') !== -1) {
24+
vRounded = Math.round(v);
25+
} else if(vRounded.lastIndexOf('.') !== -1) {
2426
vRounded = vRounded.replace(/[.]?0+$/, '');
2527
}
2628
return Lib.numSeparate(vRounded, separators);

test/jasmine/tests/pie_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,3 +2064,72 @@ describe('pie uniformtext', function() {
20642064
.then(done);
20652065
});
20662066
});
2067+
2068+
describe('pie value format', function() {
2069+
'use strict';
2070+
2071+
var gd;
2072+
2073+
beforeEach(function() {
2074+
gd = createGraphDiv();
2075+
});
2076+
2077+
afterEach(destroyGraphDiv);
2078+
2079+
it('should handle rounding big & small numbers', function(done) {
2080+
Plotly.newPlot(gd, [{
2081+
type: 'pie',
2082+
textinfo: 'value',
2083+
values: [
2084+
123456789012,
2085+
12345678901.2,
2086+
1234567890.12,
2087+
123456789.012,
2088+
12345678.9012,
2089+
1234567.89012,
2090+
123456.789012,
2091+
12345.6789012,
2092+
1234.56789012,
2093+
123.456789012,
2094+
12.3456789012,
2095+
1.23456789012,
2096+
0.123456789012,
2097+
0.0123456789012,
2098+
0.00123456789012,
2099+
0.000123456789012,
2100+
0.0000123456789012,
2101+
0.00000123456789012,
2102+
]
2103+
}])
2104+
.then(function() {
2105+
var exp = [
2106+
'123,456,789,012',
2107+
'12,345,678,901',
2108+
'1,234,567,890',
2109+
'123,456,789',
2110+
'12,345,678.9',
2111+
'1,234,567.89',
2112+
'123,456.789',
2113+
'12,345.6789',
2114+
'1,234.56789',
2115+
'123.456789',
2116+
'12.3456789',
2117+
'1.23456789',
2118+
'0.123456789',
2119+
'0.0123456789',
2120+
'0.00123456789',
2121+
'0.000123456789',
2122+
'0.0000123456789',
2123+
'0.00000123456789'
2124+
];
2125+
2126+
var selection = d3.selectAll(SLICES_TEXT_SELECTOR);
2127+
for(var i = 0; i < selection[0].length; i++) {
2128+
var text = selection[0][i].getAttribute('data-unformatted');
2129+
expect(text).toBe(exp[i]);
2130+
}
2131+
})
2132+
.catch(failTest)
2133+
.then(done);
2134+
});
2135+
});

0 commit comments

Comments
 (0)