Skip to content

Commit

Permalink
Merge pull request #1840 from nnnick/fix/1731-part-2
Browse files Browse the repository at this point in the history
Better handling of large tooltips
  • Loading branch information
etimberg committed Jan 3, 2016
2 parents 8487375 + d9cc121 commit 579e322
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/core/core.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,22 +352,40 @@
this._model.yAlign = 'bottom';
}

var lf, rf;
var lf, rf; // functions to determine left, right alignment
var olf, orf; // functions to determine if left/right alignment causes tooltip to go outside chart
var yf; // function to get the y alignment if the tooltip goes outside of the left or right edges
var _this = this;
var midX = (this._chartInstance.chartArea.left + this._chartInstance.chartArea.right) / 2;
var midY = (this._chartInstance.chartArea.top + this._chartInstance.chartArea.bottom) / 2;

if (this._model.yAlign === 'center') {
lf = function(x) { return x <= midX; };
rf = function(x) { return x > midX; };
olf = function(x) { return x + size.width > _this._chart.width; };
orf = function(x) { return x - size.width < 0; };
yf = function(y) { return y <= midY ? 'top' : 'bottom'; };
} else {
lf = function(x) { return x <= (size.width / 2); };
rf = function(x) { return x >= (_this._chart.width - (size.width / 2)); };
}

if (lf(this._model.x)) {
this._model.xAlign = 'left';

// Is tooltip too wide and goes over the right side of the chart.?
if (olf(this._model.x)) {
this._model.xAlign = 'center';
this._model.yAlign = yf(this._model.y);
}
} else if (rf(this._model.x)) {
this._model.xAlign = 'right';

// Is tooltip too wide and goes outside left edge of canvas?
if (orf(this._model.x)) {
this._model.xAlign = 'center';
this._model.yAlign = yf(this._model.y);
}
}
},
getBackgroundPoint: function getBackgroundPoint(vm, size) {
Expand Down

0 comments on commit 579e322

Please sign in to comment.