From 3b3138cd4b6719237c81304cd6b0c3e09cda0816 Mon Sep 17 00:00:00 2001 From: Roundaround Date: Mon, 4 Aug 2014 10:03:46 -0400 Subject: [PATCH] Added three "new" externally visible API functions. --- README.md | 13 ++++- js/jquery.flot.tooltip.js | 98 ++++++++++++++++---------------- js/jquery.flot.tooltip.min.js | 4 +- js/jquery.flot.tooltip.source.js | 96 +++++++++++++++---------------- package.json | 2 +- 5 files changed, 111 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 0540943..e843212 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,16 @@ when the pull request is merged and how many other changes were made at the same ## Changelog -### What's new in v0.8.3? +### What's new in v0.8.4? + +- merged pull request [#87](https://github.com/krzysu/flot.tooltip/pull/87), adding compatibility with jQuery < 1.2.6 +- added new API functions to Flot's base plot object: + - `setTooltipPosition(pos)` + - `showTooltip(item, pos)` + - `hideTooltip()` +- cleaned a lot of the source code for better maintainability and development + +### v0.8.3 - merged pull requests: [#86](https://github.com/krzysu/flot.tooltip/pull/86), [#85](https://github.com/krzysu/flot.tooltip/pull/85), [#83](https://github.com/krzysu/flot.tooltip/pull/83) - pull request #86 introduced support for showing tooltips when hovering over the lines between points @@ -234,7 +243,7 @@ From now on also minified version is available. - [@ilvalle](https://github.com/ilvalle) - pull request [#77](https://github.com/krzysu/flot.tooltip/pull/77), added time zone support by using $.plot.dateGenerator - [@willianganzert](https://github.com/willianganzert) - pull request [#83](https://github.com/krzysu/flot.tooltip/pull/83), Add "id" to tooltip element - [@larsenmtl](https://github.com/larsenmtl) - pull request [#85](https://github.com/krzysu/flot.tooltip/pull/85), Support for stacked percent plugin -- [@RoboterHund](https://github.com/RoboterHund) - pull request [#86](https://github.com/krzysu/flot.tooltip/pull/86), Compatibility fix for older versions of jQuery +- [@RoboterHund](https://github.com/RoboterHund) - pull request [#87](https://github.com/krzysu/flot.tooltip/pull/86), Compatibility fix for older versions of jQuery * * * Copyright (c) 2011-2014 Krzysztof Urbas (@krzysu) & Evan Steinkerchner (@Roundaround). diff --git a/js/jquery.flot.tooltip.js b/js/jquery.flot.tooltip.js index 8e19c55..e0580b6 100644 --- a/js/jquery.flot.tooltip.js +++ b/js/jquery.flot.tooltip.js @@ -2,7 +2,7 @@ * jquery.flot.tooltip * * description: easy-to-use tooltips for Flot charts - * version: 0.8.3 + * version: 0.8.4 * authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround * website: https://github.com/krzysu/flot.tooltip * @@ -104,7 +104,7 @@ var pos = {}; pos.x = e.pageX; pos.y = e.pageY; - that.updateTooltipPosition(pos); + plot.setTooltipPosition(pos); } function plothover(event, pos, item) { @@ -138,33 +138,8 @@ } }; - // Quick little function for showing the tooltip. - var showTooltip = function (target, position) { - var $tip = that.getDomElement(); - - // convert tooltip content template to real tipText - var tipText = that.stringFormat(that.tooltipOptions.content, target); - - $tip.html(tipText); - that.updateTooltipPosition({ x: position.pageX, y: position.pageY }); - $tip.css({ - left: that.tipPosition.x + that.tooltipOptions.shifts.x, - top: that.tipPosition.y + that.tooltipOptions.shifts.y - }).show(); - - // run callback - if (typeof that.tooltipOptions.onHover === 'function') { - that.tooltipOptions.onHover(target, $tip); - } - }; - - // Quick little function for hiding the tooltip. - var hideTooltip = function () { - that.getDomElement().hide().html(''); - }; - if (item) { - showTooltip(item, pos); + plot.showTooltip(item, pos); } else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines.track === true) { var closestTrace = { distance: -1 @@ -184,7 +159,7 @@ } if (xAfterIndex === -1) { - hideTooltip(); + plot.hideTooltip(); return; } @@ -223,13 +198,54 @@ }); if (closestTrace.distance !== -1) - showTooltip(closestTrace.item, pos); + plot.showTooltip(closestTrace.item, pos); else - hideTooltip(); + plot.hideTooltip(); } else { - hideTooltip(); + plot.hideTooltip(); } } + + // Quick little function for setting the tooltip position. + plot.setTooltipPosition = function (pos) { + var $tip = that.getDomElement(); + + var totalTipWidth = $tip.outerWidth() + that.tooltipOptions.shifts.x; + var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y; + if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) { + pos.x -= totalTipWidth; + } + if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) { + pos.y -= totalTipHeight; + } + that.tipPosition.x = pos.x; + that.tipPosition.y = pos.y; + }; + + // Quick little function for showing the tooltip. + plot.showTooltip = function (target, position) { + var $tip = that.getDomElement(); + + // convert tooltip content template to real tipText + var tipText = that.stringFormat(that.tooltipOptions.content, target); + + $tip.html(tipText); + plot.setTooltipPosition({ x: position.pageX, y: position.pageY }); + $tip.css({ + left: that.tipPosition.x + that.tooltipOptions.shifts.x, + top: that.tipPosition.y + that.tooltipOptions.shifts.y + }).show(); + + // run callback + if (typeof that.tooltipOptions.onHover === 'function') { + that.tooltipOptions.onHover(target, $tip); + } + }; + + // Quick little function for hiding the tooltip. + plot.hideTooltip = function () { + that.getDomElement().hide().html(''); + }; }; /** @@ -260,22 +276,6 @@ return $tip; }; - // as the name says - FlotTooltip.prototype.updateTooltipPosition = function (pos) { - var $tip = $('#' + this.tooltipOptions.id); - - var totalTipWidth = $tip.outerWidth() + this.tooltipOptions.shifts.x; - var totalTipHeight = $tip.outerHeight() + this.tooltipOptions.shifts.y; - if ((pos.x - $(window).scrollLeft()) > ($(window)[this.wfunc]() - totalTipWidth)) { - pos.x -= totalTipWidth; - } - if ((pos.y - $(window).scrollTop()) > ($(window)[this.hfunc]() - totalTipHeight)) { - pos.y -= totalTipHeight; - } - this.tipPosition.x = pos.x; - this.tipPosition.y = pos.y; - }; - /** * core function, create tooltip content * @param {string} content - template with tooltip content @@ -484,7 +484,7 @@ init: init, options: defaultOptions, name: 'tooltip', - version: '0.8.3' + version: '0.8.4' }); })(jQuery); diff --git a/js/jquery.flot.tooltip.min.js b/js/jquery.flot.tooltip.min.js index 2b58de7..6454d1e 100644 --- a/js/jquery.flot.tooltip.min.js +++ b/js/jquery.flot.tooltip.min.js @@ -2,11 +2,11 @@ * jquery.flot.tooltip * * description: easy-to-use tooltips for Flot charts - * version: 0.8.3 + * version: 0.8.4 * authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround * website: https://github.com/krzysu/flot.tooltip * * build on 2014-08-04 * released under MIT License, 2012 */ -!function(a){var b={tooltip:!1,tooltipOpts:{id:"flotTip",content:"%s | X: %x | Y: %y",xDateFormat:null,yDateFormat:null,monthNames:null,dayNames:null,shifts:{x:10,y:20},defaultTheme:!0,lines:{track:!1,threshold:.05},onHover:function(){},$compat:!1}},c=function(a){this.tipPosition={x:0,y:0},this.init(a)};c.prototype.init=function(b){function c(a){var b={};b.x=a.pageX,b.y=a.pageY,e.updateTooltipPosition(b)}function d(c,d,f){var g=function(a,b,c,d){return Math.sqrt((c-a)*(c-a)+(d-b)*(d-b))},h=function(a,b,c,d,e,f,h){if(!h||(h=function(a,b,c,d,e,f){if("undefined"!=typeof c)return{x:c,y:b};if("undefined"!=typeof d)return{x:a,y:d};var g,h=-1/((f-d)/(e-c));return{x:g=(e*(a*h-b+d)+c*(a*-h+b-f))/(h*(e-c)+d-f),y:h*g-h*a+b}}(a,b,c,d,e,f),h.x>=Math.min(c,e)&&h.x<=Math.max(c,e)&&h.y>=Math.min(d,f)&&h.y<=Math.max(d,f))){var i=d-f,j=e-c,k=c*f-d*e;return Math.abs(i*a+j*b+k)/Math.sqrt(i*i+j*j)}var l=g(a,b,c,d),m=g(a,b,e,f);return l>m?m:l},i=function(a,b){var c=e.getDomElement(),d=e.stringFormat(e.tooltipOptions.content,a);c.html(d),e.updateTooltipPosition({x:b.pageX,y:b.pageY}),c.css({left:e.tipPosition.x+e.tooltipOptions.shifts.x,top:e.tipPosition.y+e.tooltipOptions.shifts.y}).show(),"function"==typeof e.tooltipOptions.onHover&&e.tooltipOptions.onHover(a,c)},j=function(){e.getDomElement().hide().html("")};if(f)i(f,d);else if(e.plotOptions.series.lines.show&&e.tooltipOptions.lines.track===!0){var k={distance:-1};a.each(b.getData(),function(a,b){for(var c=0,f=-1,i=1;i=d.x&&(c=i-1,f=i);if(-1===f)return void j();var l={x:b.data[c][0],y:b.data[c][1]},m={x:b.data[f][0],y:b.data[f][1]},n=h(d.x,d.y,l.x,l.y,m.x,m.y,!1);if(ng;g++)this.plotPlugins.push(a.plot.plugins[g].name);b.hooks.bindEvents.push(function(b,f){if(e.plotOptions=b.getOptions(),e.plotOptions.tooltip!==!1&&"undefined"!=typeof e.plotOptions.tooltip){e.tooltipOptions=e.plotOptions.tooltipOpts,e.tooltipOptions.$compat?(e.wfunc="width",e.hfunc="height"):(e.wfunc="innerWidth",e.hfunc="innerHeight");{e.getDomElement()}a(b.getPlaceholder()).bind("plothover",d),a(f).bind("mousemove",c)}}),b.hooks.shutdown.push(function(b,e){a(b.getPlaceholder()).unbind("plothover",d),a(e).unbind("mousemove",c)})},c.prototype.getDomElement=function(){var b=a("#"+this.tooltipOptions.id);return 0===b.length&&(b=a("
").attr("id",this.tooltipOptions.id),b.appendTo("body").hide().css({position:"absolute"}),this.tooltipOptions.defaultTheme&&b.css({background:"#fff","z-index":"1040",padding:"0.4em 0.6em","border-radius":"0.5em","font-size":"0.8em",border:"1px solid #111",display:"none","white-space":"nowrap"})),b},c.prototype.updateTooltipPosition=function(b){var c=a("#"+this.tooltipOptions.id),d=c.outerWidth()+this.tooltipOptions.shifts.x,e=c.outerHeight()+this.tooltipOptions.shifts.y;b.x-a(window).scrollLeft()>a(window)[this.wfunc]()-d&&(b.x-=d),b.y-a(window).scrollTop()>a(window)[this.hfunc]()-e&&(b.y-=e),this.tipPosition.x=b.x,this.tipPosition.y=b.y},c.prototype.stringFormat=function(a,b){var c,d,e,f,g=/%p\.{0,1}(\d{0,})/,h=/%s/,i=/%lx/,j=/%ly/,k=/%x\.{0,1}(\d{0,})/,l=/%y\.{0,1}(\d{0,})/,m="%x",n="%y",o="%ct";if("undefined"!=typeof b.series.threshold?(c=b.datapoint[0],d=b.datapoint[1],e=b.datapoint[2]):"undefined"!=typeof b.series.lines&&b.series.lines.steps?(c=b.series.datapoints.points[2*b.dataIndex],d=b.series.datapoints.points[2*b.dataIndex+1],e=""):(c=b.series.data[b.dataIndex][0],d=b.series.data[b.dataIndex][1],e=b.series.data[b.dataIndex][2]),null===b.series.label&&b.series.originSeries&&(b.series.label=b.series.originSeries.label),"function"==typeof a&&(a=a(b.series.label,c,d,b)),"undefined"!=typeof b.series.percent?f=b.series.percent:"undefined"!=typeof b.series.percents&&(f=b.series.percents[b.dataIndex]),"number"==typeof f&&(a=this.adjustValPrecision(g,a,f)),a="undefined"!=typeof b.series.label?a.replace(h,b.series.label):a.replace(h,""),a=this.hasAxisLabel("xaxis",b)?a.replace(i,b.series.xaxis.options.axisLabel):a.replace(i,""),a=this.hasAxisLabel("yaxis",b)?a.replace(j,b.series.yaxis.options.axisLabel):a.replace(j,""),this.isTimeMode("xaxis",b)&&this.isXDateFormat(b)&&(a=a.replace(k,this.timestampToDate(c,this.tooltipOptions.xDateFormat,b.series.xaxis.options))),this.isTimeMode("yaxis",b)&&this.isYDateFormat(b)&&(a=a.replace(l,this.timestampToDate(d,this.tooltipOptions.yDateFormat,b.series.yaxis.options))),"number"==typeof c&&(a=this.adjustValPrecision(k,a,c)),"number"==typeof d&&(a=this.adjustValPrecision(l,a,d)),"undefined"!=typeof b.series.xaxis.ticks){var p;p=this.hasRotatedXAxisTicks(b)?"rotatedTicks":"ticks";var q=b.dataIndex+b.seriesIndex;if(b.series.xaxis[p].length>q&&!this.isTimeMode("xaxis",b)){var r=this.isCategoriesMode("xaxis",b)?b.series.xaxis[p][q].label:b.series.xaxis[p][q].v;r===c&&(a=a.replace(k,b.series.xaxis[p][q].label))}}if("undefined"!=typeof b.series.yaxis.ticks)for(var s in b.series.yaxis.ticks)if(b.series.yaxis.ticks.hasOwnProperty(s)){var t=this.isCategoriesMode("yaxis",b)?b.series.yaxis.ticks[s].label:b.series.yaxis.ticks[s].v;t===d&&(a=a.replace(l,b.series.yaxis.ticks[s].label))}return"undefined"!=typeof b.series.xaxis.tickFormatter&&(a=a.replace(m,b.series.xaxis.tickFormatter(c,b.series.xaxis).replace(/\$/g,"$$"))),"undefined"!=typeof b.series.yaxis.tickFormatter&&(a=a.replace(n,b.series.yaxis.tickFormatter(d,b.series.yaxis).replace(/\$/g,"$$"))),e&&(a=a.replace(o,e)),a},c.prototype.isTimeMode=function(a,b){return"undefined"!=typeof b.series[a].options.mode&&"time"===b.series[a].options.mode},c.prototype.isXDateFormat=function(){return"undefined"!=typeof this.tooltipOptions.xDateFormat&&null!==this.tooltipOptions.xDateFormat},c.prototype.isYDateFormat=function(){return"undefined"!=typeof this.tooltipOptions.yDateFormat&&null!==this.tooltipOptions.yDateFormat},c.prototype.isCategoriesMode=function(a,b){return"undefined"!=typeof b.series[a].options.mode&&"categories"===b.series[a].options.mode},c.prototype.timestampToDate=function(b,c,d){var e=a.plot.dateGenerator(b,d);return a.plot.formatDate(e,c,this.tooltipOptions.monthNames,this.tooltipOptions.dayNames)},c.prototype.adjustValPrecision=function(a,b,c){var d,e=b.match(a);return null!==e&&""!==RegExp.$1&&(d=RegExp.$1,c=c.toFixed(d),b=b.replace(a,c)),b},c.prototype.hasAxisLabel=function(b,c){return-1!==a.inArray(this.plotPlugins,"axisLabels")&&"undefined"!=typeof c.series[b].options.axisLabel&&c.series[b].options.axisLabel.length>0},c.prototype.hasRotatedXAxisTicks=function(b){return-1!==a.inArray(this.plotPlugins,"tickRotor")&&"undefined"!=typeof b.series.xaxis.rotatedTicks};var d=function(a){new c(a)};a.plot.plugins.push({init:d,options:b,name:"tooltip",version:"0.8.3"})}(jQuery); \ No newline at end of file +!function(a){var b={tooltip:!1,tooltipOpts:{id:"flotTip",content:"%s | X: %x | Y: %y",xDateFormat:null,yDateFormat:null,monthNames:null,dayNames:null,shifts:{x:10,y:20},defaultTheme:!0,lines:{track:!1,threshold:.05},onHover:function(){},$compat:!1}},c=function(a){this.tipPosition={x:0,y:0},this.init(a)};c.prototype.init=function(b){function c(a){var c={};c.x=a.pageX,c.y=a.pageY,b.setTooltipPosition(c)}function d(c,d,f){var g=function(a,b,c,d){return Math.sqrt((c-a)*(c-a)+(d-b)*(d-b))},h=function(a,b,c,d,e,f,h){if(!h||(h=function(a,b,c,d,e,f){if("undefined"!=typeof c)return{x:c,y:b};if("undefined"!=typeof d)return{x:a,y:d};var g,h=-1/((f-d)/(e-c));return{x:g=(e*(a*h-b+d)+c*(a*-h+b-f))/(h*(e-c)+d-f),y:h*g-h*a+b}}(a,b,c,d,e,f),h.x>=Math.min(c,e)&&h.x<=Math.max(c,e)&&h.y>=Math.min(d,f)&&h.y<=Math.max(d,f))){var i=d-f,j=e-c,k=c*f-d*e;return Math.abs(i*a+j*b+k)/Math.sqrt(i*i+j*j)}var l=g(a,b,c,d),m=g(a,b,e,f);return l>m?m:l};if(f)b.showTooltip(f,d);else if(e.plotOptions.series.lines.show&&e.tooltipOptions.lines.track===!0){var i={distance:-1};a.each(b.getData(),function(a,c){for(var f=0,j=-1,k=1;k=d.x&&(f=k-1,j=k);if(-1===j)return void b.hideTooltip();var l={x:c.data[f][0],y:c.data[f][1]},m={x:c.data[j][0],y:c.data[j][1]},n=h(d.x,d.y,l.x,l.y,m.x,m.y,!1);if(ng;g++)this.plotPlugins.push(a.plot.plugins[g].name);b.hooks.bindEvents.push(function(b,f){if(e.plotOptions=b.getOptions(),e.plotOptions.tooltip!==!1&&"undefined"!=typeof e.plotOptions.tooltip){e.tooltipOptions=e.plotOptions.tooltipOpts,e.tooltipOptions.$compat?(e.wfunc="width",e.hfunc="height"):(e.wfunc="innerWidth",e.hfunc="innerHeight");{e.getDomElement()}a(b.getPlaceholder()).bind("plothover",d),a(f).bind("mousemove",c)}}),b.hooks.shutdown.push(function(b,e){a(b.getPlaceholder()).unbind("plothover",d),a(e).unbind("mousemove",c)}),b.setTooltipPosition=function(b){var c=e.getDomElement(),d=c.outerWidth()+e.tooltipOptions.shifts.x,f=c.outerHeight()+e.tooltipOptions.shifts.y;b.x-a(window).scrollLeft()>a(window)[e.wfunc]()-d&&(b.x-=d),b.y-a(window).scrollTop()>a(window)[e.hfunc]()-f&&(b.y-=f),e.tipPosition.x=b.x,e.tipPosition.y=b.y},b.showTooltip=function(a,c){var d=e.getDomElement(),f=e.stringFormat(e.tooltipOptions.content,a);d.html(f),b.setTooltipPosition({x:c.pageX,y:c.pageY}),d.css({left:e.tipPosition.x+e.tooltipOptions.shifts.x,top:e.tipPosition.y+e.tooltipOptions.shifts.y}).show(),"function"==typeof e.tooltipOptions.onHover&&e.tooltipOptions.onHover(a,d)},b.hideTooltip=function(){e.getDomElement().hide().html("")}},c.prototype.getDomElement=function(){var b=a("#"+this.tooltipOptions.id);return 0===b.length&&(b=a("
").attr("id",this.tooltipOptions.id),b.appendTo("body").hide().css({position:"absolute"}),this.tooltipOptions.defaultTheme&&b.css({background:"#fff","z-index":"1040",padding:"0.4em 0.6em","border-radius":"0.5em","font-size":"0.8em",border:"1px solid #111",display:"none","white-space":"nowrap"})),b},c.prototype.stringFormat=function(a,b){var c,d,e,f,g=/%p\.{0,1}(\d{0,})/,h=/%s/,i=/%lx/,j=/%ly/,k=/%x\.{0,1}(\d{0,})/,l=/%y\.{0,1}(\d{0,})/,m="%x",n="%y",o="%ct";if("undefined"!=typeof b.series.threshold?(c=b.datapoint[0],d=b.datapoint[1],e=b.datapoint[2]):"undefined"!=typeof b.series.lines&&b.series.lines.steps?(c=b.series.datapoints.points[2*b.dataIndex],d=b.series.datapoints.points[2*b.dataIndex+1],e=""):(c=b.series.data[b.dataIndex][0],d=b.series.data[b.dataIndex][1],e=b.series.data[b.dataIndex][2]),null===b.series.label&&b.series.originSeries&&(b.series.label=b.series.originSeries.label),"function"==typeof a&&(a=a(b.series.label,c,d,b)),"undefined"!=typeof b.series.percent?f=b.series.percent:"undefined"!=typeof b.series.percents&&(f=b.series.percents[b.dataIndex]),"number"==typeof f&&(a=this.adjustValPrecision(g,a,f)),a="undefined"!=typeof b.series.label?a.replace(h,b.series.label):a.replace(h,""),a=this.hasAxisLabel("xaxis",b)?a.replace(i,b.series.xaxis.options.axisLabel):a.replace(i,""),a=this.hasAxisLabel("yaxis",b)?a.replace(j,b.series.yaxis.options.axisLabel):a.replace(j,""),this.isTimeMode("xaxis",b)&&this.isXDateFormat(b)&&(a=a.replace(k,this.timestampToDate(c,this.tooltipOptions.xDateFormat,b.series.xaxis.options))),this.isTimeMode("yaxis",b)&&this.isYDateFormat(b)&&(a=a.replace(l,this.timestampToDate(d,this.tooltipOptions.yDateFormat,b.series.yaxis.options))),"number"==typeof c&&(a=this.adjustValPrecision(k,a,c)),"number"==typeof d&&(a=this.adjustValPrecision(l,a,d)),"undefined"!=typeof b.series.xaxis.ticks){var p;p=this.hasRotatedXAxisTicks(b)?"rotatedTicks":"ticks";var q=b.dataIndex+b.seriesIndex;if(b.series.xaxis[p].length>q&&!this.isTimeMode("xaxis",b)){var r=this.isCategoriesMode("xaxis",b)?b.series.xaxis[p][q].label:b.series.xaxis[p][q].v;r===c&&(a=a.replace(k,b.series.xaxis[p][q].label))}}if("undefined"!=typeof b.series.yaxis.ticks)for(var s in b.series.yaxis.ticks)if(b.series.yaxis.ticks.hasOwnProperty(s)){var t=this.isCategoriesMode("yaxis",b)?b.series.yaxis.ticks[s].label:b.series.yaxis.ticks[s].v;t===d&&(a=a.replace(l,b.series.yaxis.ticks[s].label))}return"undefined"!=typeof b.series.xaxis.tickFormatter&&(a=a.replace(m,b.series.xaxis.tickFormatter(c,b.series.xaxis).replace(/\$/g,"$$"))),"undefined"!=typeof b.series.yaxis.tickFormatter&&(a=a.replace(n,b.series.yaxis.tickFormatter(d,b.series.yaxis).replace(/\$/g,"$$"))),e&&(a=a.replace(o,e)),a},c.prototype.isTimeMode=function(a,b){return"undefined"!=typeof b.series[a].options.mode&&"time"===b.series[a].options.mode},c.prototype.isXDateFormat=function(){return"undefined"!=typeof this.tooltipOptions.xDateFormat&&null!==this.tooltipOptions.xDateFormat},c.prototype.isYDateFormat=function(){return"undefined"!=typeof this.tooltipOptions.yDateFormat&&null!==this.tooltipOptions.yDateFormat},c.prototype.isCategoriesMode=function(a,b){return"undefined"!=typeof b.series[a].options.mode&&"categories"===b.series[a].options.mode},c.prototype.timestampToDate=function(b,c,d){var e=a.plot.dateGenerator(b,d);return a.plot.formatDate(e,c,this.tooltipOptions.monthNames,this.tooltipOptions.dayNames)},c.prototype.adjustValPrecision=function(a,b,c){var d,e=b.match(a);return null!==e&&""!==RegExp.$1&&(d=RegExp.$1,c=c.toFixed(d),b=b.replace(a,c)),b},c.prototype.hasAxisLabel=function(b,c){return-1!==a.inArray(this.plotPlugins,"axisLabels")&&"undefined"!=typeof c.series[b].options.axisLabel&&c.series[b].options.axisLabel.length>0},c.prototype.hasRotatedXAxisTicks=function(b){return-1!==a.inArray(this.plotPlugins,"tickRotor")&&"undefined"!=typeof b.series.xaxis.rotatedTicks};var d=function(a){new c(a)};a.plot.plugins.push({init:d,options:b,name:"tooltip",version:"0.8.4"})}(jQuery); \ No newline at end of file diff --git a/js/jquery.flot.tooltip.source.js b/js/jquery.flot.tooltip.source.js index ea65fdc..4cc0381 100644 --- a/js/jquery.flot.tooltip.source.js +++ b/js/jquery.flot.tooltip.source.js @@ -93,7 +93,7 @@ var pos = {}; pos.x = e.pageX; pos.y = e.pageY; - that.updateTooltipPosition(pos); + plot.setTooltipPosition(pos); } function plothover(event, pos, item) { @@ -127,33 +127,8 @@ } }; - // Quick little function for showing the tooltip. - var showTooltip = function (target, position) { - var $tip = that.getDomElement(); - - // convert tooltip content template to real tipText - var tipText = that.stringFormat(that.tooltipOptions.content, target); - - $tip.html(tipText); - that.updateTooltipPosition({ x: position.pageX, y: position.pageY }); - $tip.css({ - left: that.tipPosition.x + that.tooltipOptions.shifts.x, - top: that.tipPosition.y + that.tooltipOptions.shifts.y - }).show(); - - // run callback - if (typeof that.tooltipOptions.onHover === 'function') { - that.tooltipOptions.onHover(target, $tip); - } - }; - - // Quick little function for hiding the tooltip. - var hideTooltip = function () { - that.getDomElement().hide().html(''); - }; - if (item) { - showTooltip(item, pos); + plot.showTooltip(item, pos); } else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines.track === true) { var closestTrace = { distance: -1 @@ -173,7 +148,7 @@ } if (xAfterIndex === -1) { - hideTooltip(); + plot.hideTooltip(); return; } @@ -212,13 +187,54 @@ }); if (closestTrace.distance !== -1) - showTooltip(closestTrace.item, pos); + plot.showTooltip(closestTrace.item, pos); else - hideTooltip(); + plot.hideTooltip(); } else { - hideTooltip(); + plot.hideTooltip(); } } + + // Quick little function for setting the tooltip position. + plot.setTooltipPosition = function (pos) { + var $tip = that.getDomElement(); + + var totalTipWidth = $tip.outerWidth() + that.tooltipOptions.shifts.x; + var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y; + if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) { + pos.x -= totalTipWidth; + } + if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) { + pos.y -= totalTipHeight; + } + that.tipPosition.x = pos.x; + that.tipPosition.y = pos.y; + }; + + // Quick little function for showing the tooltip. + plot.showTooltip = function (target, position) { + var $tip = that.getDomElement(); + + // convert tooltip content template to real tipText + var tipText = that.stringFormat(that.tooltipOptions.content, target); + + $tip.html(tipText); + plot.setTooltipPosition({ x: position.pageX, y: position.pageY }); + $tip.css({ + left: that.tipPosition.x + that.tooltipOptions.shifts.x, + top: that.tipPosition.y + that.tooltipOptions.shifts.y + }).show(); + + // run callback + if (typeof that.tooltipOptions.onHover === 'function') { + that.tooltipOptions.onHover(target, $tip); + } + }; + + // Quick little function for hiding the tooltip. + plot.hideTooltip = function () { + that.getDomElement().hide().html(''); + }; }; /** @@ -249,22 +265,6 @@ return $tip; }; - // as the name says - FlotTooltip.prototype.updateTooltipPosition = function (pos) { - var $tip = $('#' + this.tooltipOptions.id); - - var totalTipWidth = $tip.outerWidth() + this.tooltipOptions.shifts.x; - var totalTipHeight = $tip.outerHeight() + this.tooltipOptions.shifts.y; - if ((pos.x - $(window).scrollLeft()) > ($(window)[this.wfunc]() - totalTipWidth)) { - pos.x -= totalTipWidth; - } - if ((pos.y - $(window).scrollTop()) > ($(window)[this.hfunc]() - totalTipHeight)) { - pos.y -= totalTipHeight; - } - this.tipPosition.x = pos.x; - this.tipPosition.y = pos.y; - }; - /** * core function, create tooltip content * @param {string} content - template with tooltip content @@ -473,7 +473,7 @@ init: init, options: defaultOptions, name: 'tooltip', - version: '0.8.3' + version: '0.8.4' }); })(jQuery); diff --git a/package.json b/package.json index 4821041..94b8f48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery.flot.tooltip", - "version": "0.8.3", + "version": "0.8.4", "description": "easy-to-use tooltips for Flot charts", "website": "https://github.com/krzysu/flot.tooltip", "directories": {