Skip to content

Format numbers in waterfall hover #3711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/traces/waterfall/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -22,28 +23,35 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {

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;

if(!di.isSum) {
// format delta numbers:
if(size > 0) {
point.extraText = size + ' ' + DIRSYMBOL.increasing;
point.extraText = formatNumber(size) + ' ' + DIRSYMBOL.increasing;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Axes.hoverLabelText, it's made specifically for this purpose:

pointData2[vLetter + 'Label'] = (t.labels ? t.labels[attr] + ' ' : '') + Axes.hoverLabelText(vAxis, val);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @alexcjohnson.
Revised in 07b6c9c.

} else if(size < 0) {
point.extraText = '(' + (-size) + ') ' + DIRSYMBOL.decreasing;
point.extraText = '(' + (formatNumber(-size)) + ') ' + DIRSYMBOL.decreasing;
} else {
return;
}
// display initial value
point.extraText += '<br>Initial: ' + (di.b + di.s - size);
point.extraText += '<br>Initial: ' + formatNumber(di.b + di.s - size);
} else {
point[sizeLetter + 'LabelVal'] = size;
point[sizeLetter + 'LabelVal'] = formatNumber(size);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not quite sure where this would break down, but in principle *LabelVal should be a number, and *Label should be the string you want to use to display that number.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps non-default separators, or tickprefix/suffix, would break this as is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In bar traces we use a similar pattern:

pointData[sizeLetter + 'LabelVal'] = size;
.
Do you think there would be a problem there?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bar uses *LabelVal correctly, it's a number. I'm guessing that if there's no *Label then later on *LabelVal gets converted to a string using hoverLabelText but I don't know. If so, perhaps you can just leave this one as a number too.

Whew, the hover system is certainly ripe for refactoring... or at least documenting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whew, the hover system is certainly ripe for refactoring... or at least documenting.

🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bar uses *LabelVal correctly, it's a number. I'm guessing that if there's no *Label then later on *LabelVal gets converted to a string using hoverLabelText but I don't know. If so, perhaps you can just leave this one as a number too.

OK. Let's keep this as it is in this PR.

}

point.color = getTraceColor(trace, di);
Expand All @@ -56,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;
}
28 changes: 28 additions & 0 deletions test/jasmine/tests/waterfall_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['A', 'B', 'C', 'D', 'E'],
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: 'E'
});
})
.catch(failTest)
.then(done);
});
});
});

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