Skip to content

Commit 5891651

Browse files
committed
revise pie format functions and tests
1 parent 938dabe commit 5891651

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

src/traces/pie/helpers.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010

1111
var Lib = require('../../lib');
1212

13+
function format(vRounded) {
14+
return (
15+
vRounded.indexOf('e') !== -1 ? vRounded.replace(/[.]?0+e/, 'e') :
16+
vRounded.indexOf('.') !== -1 ? vRounded.replace(/[.]?0+$/, '') :
17+
vRounded
18+
);
19+
}
20+
1321
exports.formatPiePercent = function formatPiePercent(v, separators) {
14-
var vRounded = (v * 100).toPrecision(3);
15-
if(vRounded.lastIndexOf('.') !== -1) {
16-
vRounded = vRounded.replace(/[.]?0+$/, '');
17-
}
22+
var vRounded = format((v * 100).toPrecision(3));
1823
return Lib.numSeparate(vRounded, separators) + '%';
1924
};
2025

2126
exports.formatPieValue = function formatPieValue(v, separators) {
22-
var vRounded = v.toPrecision(10);
23-
if(vRounded.indexOf('e+') !== -1) {
24-
vRounded = Math.round(v);
25-
} else if(vRounded.lastIndexOf('.') !== -1) {
26-
vRounded = vRounded.replace(/[.]?0+$/, '');
27-
}
27+
var vRounded = format(v.toPrecision(10));
2828
return Lib.numSeparate(vRounded, separators);
2929
};
3030

test/jasmine/tests/pie_test.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,7 +2076,7 @@ describe('pie value format', function() {
20762076

20772077
afterEach(destroyGraphDiv);
20782078

2079-
it('should handle rounding big & small numbers', function(done) {
2079+
it('should handle rounding big & small values', function(done) {
20802080
Plotly.newPlot(gd, [{
20812081
type: 'pie',
20822082
textinfo: 'value',
@@ -2099,12 +2099,14 @@ describe('pie value format', function() {
20992099
0.000123456789012,
21002100
0.0000123456789012,
21012101
0.00000123456789012,
2102+
0.000000123456789012,
2103+
0.0000000123456789012
21022104
]
21032105
}])
21042106
.then(function() {
21052107
var exp = [
2106-
'123,456,789,012',
2107-
'12,345,678,901',
2108+
'1.23456789e+11',
2109+
'1.23456789e+10',
21082110
'1,234,567,890',
21092111
'123,456,789',
21102112
'12,345,678.9',
@@ -2120,7 +2122,9 @@ describe('pie value format', function() {
21202122
'0.00123456789',
21212123
'0.000123456789',
21222124
'0.0000123456789',
2123-
'0.00000123456789'
2125+
'0.00000123456789',
2126+
'1.23456789e-7',
2127+
'1.23456789e-8'
21242128
];
21252129

21262130
var selection = d3.selectAll(SLICES_TEXT_SELECTOR);
@@ -2132,4 +2136,45 @@ describe('pie value format', function() {
21322136
.catch(failTest)
21332137
.then(done);
21342138
});
2139+
2140+
it('should handle rounding big & small percents', function(done) {
2141+
Plotly.newPlot(gd, [{
2142+
type: 'pie',
2143+
textinfo: 'percent',
2144+
values: [
2145+
0.9,
2146+
0.09,
2147+
0.009,
2148+
0.0009,
2149+
0.00009,
2150+
0.000009,
2151+
0.0000009,
2152+
0.00000009,
2153+
0.000000009,
2154+
0.0000000009
2155+
]
2156+
}])
2157+
.then(function() {
2158+
var exp = [
2159+
'90%',
2160+
'9%',
2161+
'0.9%',
2162+
'0.09%',
2163+
'0.009%',
2164+
'0.0009%',
2165+
'0.00009%',
2166+
'0.000009%',
2167+
'9e-7%',
2168+
'9e-8%'
2169+
];
2170+
2171+
var selection = d3.selectAll(SLICES_TEXT_SELECTOR);
2172+
for(var i = 0; i < selection[0].length; i++) {
2173+
var text = selection[0][i].innerHTML;
2174+
expect(text).toBe(exp[i]);
2175+
}
2176+
})
2177+
.catch(failTest)
2178+
.then(done);
2179+
});
21352180
});

0 commit comments

Comments
 (0)