Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions src/traces/waterfall/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
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 Down
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: [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() {
Expand Down