[vis/tooltip] modify the placement if we can't swap direction

This commit is contained in:
Spencer Alger 2014-09-30 18:58:57 -07:00
parent cdcacdbce7
commit d56e14c6db

View file

@ -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;
@ -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;