Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebec807

Browse files
committed
fix3710 - format waterfall values and sums in hover
1 parent c657348 commit ebec807

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/traces/waterfall/hover.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ var DIRSYMBOL = {
1616
decreasing: '▼'
1717
};
1818

19+
function formatNumber(a) {
20+
return parseFloat(a.toPrecision(10));
21+
}
22+
1923
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
2024
var point = hoverOnBars(pointData, xval, yval, hovermode);
2125
if(!point) return;
@@ -34,16 +38,16 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
3438
if(!di.isSum) {
3539
// format delta numbers:
3640
if(size > 0) {
37-
point.extraText = size + ' ' + DIRSYMBOL.increasing;
41+
point.extraText = formatNumber(size) + ' ' + DIRSYMBOL.increasing;
3842
} else if(size < 0) {
39-
point.extraText = '(' + (-size) + ') ' + DIRSYMBOL.decreasing;
43+
point.extraText = '(' + (formatNumber(-size)) + ') ' + DIRSYMBOL.decreasing;
4044
} else {
4145
return;
4246
}
4347
// display initial value
44-
point.extraText += '<br>Initial: ' + (di.b + di.s - size);
48+
point.extraText += '<br>Initial: ' + formatNumber(di.b + di.s - size);
4549
} else {
46-
point[sizeLetter + 'LabelVal'] = size;
50+
point[sizeLetter + 'LabelVal'] = formatNumber(size);
4751
}
4852

4953
point.color = getTraceColor(trace, di);

test/jasmine/tests/waterfall_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,34 @@ describe('waterfall hover', function() {
12891289
.catch(failTest)
12901290
.then(done);
12911291
});
1292+
1293+
describe('round hover precision', function() {
1294+
it('should format numbers', function(done) {
1295+
gd = createGraphDiv();
1296+
1297+
Plotly.plot(gd, {
1298+
data: [{
1299+
x: [1, 2, 3, 4, 5],
1300+
y: [0, -1.1, 2.2, -3.3, 4.4],
1301+
type: 'waterfall'
1302+
}],
1303+
layout: {width: 400, height: 400}
1304+
})
1305+
.then(function() {
1306+
var evt = { xpx: 200, ypx: 350 };
1307+
Fx.hover('graph', evt, 'xy');
1308+
})
1309+
.then(function() {
1310+
assertHoverLabelContent({
1311+
nums: '2.2\n4.4 ▲\nInitial: -2.2',
1312+
name: '',
1313+
axis: '5'
1314+
});
1315+
})
1316+
.catch(failTest)
1317+
.then(done);
1318+
});
1319+
});
12921320
});
12931321

12941322
describe('with special width/offset combinations', function() {

0 commit comments

Comments
 (0)