Skip to content

Make hover spikes work when no tick labels are present #1980

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
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
79 changes: 61 additions & 18 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1825,9 +1825,16 @@ axes.doTicks = function(gd, axid, skipTitle) {
// tick labels - for now just the main labels.
// TODO: mirror labels, esp for subplots
var tickLabels = container.selectAll('g.' + tcls).data(vals, datafn);
if(!ax.showticklabels || !isNumeric(position)) {

if(!isNumeric(position)) {
tickLabels.remove();
drawAxTitle();
return;
}
if(!ax.showticklabels) {
tickLabels.remove();
drawAxTitle();
calcBoundingBox();
return;
}

Expand Down Expand Up @@ -1993,23 +2000,59 @@ axes.doTicks = function(gd, axid, skipTitle) {
}

function calcBoundingBox() {
var bBox = container.node().getBoundingClientRect();
var gdBB = gd.getBoundingClientRect();

/*
* the way we're going to use this, the positioning that matters
* is relative to the origin of gd. This is important particularly
* if gd is scrollable, and may have been scrolled between the time
* we calculate this and the time we use it
*/
ax._boundingBox = {
width: bBox.width,
height: bBox.height,
left: bBox.left - gdBB.left,
right: bBox.right - gdBB.left,
top: bBox.top - gdBB.top,
bottom: bBox.bottom - gdBB.top
};
if(ax.showticklabels) {
var gdBB = gd.getBoundingClientRect();
var bBox = container.node().getBoundingClientRect();

/*
* the way we're going to use this, the positioning that matters
* is relative to the origin of gd. This is important particularly
* if gd is scrollable, and may have been scrolled between the time
* we calculate this and the time we use it
*/

ax._boundingBox = {
width: bBox.width,
height: bBox.height,
left: bBox.left - gdBB.left,
right: bBox.right - gdBB.left,
top: bBox.top - gdBB.top,
bottom: bBox.bottom - gdBB.top
};
} else {
var gs = fullLayout._size;
var pos;

// set dummy bbox for ticklabel-less axes

if(axLetter === 'x') {
pos = ax.anchor === 'free' ?
gs.t + gs.h * (1 - ax.position) :
gs.t + gs.h * (1 - ax._anchorAxis.domain[{bottom: 0, top: 1}[ax.side]]);

ax._boundingBox = {
top: pos,
bottom: pos,
left: ax._offset,
rigth: ax._offset + ax._length,
width: ax._length,
height: 0
};
} else {
pos = ax.anchor === 'free' ?
gs.l + gs.w * ax.position :
gs.l + gs.w * ax._anchorAxis.domain[{left: 0, right: 1}[ax.side]];

ax._boundingBox = {
left: pos,
right: pos,
bottom: ax._offset + ax._length,
top: ax._offset,
height: ax._length,
width: 0
};
}
}

/*
* for spikelines: what's the full domain of positions in the
Expand Down
20 changes: 19 additions & 1 deletion test/jasmine/tests/hover_spikeline_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Plotly = require('@lib/index');
var Fx = require('@src/components/fx');
var Lib = require('@src/lib');

var fail = require('../assets/fail_test');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');

Expand All @@ -16,6 +17,7 @@ describe('spikeline', function() {

describe('hover', function() {
var mockCopy = Lib.extendDeep({}, mock);
var gd;

mockCopy.layout.xaxis.showspikes = true;
mockCopy.layout.xaxis.spikemode = 'toaxis';
Expand All @@ -24,8 +26,10 @@ describe('spikeline', function() {
mockCopy.layout.xaxis2.showspikes = true;
mockCopy.layout.xaxis2.spikemode = 'toaxis';
mockCopy.layout.hovermode = 'closest';

beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});

it('draws lines and markers on enabled axes', function() {
Expand All @@ -34,6 +38,20 @@ describe('spikeline', function() {
expect(d3.selectAll('circle.spikeline').size()).toEqual(1);
});

it('draws lines and markers on enabled axes w/o tick labels', function(done) {
Plotly.relayout(gd, {
'xaxis.showticklabels': false,
'yaxis.showticklabels': false
})
.then(function() {
Fx.hover('graph', {xval: 2, yval: 3}, 'xy');
expect(d3.selectAll('line.spikeline').size()).toEqual(4);
expect(d3.selectAll('circle.spikeline').size()).toEqual(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it worth verifying that these things have reasonable dimensions/positions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. done in 92b606b

})
.catch(fail)
.then(done);
});

it('doesn\'t draw lines and markers on disabled axes', function() {
Fx.hover('graph', {xval: 30, yval: 40}, 'x2y2');
expect(d3.selectAll('line.spikeline').size()).toEqual(2);
Expand Down