From 78cd8c854fbc03bfe852344f0fd8b56bdb328f3d Mon Sep 17 00:00:00 2001 From: Roundaround Date: Mon, 4 Aug 2014 09:47:11 -0400 Subject: [PATCH] Bit more code cleanup, including now the production files --- js/jquery.flot.tooltip.js | 118 ++++++++++++++++--------------- js/jquery.flot.tooltip.min.js | 4 +- js/jquery.flot.tooltip.source.js | 16 ++--- 3 files changed, 70 insertions(+), 68 deletions(-) diff --git a/js/jquery.flot.tooltip.js b/js/jquery.flot.tooltip.js index 50fa61e..8e19c55 100644 --- a/js/jquery.flot.tooltip.js +++ b/js/jquery.flot.tooltip.js @@ -6,7 +6,7 @@ * authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround * website: https://github.com/krzysu/flot.tooltip * - * build on 2014-08-01 + * build on 2014-08-04 * released under MIT License, 2012 */ (function ($) { @@ -39,13 +39,14 @@ }, // callbacks - onHover: function(flotItem, $tooltipEl) {} + onHover: function (flotItem, $tooltipEl) {}, + + $compat: false } }; // object - var FlotTooltip = function(plot) { - + var FlotTooltip = function (plot) { // variables this.tipPosition = {x: 0, y: 0}; @@ -53,8 +54,7 @@ }; // main plugin function - FlotTooltip.prototype.init = function(plot) { - + FlotTooltip.prototype.init = function (plot) { var that = this; // detect other flot plugins @@ -78,6 +78,14 @@ // shortcut to access tooltip options that.tooltipOptions = that.plotOptions.tooltipOpts; + if (that.tooltipOptions.$compat) { + that.wfunc = 'width'; + that.hfunc = 'height'; + } else { + that.wfunc = 'innerWidth'; + that.hfunc = 'innerHeight'; + } + // create tooltip DOM element var $tip = that.getDomElement(); @@ -131,14 +139,14 @@ }; // Quick little function for showing the tooltip. - function showTooltip(item) { + var showTooltip = function (target, position) { var $tip = that.getDomElement(); // convert tooltip content template to real tipText - var tipText = that.stringFormat(that.tooltipOptions.content, item); + var tipText = that.stringFormat(that.tooltipOptions.content, target); $tip.html(tipText); - that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY }); + 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 @@ -146,17 +154,17 @@ // run callback if (typeof that.tooltipOptions.onHover === 'function') { - that.tooltipOptions.onHover(item, $tip); + that.tooltipOptions.onHover(target, $tip); } - } + }; // Quick little function for hiding the tooltip. - function hideTooltip() { + var hideTooltip = function () { that.getDomElement().hide().html(''); - } + }; if (item) { - showTooltip(item); + showTooltip(item, pos); } else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines.track === true) { var closestTrace = { distance: -1 @@ -215,7 +223,7 @@ }); if (closestTrace.distance !== -1) - showTooltip(closestTrace.item); + showTooltip(closestTrace.item, pos); else hideTooltip(); } else { @@ -228,7 +236,7 @@ * get or create tooltip DOM element * @return jQuery object */ - FlotTooltip.prototype.getDomElement = function() { + FlotTooltip.prototype.getDomElement = function () { var $tip = $('#' + this.tooltipOptions.id); if( $tip.length === 0 ){ @@ -253,15 +261,15 @@ }; // as the name says - FlotTooltip.prototype.updateTooltipPosition = function(pos) { + 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).innerWidth() - totalTipWidth)) { + if ((pos.x - $(window).scrollLeft()) > ($(window)[this.wfunc]() - totalTipWidth)) { pos.x -= totalTipWidth; } - if ((pos.y - $(window).scrollTop()) > ($(window).innerHeight() - totalTipHeight)) { + if ((pos.y - $(window).scrollTop()) > ($(window)[this.hfunc]() - totalTipHeight)) { pos.y -= totalTipHeight; } this.tipPosition.x = pos.x; @@ -274,7 +282,7 @@ * @param {object} item - Flot item * @return {string} real tooltip content for current item */ - FlotTooltip.prototype.stringFormat = function(content, item) { + FlotTooltip.prototype.stringFormat = function (content, item) { var percentPattern = /%p\.{0,1}(\d{0,})/; var seriesPattern = /%s/; @@ -310,80 +318,75 @@ } // if it is a function callback get the content string - if( typeof(content) === 'function' ) { + if (typeof(content) === 'function') { content = content(item.series.label, x, y, item); } // percent match for pie charts and stacked percent - if (typeof (item.series.percent) !== 'undefined'){ + if (typeof (item.series.percent) !== 'undefined') { p = item.series.percent; - } else if (typeof (item.series.percents) !== 'undefined'){ + } else if (typeof (item.series.percents) !== 'undefined') { p = item.series.percents[item.dataIndex]; } - if( typeof p === 'number' ) { + if (typeof p === 'number') { content = this.adjustValPrecision(percentPattern, content, p); } // series match - if( typeof(item.series.label) !== 'undefined' ) { + if (typeof(item.series.label) !== 'undefined') { content = content.replace(seriesPattern, item.series.label); - } - else { + } else { //remove %s if label is undefined content = content.replace(seriesPattern, ""); } // x axis label match - if( this.hasAxisLabel('xaxis', item) ) { + if (this.hasAxisLabel('xaxis', item)) { content = content.replace(xLabelPattern, item.series.xaxis.options.axisLabel); - } - else { + } else { //remove %lx if axis label is undefined or axislabels plugin not present content = content.replace(xLabelPattern, ""); } // y axis label match - if( this.hasAxisLabel('yaxis', item) ) { + if (this.hasAxisLabel('yaxis', item)) { content = content.replace(yLabelPattern, item.series.yaxis.options.axisLabel); - } - else { + } else { //remove %ly if axis label is undefined or axislabels plugin not present content = content.replace(yLabelPattern, ""); } // time mode axes with custom dateFormat - if(this.isTimeMode('xaxis', item) && this.isXDateFormat(item)) { + if (this.isTimeMode('xaxis', item) && this.isXDateFormat(item)) { content = content.replace(xPattern, this.timestampToDate(x, this.tooltipOptions.xDateFormat, item.series.xaxis.options)); } - - if(this.isTimeMode('yaxis', item) && this.isYDateFormat(item)) { + if (this.isTimeMode('yaxis', item) && this.isYDateFormat(item)) { content = content.replace(yPattern, this.timestampToDate(y, this.tooltipOptions.yDateFormat, item.series.yaxis.options)); } // set precision if defined - if(typeof x === 'number') { + if (typeof x === 'number') { content = this.adjustValPrecision(xPattern, content, x); } - if(typeof y === 'number') { + if (typeof y === 'number') { content = this.adjustValPrecision(yPattern, content, y); } // change x from number to given label, if given - if(typeof item.series.xaxis.ticks !== 'undefined') { + if (typeof item.series.xaxis.ticks !== 'undefined') { var ticks; - if(this.hasRotatedXAxisTicks(item)) { + if (this.hasRotatedXAxisTicks(item)) { // xaxis.ticks will be an empty array if tickRotor is being used, but the values are available in rotatedTicks ticks = 'rotatedTicks'; - } - else { + } else { ticks = 'ticks'; } // see https://github.com/krzysu/flot.tooltip/issues/65 var tickIndex = item.dataIndex + item.seriesIndex; - if(item.series.xaxis[ticks].length > tickIndex && !this.isTimeMode('xaxis', item)) { + if (item.series.xaxis[ticks].length > tickIndex && !this.isTimeMode('xaxis', item)) { var valueX = (this.isCategoriesMode('xaxis', item)) ? item.series.xaxis[ticks][tickIndex].label : item.series.xaxis[ticks][tickIndex].v; if (valueX === x) { content = content.replace(xPattern, item.series.xaxis[ticks][tickIndex].label); @@ -392,7 +395,7 @@ } // change y from number to given label, if given - if(typeof item.series.yaxis.ticks !== 'undefined') { + if (typeof item.series.yaxis.ticks !== 'undefined') { for (var index in item.series.yaxis.ticks) { if (item.series.yaxis.ticks.hasOwnProperty(index)) { var valueY = (this.isCategoriesMode('yaxis', item)) ? item.series.yaxis.ticks[index].label : item.series.yaxis.ticks[index].v; @@ -404,47 +407,46 @@ } // if no value customization, use tickFormatter by default - if(typeof item.series.xaxis.tickFormatter !== 'undefined') { + if (typeof item.series.xaxis.tickFormatter !== 'undefined') { //escape dollar content = content.replace(xPatternWithoutPrecision, item.series.xaxis.tickFormatter(x, item.series.xaxis).replace(/\$/g, '$$')); } - if(typeof item.series.yaxis.tickFormatter !== 'undefined') { + if (typeof item.series.yaxis.tickFormatter !== 'undefined') { //escape dollar content = content.replace(yPatternWithoutPrecision, item.series.yaxis.tickFormatter(y, item.series.yaxis).replace(/\$/g, '$$')); } - if(customText) { + if (customText) content = content.replace(customTextPattern, customText); - } return content; }; // helpers just for readability - FlotTooltip.prototype.isTimeMode = function(axisName, item) { + FlotTooltip.prototype.isTimeMode = function (axisName, item) { return (typeof item.series[axisName].options.mode !== 'undefined' && item.series[axisName].options.mode === 'time'); }; - FlotTooltip.prototype.isXDateFormat = function(item) { + FlotTooltip.prototype.isXDateFormat = function (item) { return (typeof this.tooltipOptions.xDateFormat !== 'undefined' && this.tooltipOptions.xDateFormat !== null); }; - FlotTooltip.prototype.isYDateFormat = function(item) { + FlotTooltip.prototype.isYDateFormat = function (item) { return (typeof this.tooltipOptions.yDateFormat !== 'undefined' && this.tooltipOptions.yDateFormat !== null); }; - FlotTooltip.prototype.isCategoriesMode = function(axisName, item) { + FlotTooltip.prototype.isCategoriesMode = function (axisName, item) { return (typeof item.series[axisName].options.mode !== 'undefined' && item.series[axisName].options.mode === 'categories'); }; // - FlotTooltip.prototype.timestampToDate = function(tmst, dateFormat, options) { + FlotTooltip.prototype.timestampToDate = function (tmst, dateFormat, options) { var theDate = $.plot.dateGenerator(tmst, options); return $.plot.formatDate(theDate, dateFormat, this.tooltipOptions.monthNames, this.tooltipOptions.dayNames); }; // - FlotTooltip.prototype.adjustValPrecision = function(pattern, content, value) { + FlotTooltip.prototype.adjustValPrecision = function (pattern, content, value) { var precision; var matchResult = content.match(pattern); @@ -463,17 +465,17 @@ // other plugins detection below // check if flot-axislabels plugin (https://github.com/markrcote/flot-axislabels) is used and that an axis label is given - FlotTooltip.prototype.hasAxisLabel = function(axisName, item) { + FlotTooltip.prototype.hasAxisLabel = function (axisName, item) { return ($.inArray(this.plotPlugins, 'axisLabels') !== -1 && typeof item.series[axisName].options.axisLabel !== 'undefined' && item.series[axisName].options.axisLabel.length > 0); }; // check whether flot-tickRotor, a plugin which allows rotation of X-axis ticks, is being used - FlotTooltip.prototype.hasRotatedXAxisTicks = function(item) { - return ($.grep($.plot.plugins, function(p){ return p.name === "tickRotor"; }).length === 1 && typeof item.series.xaxis.rotatedTicks !== 'undefined'); + FlotTooltip.prototype.hasRotatedXAxisTicks = function (item) { + return ($.inArray(this.plotPlugins, 'tickRotor') !== -1 && typeof item.series.xaxis.rotatedTicks !== 'undefined'); }; // - var init = function(plot) { + var init = function (plot) { new FlotTooltip(plot); }; diff --git a/js/jquery.flot.tooltip.min.js b/js/jquery.flot.tooltip.min.js index 868a883..2b58de7 100644 --- a/js/jquery.flot.tooltip.min.js +++ b/js/jquery.flot.tooltip.min.js @@ -6,7 +6,7 @@ * authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround * website: https://github.com/krzysu/flot.tooltip * - * build on 2014-08-01 + * 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(){}}},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){function g(a){var b=e.getDomElement(),c=e.stringFormat(e.tooltipOptions.content,a);b.html(c),e.updateTooltipPosition({x:d.pageX,y:d.pageY}),b.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,b)}function h(){e.getDomElement().hide().html("")}var i=function(a,b,c,d){return Math.sqrt((c-a)*(c-a)+(d-b)*(d-b))},j=function(a,b,c,d,e,f,g){if(!g||(g=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),g.x>=Math.min(c,e)&&g.x<=Math.max(c,e)&&g.y>=Math.min(d,f)&&g.y<=Math.max(d,f))){var h=d-f,j=e-c,k=c*f-d*e;return Math.abs(h*a+j*b+k)/Math.sqrt(h*h+j*j)}var l=i(a,b,c,d),m=i(a,b,e,f);return l>m?m:l};if(f)g(f);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,g=1;g=d.x&&(c=g-1,f=g);if(-1===f)return void h();var l={x:b.data[c][0],y:b.data[c][1]},m={x:b.data[f][0],y:b.data[f][1]},n=j(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.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).innerWidth()-d&&(b.x-=d),b.y-a(window).scrollTop()>a(window).innerHeight()-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.grep(a.plot.plugins,function(a){return"tickRotor"===a.name}).length&&"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 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 diff --git a/js/jquery.flot.tooltip.source.js b/js/jquery.flot.tooltip.source.js index b78ffaf..ea65fdc 100644 --- a/js/jquery.flot.tooltip.source.js +++ b/js/jquery.flot.tooltip.source.js @@ -128,14 +128,14 @@ }; // Quick little function for showing the tooltip. - var showTooltip = function (item) { + var showTooltip = function (target, position) { var $tip = that.getDomElement(); // convert tooltip content template to real tipText - var tipText = that.stringFormat(that.tooltipOptions.content, item); + var tipText = that.stringFormat(that.tooltipOptions.content, target); $tip.html(tipText); - that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY }); + 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 @@ -143,17 +143,17 @@ // run callback if (typeof that.tooltipOptions.onHover === 'function') { - that.tooltipOptions.onHover(item, $tip); + that.tooltipOptions.onHover(target, $tip); } - } + }; // Quick little function for hiding the tooltip. var hideTooltip = function () { that.getDomElement().hide().html(''); - } + }; if (item) { - showTooltip(item); + showTooltip(item, pos); } else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines.track === true) { var closestTrace = { distance: -1 @@ -212,7 +212,7 @@ }); if (closestTrace.distance !== -1) - showTooltip(closestTrace.item); + showTooltip(closestTrace.item, pos); else hideTooltip(); } else {