Skip to content

Fix hover label positioning for bar trace with set width #1527

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 6 commits into from
Apr 5, 2017

Conversation

etpinard
Copy link
Contributor

@etpinard etpinard commented Mar 29, 2017

fixes #1317 - at least the hovermode: 'closest' part, see #1317 (comment) for more details.

TODO:

  • add more tests!

@etpinard etpinard added status: in progress bug something broken labels Mar 29, 2017
@etpinard etpinard added this to the v1.26.0 milestone Mar 29, 2017
var dx, dy;
if(trace.orientation === 'h') {
dx = function(di) {
// add a gradient so hovering near the end of a
// bar makes it a little closer match
return Fx.inbox(di.b - xval, di.x - xval) + (di.x - xval) / (di.x - di.b);
};
dy = function(di) {
dy = function(di, i) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suppose this (providing i as an extra arg) is the most flexible way to handle the issue for future extensibility, but the way that matches better with what we do in scatter - and should be slightly better perf - would be to include the width of each point in di

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. Good call! Post #1519 this will be easy!

@@ -392,9 +392,11 @@ function setBarCenter(gd, pa, sieve) {
for(var j = 0; j < calcTrace.length; j++) {
var calcBar = calcTrace[j];

// store the actual bar width and position, for use by hover
var width = calcBar.w = (barwidthIsArray) ? barwidth[j] : barwidth;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suspect we could clean up the hover routine quite a bit more if we stopped using .x and .y in calcdata... then we'd only have to switch at the beginning of the routine "are xval and yval position and size or vice versa" but I wasn't sure what the consequences of that would be beyond hover, just adding one more value was safe and easy.

* Nearly always it's the group that matters, but in case the bar
* was explicitly set wider than its group we'd better accept the
* whole bar.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

fixes the "compare" part of #1317 - at least if the bar is wider than our default. I guess if it's narrower and there are other things on the plot, we could be accepting this point when we shouldn't be... maybe I'll look at that case tomorrow and see if I can trigger it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, the only case I can see where this pops up is an explicitly narrow (width<1) single bar with other things on the plot as well, like:

var data = [
	{type: 'bar', x: [1], y: [2], width: 0.1}, {x:[1.1], y:[1]}
];
var layout = {
	xaxis: {
		range: [-2, 2]
	}
};

Plotly.newPlot(gd, data, layout);

then the hoverable region for the bar is so wide it actually extends past the scatter point. But the scatter point still wins when you'd want it to. So if that's all it is, I don't feel like it's worth addressing this very obscure edge case, it'd be just as likely to cause other problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with ⬆️ . Thanks for documenting it!

@@ -61,7 +59,7 @@ module.exports = function plot(gd, plotinfo, cdbar) {
// log values go off-screen by plotwidth
// so you see them continue if you drag the plot
var p0 = di.p + ((poffsetIsArray) ? poffset[i] : poffset),
p1 = p0 + ((barwidthIsArray) ? barwidth[i] : barwidth),
p1 = p0 + di.w,
Copy link
Collaborator

Choose a reason for hiding this comment

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

a little follow-on benefit of adding di.w

@alexcjohnson
Copy link
Collaborator

@etpinard back to you for review. Do the tests I added satisfy the TODO up top?

@etpinard
Copy link
Contributor Author

etpinard commented Apr 5, 2017

@alexcjohnson

Do the tests I added satisfy the TODO up top?

Absolutely.

💃 thanks for wrapping this one up 🎉

@alexcjohnson alexcjohnson merged commit ce06ea9 into master Apr 5, 2017
@alexcjohnson alexcjohnson deleted the bar-width-hover branch April 5, 2017 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Manual width breaks hover boxes
2 participants