Update flot-tooltip to 0.9.0

This commit is contained in:
Garret Wassermann 2017-05-26 16:05:19 -04:00
parent 1e9c4888b9
commit 23898fbf35
3 changed files with 59 additions and 55 deletions

View File

@ -2,11 +2,11 @@
* jquery.flot.tooltip * jquery.flot.tooltip
* *
* description: easy-to-use tooltips for Flot charts * description: easy-to-use tooltips for Flot charts
* version: 0.8.7 * version: 0.9.0
* authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround * authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround
* website: https://github.com/krzysu/flot.tooltip * website: https://github.com/krzysu/flot.tooltip
* *
* build on 2016-03-15 * build on 2016-07-26
* released under MIT License, 2012 * released under MIT License, 2012
*/ */
(function ($) { (function ($) {
@ -25,7 +25,7 @@
// %y -> Y value, // %y -> Y value,
// %x.2 -> precision of X value, // %x.2 -> precision of X value,
// %p -> percent // %p -> percent
// %n -> value (not percent) of pie chart // %n -> value (not percent) of pie chart
xDateFormat: null, xDateFormat: null,
yDateFormat: null, yDateFormat: null,
monthNames: null, monthNames: null,
@ -260,18 +260,19 @@
var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y; var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y;
if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) { if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) {
pos.x -= totalTipWidth; pos.x -= totalTipWidth;
pos.x = Math.max(pos.x, 0);
} }
if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) { if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) {
pos.y -= totalTipHeight; pos.y -= totalTipHeight;
} }
/* /*
The section applies the new positioning ONLY if pos.x and pos.y The section applies the new positioning ONLY if pos.x and pos.y
are numbers. If they are undefined or not a number, use the last are numbers. If they are undefined or not a number, use the last
known numerical position. This hack fixes a bug that kept pie known numerical position. This hack fixes a bug that kept pie
charts from keeping their tooltip positioning. charts from keeping their tooltip positioning.
*/ */
if (isNaN(pos.x)) { if (isNaN(pos.x)) {
that.tipPosition.x = that.tipPosition.xPrev; that.tipPosition.x = that.tipPosition.xPrev;
} }
@ -286,7 +287,7 @@
that.tipPosition.y = pos.y; that.tipPosition.y = pos.y;
that.tipPosition.yPrev = pos.y; that.tipPosition.yPrev = pos.y;
} }
}; };
// Quick little function for showing the tooltip. // Quick little function for showing the tooltip.
@ -299,7 +300,7 @@
return; return;
$tip.html(tipText); $tip.html(tipText);
plot.setTooltipPosition({ x: position.pageX, y: position.pageY }); plot.setTooltipPosition({ x: that.tipPosition.x, y: that.tipPosition.y });
$tip.css({ $tip.css({
left: that.tipPosition.x + that.tooltipOptions.shifts.x, left: that.tipPosition.x + that.tooltipOptions.shifts.x,
top: that.tipPosition.y + that.tooltipOptions.shifts.y top: that.tipPosition.y + that.tooltipOptions.shifts.y
@ -333,7 +334,7 @@
if( $tip.length === 0 ){ if( $tip.length === 0 ){
$tip = $('<div />').addClass(this.tooltipOptions.cssClass); $tip = $('<div />').addClass(this.tooltipOptions.cssClass);
$tip.appendTo('body').hide().css({position: 'absolute'}); $tip.appendTo('body').hide().css({position: 'absolute'});
if(this.tooltipOptions.defaultTheme) { if(this.tooltipOptions.defaultTheme) {
$tip.css({ $tip.css({
'background': '#fff', 'background': '#fff',
@ -370,7 +371,7 @@
var yPatternWithoutPrecision = "%y"; var yPatternWithoutPrecision = "%y";
var customTextPattern = "%ct"; var customTextPattern = "%ct";
var nPiePattern = "%n"; var nPiePattern = "%n";
var x, y, customText, p, n; var x, y, customText, p, n;
// for threshold plugin we need to read data from different place // for threshold plugin we need to read data from different place
@ -385,7 +386,7 @@
x = item.datapoint[0]; x = item.datapoint[0];
y = item.datapoint[1]; y = item.datapoint[1];
} }
else if (typeof item.series.lines !== "undefined" && item.series.lines.steps) { else if (typeof item.series.lines !== "undefined" && item.series.lines.steps) {
x = item.series.datapoints.points[item.dataIndex * 2]; x = item.series.datapoints.points[item.dataIndex * 2];
y = item.series.datapoints.points[item.dataIndex * 2 + 1]; y = item.series.datapoints.points[item.dataIndex * 2 + 1];
@ -412,33 +413,34 @@
return ''; return '';
} }
/* replacement of %ct and other multi-character templates must /* replacement of %ct and other multi-character templates must
precede the replacement of single-character templates precede the replacement of single-character templates
to avoid conflict between '%c' and '%ct' and similar substrings to avoid conflict between '%c' and '%ct' and similar substrings
*/ */
if (customText) if (customText) {
content = content.replace(customTextPattern, customText); content = content.replace(customTextPattern, customText);
}
// percent match for pie charts and stacked percent // percent match for pie charts and stacked percent
if (typeof (item.series.percent) !== 'undefined') { if (typeof (item.series.percent) !== 'undefined') {
p = item.series.percent; p = item.series.percent;
} else if (typeof (item.series.percents) !== 'undefined') { } else if (typeof (item.series.percents) !== 'undefined') {
p = item.series.percents[item.dataIndex]; p = item.series.percents[item.dataIndex];
} }
if (typeof p === 'number') { if (typeof p === 'number') {
content = this.adjustValPrecision(percentPattern, content, p); content = this.adjustValPrecision(percentPattern, content, p);
} }
// replace %n with number of items represented by slice in pie charts // replace %n with number of items represented by slice in pie charts
if (item.series.hasOwnProperty('pie')) { if (item.series.hasOwnProperty('pie')) {
if (typeof (item.series.data[0][1] !== 'undefined')) { if (typeof item.series.data[0][1] !== 'undefined') {
n = item.series.data[0][1]; n = item.series.data[0][1];
} }
} }
if (typeof n === 'number') { if (typeof n === 'number') {
content = content.replace(nPiePattern, n); content = content.replace(nPiePattern, n);
} }
// series match // series match
if (typeof(item.series.label) !== 'undefined') { if (typeof(item.series.label) !== 'undefined') {
content = content.replace(seriesPattern, item.series.label); content = content.replace(seriesPattern, item.series.label);
@ -446,7 +448,7 @@
//remove %s if label is undefined //remove %s if label is undefined
content = content.replace(seriesPattern, ""); content = content.replace(seriesPattern, "");
} }
// color match // color match
if (typeof(item.series.color) !== 'undefined') { if (typeof(item.series.color) !== 'undefined') {
content = content.replace(colorPattern, item.series.color); content = content.replace(colorPattern, item.series.color);

File diff suppressed because one or more lines are too long

View File

@ -14,7 +14,7 @@
// %y -> Y value, // %y -> Y value,
// %x.2 -> precision of X value, // %x.2 -> precision of X value,
// %p -> percent // %p -> percent
// %n -> value (not percent) of pie chart // %n -> value (not percent) of pie chart
xDateFormat: null, xDateFormat: null,
yDateFormat: null, yDateFormat: null,
monthNames: null, monthNames: null,
@ -249,18 +249,19 @@
var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y; var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y;
if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) { if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) {
pos.x -= totalTipWidth; pos.x -= totalTipWidth;
pos.x = Math.max(pos.x, 0);
} }
if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) { if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) {
pos.y -= totalTipHeight; pos.y -= totalTipHeight;
} }
/* /*
The section applies the new positioning ONLY if pos.x and pos.y The section applies the new positioning ONLY if pos.x and pos.y
are numbers. If they are undefined or not a number, use the last are numbers. If they are undefined or not a number, use the last
known numerical position. This hack fixes a bug that kept pie known numerical position. This hack fixes a bug that kept pie
charts from keeping their tooltip positioning. charts from keeping their tooltip positioning.
*/ */
if (isNaN(pos.x)) { if (isNaN(pos.x)) {
that.tipPosition.x = that.tipPosition.xPrev; that.tipPosition.x = that.tipPosition.xPrev;
} }
@ -275,7 +276,7 @@
that.tipPosition.y = pos.y; that.tipPosition.y = pos.y;
that.tipPosition.yPrev = pos.y; that.tipPosition.yPrev = pos.y;
} }
}; };
// Quick little function for showing the tooltip. // Quick little function for showing the tooltip.
@ -288,7 +289,7 @@
return; return;
$tip.html(tipText); $tip.html(tipText);
plot.setTooltipPosition({ x: position.pageX, y: position.pageY }); plot.setTooltipPosition({ x: that.tipPosition.x, y: that.tipPosition.y });
$tip.css({ $tip.css({
left: that.tipPosition.x + that.tooltipOptions.shifts.x, left: that.tipPosition.x + that.tooltipOptions.shifts.x,
top: that.tipPosition.y + that.tooltipOptions.shifts.y top: that.tipPosition.y + that.tooltipOptions.shifts.y
@ -322,7 +323,7 @@
if( $tip.length === 0 ){ if( $tip.length === 0 ){
$tip = $('<div />').addClass(this.tooltipOptions.cssClass); $tip = $('<div />').addClass(this.tooltipOptions.cssClass);
$tip.appendTo('body').hide().css({position: 'absolute'}); $tip.appendTo('body').hide().css({position: 'absolute'});
if(this.tooltipOptions.defaultTheme) { if(this.tooltipOptions.defaultTheme) {
$tip.css({ $tip.css({
'background': '#fff', 'background': '#fff',
@ -359,7 +360,7 @@
var yPatternWithoutPrecision = "%y"; var yPatternWithoutPrecision = "%y";
var customTextPattern = "%ct"; var customTextPattern = "%ct";
var nPiePattern = "%n"; var nPiePattern = "%n";
var x, y, customText, p, n; var x, y, customText, p, n;
// for threshold plugin we need to read data from different place // for threshold plugin we need to read data from different place
@ -374,7 +375,7 @@
x = item.datapoint[0]; x = item.datapoint[0];
y = item.datapoint[1]; y = item.datapoint[1];
} }
else if (typeof item.series.lines !== "undefined" && item.series.lines.steps) { else if (typeof item.series.lines !== "undefined" && item.series.lines.steps) {
x = item.series.datapoints.points[item.dataIndex * 2]; x = item.series.datapoints.points[item.dataIndex * 2];
y = item.series.datapoints.points[item.dataIndex * 2 + 1]; y = item.series.datapoints.points[item.dataIndex * 2 + 1];
@ -401,33 +402,34 @@
return ''; return '';
} }
/* replacement of %ct and other multi-character templates must /* replacement of %ct and other multi-character templates must
precede the replacement of single-character templates precede the replacement of single-character templates
to avoid conflict between '%c' and '%ct' and similar substrings to avoid conflict between '%c' and '%ct' and similar substrings
*/ */
if (customText) if (customText) {
content = content.replace(customTextPattern, customText); content = content.replace(customTextPattern, customText);
}
// percent match for pie charts and stacked percent // percent match for pie charts and stacked percent
if (typeof (item.series.percent) !== 'undefined') { if (typeof (item.series.percent) !== 'undefined') {
p = item.series.percent; p = item.series.percent;
} else if (typeof (item.series.percents) !== 'undefined') { } else if (typeof (item.series.percents) !== 'undefined') {
p = item.series.percents[item.dataIndex]; p = item.series.percents[item.dataIndex];
} }
if (typeof p === 'number') { if (typeof p === 'number') {
content = this.adjustValPrecision(percentPattern, content, p); content = this.adjustValPrecision(percentPattern, content, p);
} }
// replace %n with number of items represented by slice in pie charts // replace %n with number of items represented by slice in pie charts
if (item.series.hasOwnProperty('pie')) { if (item.series.hasOwnProperty('pie')) {
if (typeof (item.series.data[0][1] !== 'undefined')) { if (typeof item.series.data[0][1] !== 'undefined') {
n = item.series.data[0][1]; n = item.series.data[0][1];
} }
} }
if (typeof n === 'number') { if (typeof n === 'number') {
content = content.replace(nPiePattern, n); content = content.replace(nPiePattern, n);
} }
// series match // series match
if (typeof(item.series.label) !== 'undefined') { if (typeof(item.series.label) !== 'undefined') {
content = content.replace(seriesPattern, item.series.label); content = content.replace(seriesPattern, item.series.label);
@ -435,7 +437,7 @@
//remove %s if label is undefined //remove %s if label is undefined
content = content.replace(seriesPattern, ""); content = content.replace(seriesPattern, "");
} }
// color match // color match
if (typeof(item.series.color) !== 'undefined') { if (typeof(item.series.color) !== 'undefined') {
content = content.replace(colorPattern, item.series.color); content = content.replace(colorPattern, item.series.color);