Skip to content

Commit e8b9009

Browse files
authored
Merge pull request #3711 from plotly/fix3710-format-numbers-waterfall-hover
Format numbers in waterfall hover
2 parents 32738b5 + 07b6c9c commit e8b9009

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/traces/waterfall/hover.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
'use strict';
1010

11-
var Color = require('../../components/color');
11+
var hoverLabelText = require('../../plots/cartesian/axes').hoverLabelText;
12+
var opacity = require('../../components/color').opacity;
1213
var hoverOnBars = require('../bar/hover').hoverOnBars;
1314

1415
var DIRSYMBOL = {
@@ -22,28 +23,35 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
2223

2324
var cd = point.cd;
2425
var trace = cd[0].trace;
26+
var isHorizontal = (trace.orientation === 'h');
27+
28+
var vAxis = isHorizontal ? pointData.xa : pointData.ya;
29+
30+
function formatNumber(a) {
31+
return hoverLabelText(vAxis, a);
32+
}
2533

2634
// the closest data point
2735
var index = point.index;
2836
var di = cd[index];
2937

30-
var sizeLetter = (trace.orientation === 'h') ? 'x' : 'y';
38+
var sizeLetter = isHorizontal ? 'x' : 'y';
3139

3240
var size = (di.isSum) ? di.b + di.s : di.rawS;
3341

3442
if(!di.isSum) {
3543
// format delta numbers:
3644
if(size > 0) {
37-
point.extraText = size + ' ' + DIRSYMBOL.increasing;
45+
point.extraText = formatNumber(size) + ' ' + DIRSYMBOL.increasing;
3846
} else if(size < 0) {
39-
point.extraText = '(' + (-size) + ') ' + DIRSYMBOL.decreasing;
47+
point.extraText = '(' + (formatNumber(-size)) + ') ' + DIRSYMBOL.decreasing;
4048
} else {
4149
return;
4250
}
4351
// display initial value
44-
point.extraText += '<br>Initial: ' + (di.b + di.s - size);
52+
point.extraText += '<br>Initial: ' + formatNumber(di.b + di.s - size);
4553
} else {
46-
point[sizeLetter + 'LabelVal'] = size;
54+
point[sizeLetter + 'LabelVal'] = formatNumber(size);
4755
}
4856

4957
point.color = getTraceColor(trace, di);
@@ -56,6 +64,6 @@ function getTraceColor(trace, di) {
5664
var mc = cont.color;
5765
var mlc = cont.line.color;
5866
var mlw = cont.line.width;
59-
if(Color.opacity(mc)) return mc;
60-
else if(Color.opacity(mlc) && mlw) return mlc;
67+
if(opacity(mc)) return mc;
68+
else if(opacity(mlc) && mlw) return mlc;
6169
}

test/jasmine/tests/waterfall_test.js

+28
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: ['A', 'B', 'C', 'D', 'E'],
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: 'E'
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)