From ebec807e97c2ba5b9874440f88e2d8dfa3a09c5d Mon Sep 17 00:00:00 2001 From: archmoj Date: Mon, 1 Apr 2019 22:04:53 -0400 Subject: [PATCH 1/2] fix3710 - format waterfall values and sums in hover --- src/traces/waterfall/hover.js | 12 ++++++++---- test/jasmine/tests/waterfall_test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/traces/waterfall/hover.js b/src/traces/waterfall/hover.js index 923668dabcf..dd83214cdc7 100644 --- a/src/traces/waterfall/hover.js +++ b/src/traces/waterfall/hover.js @@ -16,6 +16,10 @@ var DIRSYMBOL = { decreasing: '▼' }; +function formatNumber(a) { + return parseFloat(a.toPrecision(10)); +} + module.exports = function hoverPoints(pointData, xval, yval, hovermode) { var point = hoverOnBars(pointData, xval, yval, hovermode); if(!point) return; @@ -34,16 +38,16 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { if(!di.isSum) { // format delta numbers: if(size > 0) { - point.extraText = size + ' ' + DIRSYMBOL.increasing; + point.extraText = formatNumber(size) + ' ' + DIRSYMBOL.increasing; } else if(size < 0) { - point.extraText = '(' + (-size) + ') ' + DIRSYMBOL.decreasing; + point.extraText = '(' + (formatNumber(-size)) + ') ' + DIRSYMBOL.decreasing; } else { return; } // display initial value - point.extraText += '
Initial: ' + (di.b + di.s - size); + point.extraText += '
Initial: ' + formatNumber(di.b + di.s - size); } else { - point[sizeLetter + 'LabelVal'] = size; + point[sizeLetter + 'LabelVal'] = formatNumber(size); } point.color = getTraceColor(trace, di); diff --git a/test/jasmine/tests/waterfall_test.js b/test/jasmine/tests/waterfall_test.js index 10fdf6a6b4d..7508bc73e10 100644 --- a/test/jasmine/tests/waterfall_test.js +++ b/test/jasmine/tests/waterfall_test.js @@ -1289,6 +1289,34 @@ describe('waterfall hover', function() { .catch(failTest) .then(done); }); + + describe('round hover precision', function() { + it('should format numbers', function(done) { + gd = createGraphDiv(); + + Plotly.plot(gd, { + data: [{ + x: [1, 2, 3, 4, 5], + y: [0, -1.1, 2.2, -3.3, 4.4], + type: 'waterfall' + }], + layout: {width: 400, height: 400} + }) + .then(function() { + var evt = { xpx: 200, ypx: 350 }; + Fx.hover('graph', evt, 'xy'); + }) + .then(function() { + assertHoverLabelContent({ + nums: '2.2\n4.4 ▲\nInitial: -2.2', + name: '', + axis: '5' + }); + }) + .catch(failTest) + .then(done); + }); + }); }); describe('with special width/offset combinations', function() { From 07b6c9c296b2037ebda7d12619e31354fc263296 Mon Sep 17 00:00:00 2001 From: archmoj Date: Tue, 2 Apr 2019 10:21:18 -0400 Subject: [PATCH 2/2] apply hoverLabelText in waterfall hover --- src/traces/waterfall/hover.js | 20 ++++++++++++-------- test/jasmine/tests/waterfall_test.js | 6 +++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/traces/waterfall/hover.js b/src/traces/waterfall/hover.js index dd83214cdc7..f3bbe18ce83 100644 --- a/src/traces/waterfall/hover.js +++ b/src/traces/waterfall/hover.js @@ -8,7 +8,8 @@ 'use strict'; -var Color = require('../../components/color'); +var hoverLabelText = require('../../plots/cartesian/axes').hoverLabelText; +var opacity = require('../../components/color').opacity; var hoverOnBars = require('../bar/hover').hoverOnBars; var DIRSYMBOL = { @@ -16,22 +17,25 @@ var DIRSYMBOL = { decreasing: '▼' }; -function formatNumber(a) { - return parseFloat(a.toPrecision(10)); -} - module.exports = function hoverPoints(pointData, xval, yval, hovermode) { var point = hoverOnBars(pointData, xval, yval, hovermode); if(!point) return; var cd = point.cd; var trace = cd[0].trace; + var isHorizontal = (trace.orientation === 'h'); + + var vAxis = isHorizontal ? pointData.xa : pointData.ya; + + function formatNumber(a) { + return hoverLabelText(vAxis, a); + } // the closest data point var index = point.index; var di = cd[index]; - var sizeLetter = (trace.orientation === 'h') ? 'x' : 'y'; + var sizeLetter = isHorizontal ? 'x' : 'y'; var size = (di.isSum) ? di.b + di.s : di.rawS; @@ -60,6 +64,6 @@ function getTraceColor(trace, di) { var mc = cont.color; var mlc = cont.line.color; var mlw = cont.line.width; - if(Color.opacity(mc)) return mc; - else if(Color.opacity(mlc) && mlw) return mlc; + if(opacity(mc)) return mc; + else if(opacity(mlc) && mlw) return mlc; } diff --git a/test/jasmine/tests/waterfall_test.js b/test/jasmine/tests/waterfall_test.js index 7508bc73e10..abce45ce1cd 100644 --- a/test/jasmine/tests/waterfall_test.js +++ b/test/jasmine/tests/waterfall_test.js @@ -1296,7 +1296,7 @@ describe('waterfall hover', function() { Plotly.plot(gd, { data: [{ - x: [1, 2, 3, 4, 5], + x: ['A', 'B', 'C', 'D', 'E'], y: [0, -1.1, 2.2, -3.3, 4.4], type: 'waterfall' }], @@ -1308,9 +1308,9 @@ describe('waterfall hover', function() { }) .then(function() { assertHoverLabelContent({ - nums: '2.2\n4.4 ▲\nInitial: -2.2', + nums: '2.2\n4.4 ▲\nInitial: −2.2', name: '', - axis: '5' + axis: 'E' }); }) .catch(failTest)