Skip to content

Commit

Permalink
[vis/tooltip] modify the placement if we can't swap direction
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer Alger committed Oct 1, 2014
1 parent cdcacdb commit d56e14c
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/kibana/components/vislib/lib/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ define(function (require) {

Tooltip.prototype.getTooltipPlacement = function (event) {
var self = this;
var OFFSET = 15;
var OFFSET = 10;

var chart = $(self.el).find('.' + self.containerClass);
if (!chart.size()) return;
Expand All @@ -82,22 +82,44 @@ define(function (require) {
west: event.clientX - tipWidth - OFFSET
};

var eastOverEdge = left.east + tipWidth > chartPos.right;
var westOverEdge = left.west + tipWidth < chartPos.left;

// the placements if we were to place the tip north or south
var top = {
south: event.clientY + OFFSET,
north: event.clientY - tipHeight - OFFSET
};

var northOverEdge = top.north + tipHeight < chartPos.top;
var southOverEdge = top.south + tipHeight > chartPos.bottom;

return {
top: (southOverEdge && !northOverEdge) ? top.north : top.south,
left: (eastOverEdge && !westOverEdge) ? left.west : left.east
// number of pixels that the toolip would overflow it's far
// side, if we placed it that way. (negative === no overflow)
var overflow = {
north: chartPos.top - top.north,
east: (left.east + tipWidth) - chartPos.right,
south: (top.south + tipHeight) - chartPos.bottom,
west: chartPos.left - left.west
};

var placement = {};

if (overflow.south > 0) {
if (overflow.north < 0) {
placement.top = top.north;
} else {
placement.top = top.south - overflow.south;
}
} else {
placement.top = top.south;
}

if (overflow.east > 0) {
if (overflow.west < 0) {
placement.left = left.west;
} else {
placement.left = left.east - overflow.east;
}
} else {
placement.left = left.east;
}

return placement;
};

return Tooltip;
Expand Down

0 comments on commit d56e14c

Please sign in to comment.