Skip to content
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

Fix layout of centered hover info #2154 #2440

Merged
merged 2 commits into from
Mar 5, 2018
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
5 changes: 3 additions & 2 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ function alignHoverText(hoverLabels, rotateLabels) {
offsetY = d.offset;
if(d.anchor === 'middle') {
txx -= d.tx2width / 2;
tx2x -= d.tx2width / 2;
tx2x += d.txwidth / 2 + HOVERTEXTPAD;
}
if(rotateLabels) {
offsetY *= -YSHIFTY;
Expand All @@ -1135,7 +1135,8 @@ function alignHoverText(hoverLabels, rotateLabels) {

g.select('path').attr('d', d.anchor === 'middle' ?
// middle aligned: rect centered on data
('M-' + (d.bx / 2) + ',-' + (d.by / 2) + 'h' + d.bx + 'v' + d.by + 'h-' + d.bx + 'Z') :
('M-' + (d.bx / 2 + d.tx2width / 2) + ',-' + (d.by / 2) +
'h' + d.bx + 'v' + d.by + 'h-' + d.bx + 'Z') :
// left or right aligned: side rect with arrow to data
('M0,0L' + (horzSign * HOVERARROWSIZE + offsetX) + ',' + (HOVERARROWSIZE + offsetY) +
'v' + (d.by / 2 - HOVERARROWSIZE) +
Expand Down
86 changes: 86 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,92 @@ describe('hover info', function() {
});

});

describe('centered', function() {
var trace1 = {
x: ['giraffes'],
y: [5],
name: 'LA Zoo',
type: 'bar',
text: ['Way too long hover info!']
};
var trace2 = {
x: ['giraffes'],
y: [5],
name: 'SF Zoo',
type: 'bar',
text: ['San Francisco']
};
var data = [trace1, trace2];
var layout = {width: 600, height: 300, barmode: 'stack'};

var gd;

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

function centeredHoverInfoNodes() {
var g = d3.selectAll('g.hoverlayer g.hovertext').filter(function() {
return !d3.select(this).select('[data-unformatted="LA Zoo"]').empty();
});

return {
primaryText: g.select('text:not([data-unformatted="LA Zoo"])').node(),
primaryBox: g.select('path').node(),
secondaryText: g.select('[data-unformatted="LA Zoo"]').node(),
secondaryBox: g.select('rect').node()
};
}

function ensureCentered(hoverInfoNodes) {
expect(hoverInfoNodes.primaryText.getAttribute('text-anchor')).toBe('middle');
expect(hoverInfoNodes.secondaryText.getAttribute('text-anchor')).toBe('middle');
return hoverInfoNodes;
}

function assertElemInside(elem, container, msg) {
var elemBB = elem.getBoundingClientRect();
var contBB = container.getBoundingClientRect();
expect(contBB.left < elemBB.left &&
contBB.right > elemBB.right &&
contBB.top < elemBB.top &&
contBB.bottom > elemBB.bottom).toBe(true, msg);
}

function assertElemRightTo(elem, refElem, msg) {
var elemBB = elem.getBoundingClientRect();
var refElemBB = refElem.getBoundingClientRect();
expect(elemBB.left >= refElemBB.right).toBe(true, msg);
}

function assertTopsAligned(elem1, elem2, msg) {
var elem1BB = elem1.getBoundingClientRect();
var elem2BB = elem2.getBoundingClientRect();

// Hint: toBeWithin tolerance is exclusive, hence a
// diff of exactly 1 would fail the test
var tolerance = 1.1;
expect(elem1BB.top - elem2BB.top).toBeWithin(0, tolerance, msg);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love these assert functions! They'd be good candidates for assets/custom_assertions but we can leave them here until someone else finds a use for them elsewhere.


it('renders labels inside boxes', function() {
_hover(gd, 300, 150);

var nodes = ensureCentered(centeredHoverInfoNodes());
assertElemInside(nodes.primaryText, nodes.primaryBox, 'Primary text inside box');
assertElemInside(nodes.secondaryText, nodes.secondaryBox, 'Secondary text inside box');
});

it('renders secondary info box right to primary info box', function() {
_hover(gd, 300, 150);

var nodes = ensureCentered(centeredHoverInfoNodes());
assertElemRightTo(nodes.secondaryBox, nodes.primaryBox, 'Secondary box right to primary box');
assertTopsAligned(nodes.secondaryBox, nodes.primaryBox, 'Top edges of primary and secondary boxes aligned');
});
});
});

describe('hover info on stacked subplots', function() {
Expand Down