diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..b2bc3e9 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,9 @@ +## For developers/contributors + +If you wish to contribute, please do so by editing the jquery.flot.tooltip.source.js file. The other .js files are built with Grunt and should not be directly edited. + +When working with external plugin support, you can use the array plotPlugins (via this.plotPlugins), which is a collection of the names of the currently loaded Flot plugins. For instance if checking for the existance of the official +symbol plugin, you would check `if ($.inArray('symbol', this.plotPlugins) !== -1)`. + +There exists a Gruntfile.js for development purposes, but please do not commit built production or minified .js files when making a pull request. Additionally, do not change the version, because the new version could vary depending on +when the pull request is merged and how many other changes were made at the same time. diff --git a/README.md b/README.md index 4092513..74459b8 100644 --- a/README.md +++ b/README.md @@ -82,15 +82,13 @@ In comments there are default values ## For developers/contributors -If you wish to contribute, please do so by editing the jquery.flot.tooltip.source.js file. The other .js files are built with Grunt and should not be directly edited. +See CONTRIBUTING.md -When working with external plugin support, you can use the array plotPlugins (via this.plotPlugins), which is a collection of the names of the currently loaded Flot plugins. For instance if checking for the existance of the official -symbol plugin, you would check `if ($.inArray('symbol', this.plotPlugins) !== -1)`. +## Changelog -There exists a Gruntfile.js for development purposes, but please do not commit built production or minified .js files when making a pull request. Additionally, do not change the version, because the new version could vary depending on -when the pull request is merged and how many other changes were made at the same time. +### v0.9.0 -## Changelog +- merged pull requests: [#140](https://github.com/krzysu/flot.tooltip/pull/140), [#142](https://github.com/krzysu/flot.tooltip/pull/142) ### v0.8.7 diff --git a/js/jquery.flot.tooltip.js b/js/jquery.flot.tooltip.js index 18d4e28..ff606c6 100644 --- a/js/jquery.flot.tooltip.js +++ b/js/jquery.flot.tooltip.js @@ -2,11 +2,11 @@ * jquery.flot.tooltip * * 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 * website: https://github.com/krzysu/flot.tooltip * - * build on 2016-03-15 + * build on 2016-07-26 * released under MIT License, 2012 */ (function ($) { @@ -25,7 +25,7 @@ // %y -> Y value, // %x.2 -> precision of X value, // %p -> percent - // %n -> value (not percent) of pie chart + // %n -> value (not percent) of pie chart xDateFormat: null, yDateFormat: null, monthNames: null, @@ -260,18 +260,19 @@ var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y; if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) { pos.x -= totalTipWidth; + pos.x = Math.max(pos.x, 0); } if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) { pos.y -= totalTipHeight; } - /* + /* 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 - 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. */ - + if (isNaN(pos.x)) { that.tipPosition.x = that.tipPosition.xPrev; } @@ -286,7 +287,7 @@ that.tipPosition.y = pos.y; that.tipPosition.yPrev = pos.y; } - + }; // Quick little function for showing the tooltip. @@ -333,7 +334,7 @@ if( $tip.length === 0 ){ $tip = $('
').addClass(this.tooltipOptions.cssClass); $tip.appendTo('body').hide().css({position: 'absolute'}); - + if(this.tooltipOptions.defaultTheme) { $tip.css({ 'background': '#fff', @@ -370,7 +371,7 @@ var yPatternWithoutPrecision = "%y"; var customTextPattern = "%ct"; var nPiePattern = "%n"; - + var x, y, customText, p, n; // for threshold plugin we need to read data from different place @@ -385,7 +386,7 @@ x = item.datapoint[0]; y = item.datapoint[1]; } - + else if (typeof item.series.lines !== "undefined" && item.series.lines.steps) { x = item.series.datapoints.points[item.dataIndex * 2]; y = item.series.datapoints.points[item.dataIndex * 2 + 1]; @@ -412,33 +413,34 @@ return ''; } - /* replacement of %ct and other multi-character templates must - precede the replacement of single-character templates - to avoid conflict between '%c' and '%ct' and similar substrings - */ - if (customText) + /* replacement of %ct and other multi-character templates must + precede the replacement of single-character templates + to avoid conflict between '%c' and '%ct' and similar substrings + */ + if (customText) { content = content.replace(customTextPattern, customText); + } // percent match for pie charts and stacked percent if (typeof (item.series.percent) !== 'undefined') { p = item.series.percent; } else if (typeof (item.series.percents) !== 'undefined') { p = item.series.percents[item.dataIndex]; - } + } if (typeof p === 'number') { content = this.adjustValPrecision(percentPattern, content, p); } - // replace %n with number of items represented by slice in pie charts - if (item.series.hasOwnProperty('pie')) { - if (typeof (item.series.data[0][1] !== 'undefined')) { - n = item.series.data[0][1]; - } - } - if (typeof n === 'number') { + // replace %n with number of items represented by slice in pie charts + if (item.series.hasOwnProperty('pie')) { + if (typeof item.series.data[0][1] !== 'undefined') { + n = item.series.data[0][1]; + } + } + if (typeof n === 'number') { content = content.replace(nPiePattern, n); - } - + } + // series match if (typeof(item.series.label) !== 'undefined') { content = content.replace(seriesPattern, item.series.label); @@ -446,7 +448,7 @@ //remove %s if label is undefined content = content.replace(seriesPattern, ""); } - + // color match if (typeof(item.series.color) !== 'undefined') { content = content.replace(colorPattern, item.series.color); diff --git a/js/jquery.flot.tooltip.min.js b/js/jquery.flot.tooltip.min.js index 6e2376b..451811a 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.7 + * version: 0.9.0 * authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround * website: https://github.com/krzysu/flot.tooltip * - * build on 2016-03-15 + * build on 2016-07-26 * released under MIT License, 2012 */ -!function(a){var b={tooltip:{show:!1,cssClass:"flotTip",content:"%s | X: %x | Y: %y",xDateFormat:null,yDateFormat:null,monthNames:null,dayNames:null,shifts:{x:10,y:20},defaultTheme:!0,snap:!0,lines:!1,clickTips:!1,onHover:function(a,b){},$compat:!1}};b.tooltipOpts=b.tooltip;var 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,g){f.clickmode?(a(b.getPlaceholder()).bind("plothover",e),b.hideTooltip(),f.clickmode=!1):(e(c,d,g),f.getDomElement().is(":visible")&&(a(b.getPlaceholder()).unbind("plothover",e),f.clickmode=!0))}function e(c,d,e){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(e)b.showTooltip(e,f.tooltipOptions.snap?e:d);else if(f.plotOptions.series.lines.show&&f.tooltipOptions.lines===!0){var i=f.plotOptions.grid.mouseActiveRadius,j={distance:i+1},k=d;a.each(b.getData(),function(a,c){for(var e=0,i=-1,l=1;l=d.x&&(e=l-1,i=l);if(-1===i)return void b.hideTooltip();var m={x:c.data[e][0],y:c.data[e][1]},n={x:c.data[i][0],y:c.data[i][1]},o=h(c.xaxis.p2c(d.x),c.yaxis.p2c(d.y),c.xaxis.p2c(m.x),c.yaxis.p2c(m.y),c.xaxis.p2c(n.x),c.yaxis.p2c(n.y),!1);if(oh;h++)this.plotPlugins.push(a.plot.plugins[h].name);b.hooks.bindEvents.push(function(b,g){if(f.plotOptions=b.getOptions(),"boolean"==typeof f.plotOptions.tooltip&&(f.plotOptions.tooltipOpts.show=f.plotOptions.tooltip,f.plotOptions.tooltip=f.plotOptions.tooltipOpts,delete f.plotOptions.tooltipOpts),f.plotOptions.tooltip.show!==!1&&"undefined"!=typeof f.plotOptions.tooltip.show){f.tooltipOptions=f.plotOptions.tooltip,f.tooltipOptions.$compat?(f.wfunc="width",f.hfunc="height"):(f.wfunc="innerWidth",f.hfunc="innerHeight");f.getDomElement();a(b.getPlaceholder()).bind("plothover",e),f.tooltipOptions.clickTips&&a(b.getPlaceholder()).bind("plotclick",d),f.clickmode=!1,a(g).bind("mousemove",c)}}),b.hooks.shutdown.push(function(b,f){a(b.getPlaceholder()).unbind("plothover",e),a(b.getPlaceholder()).unbind("plotclick",d),b.removeTooltip(),a(f).unbind("mousemove",c)}),b.setTooltipPosition=function(b){var c=f.getDomElement(),d=c.outerWidth()+f.tooltipOptions.shifts.x,e=c.outerHeight()+f.tooltipOptions.shifts.y;b.x-a(window).scrollLeft()>a(window)[f.wfunc]()-d&&(b.x-=d),b.y-a(window).scrollTop()>a(window)[f.hfunc]()-e&&(b.y-=e),isNaN(b.x)?f.tipPosition.x=f.tipPosition.xPrev:(f.tipPosition.x=b.x,f.tipPosition.xPrev=b.x),isNaN(b.y)?f.tipPosition.y=f.tipPosition.yPrev:(f.tipPosition.y=b.y,f.tipPosition.yPrev=b.y)},b.showTooltip=function(a,c,d){var e=f.getDomElement(),g=f.stringFormat(f.tooltipOptions.content,a);""!==g&&(e.html(g),b.setTooltipPosition({x:c.pageX,y:c.pageY}),e.css({left:f.tipPosition.x+f.tooltipOptions.shifts.x,top:f.tipPosition.y+f.tooltipOptions.shifts.y}).show(),"function"==typeof f.tooltipOptions.onHover&&f.tooltipOptions.onHover(a,e))},b.hideTooltip=function(){f.getDomElement().hide().html("")},b.removeTooltip=function(){f.getDomElement().remove()}},c.prototype.getDomElement=function(){var b=a("
");return this.tooltipOptions&&this.tooltipOptions.cssClass&&(b=a("."+this.tooltipOptions.cssClass),0===b.length&&(b=a("
").addClass(this.tooltipOptions.cssClass),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,h=/%p\.{0,1}(\d{0,})/,i=/%s/,j=/%c/,k=/%lx/,l=/%ly/,m=/%x\.{0,1}(\d{0,})/,n=/%y\.{0,1}(\d{0,})/,o="%x",p="%y",q="%ct",r="%n";if("undefined"!=typeof b.series.threshold?(c=b.datapoint[0],d=b.datapoint[1],e=b.datapoint[2]):"undefined"!=typeof b.series.curvedLines?(c=b.datapoint[0],d=b.datapoint[1]):"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)),"boolean"==typeof a&&!a)return"";if(e&&(a=a.replace(q,e)),"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(h,a,f)),b.series.hasOwnProperty("pie")&&(g=b.series.data[0][1]),"number"==typeof g&&(a=a.replace(r,g)),a="undefined"!=typeof b.series.label?a.replace(i,b.series.label):a.replace(i,""),a="undefined"!=typeof b.series.color?a.replace(j,b.series.color):a.replace(j,""),a=this.hasAxisLabel("xaxis",b)?a.replace(k,b.series.xaxis.options.axisLabel):a.replace(k,""),a=this.hasAxisLabel("yaxis",b)?a.replace(l,b.series.yaxis.options.axisLabel):a.replace(l,""),this.isTimeMode("xaxis",b)&&this.isXDateFormat(b)&&(a=a.replace(m,this.timestampToDate(c,this.tooltipOptions.xDateFormat,b.series.xaxis.options))),this.isTimeMode("yaxis",b)&&this.isYDateFormat(b)&&(a=a.replace(n,this.timestampToDate(d,this.tooltipOptions.yDateFormat,b.series.yaxis.options))),"number"==typeof c&&(a=this.adjustValPrecision(m,a,c)),"number"==typeof d&&(a=this.adjustValPrecision(n,a,d)),"undefined"!=typeof b.series.xaxis.ticks){var s;s=this.hasRotatedXAxisTicks(b)?"rotatedTicks":"ticks";var t=b.dataIndex+b.seriesIndex;for(var u in b.series.xaxis[s])if(b.series.xaxis[s].hasOwnProperty(t)&&!this.isTimeMode("xaxis",b)){var v=this.isCategoriesMode("xaxis",b)?b.series.xaxis[s][t].label:b.series.xaxis[s][t].v;v===c&&(a=a.replace(m,b.series.xaxis[s][t].label.replace(/\$/g,"$$$$")))}}if("undefined"!=typeof b.series.yaxis.ticks)for(var w in b.series.yaxis.ticks)if(b.series.yaxis.ticks.hasOwnProperty(w)){var x=this.isCategoriesMode("yaxis",b)?b.series.yaxis.ticks[w].label:b.series.yaxis.ticks[w].v;x===d&&(a=a.replace(n,b.series.yaxis.ticks[w].label.replace(/\$/g,"$$$$")))}return"undefined"!=typeof b.series.xaxis.tickFormatter&&(a=a.replace(o,b.series.xaxis.tickFormatter(c,b.series.xaxis).replace(/\$/g,"$$"))),"undefined"!=typeof b.series.yaxis.tickFormatter&&(a=a.replace(p,b.series.yaxis.tickFormatter(d,b.series.yaxis).replace(/\$/g,"$$"))),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(a){return"undefined"!=typeof this.tooltipOptions.xDateFormat&&null!==this.tooltipOptions.xDateFormat},c.prototype.isYDateFormat=function(a){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("axisLabels",this.plotPlugins)&&"undefined"!=typeof c.series[b].options.axisLabel&&c.series[b].options.axisLabel.length>0},c.prototype.hasRotatedXAxisTicks=function(b){return-1!==a.inArray("tickRotor",this.plotPlugins)&&"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.5"})}(jQuery); \ No newline at end of file +!function(a){var b={tooltip:{show:!1,cssClass:"flotTip",content:"%s | X: %x | Y: %y",xDateFormat:null,yDateFormat:null,monthNames:null,dayNames:null,shifts:{x:10,y:20},defaultTheme:!0,snap:!0,lines:!1,clickTips:!1,onHover:function(a,b){},$compat:!1}};b.tooltipOpts=b.tooltip;var 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,g){f.clickmode?(a(b.getPlaceholder()).bind("plothover",e),b.hideTooltip(),f.clickmode=!1):(e(c,d,g),f.getDomElement().is(":visible")&&(a(b.getPlaceholder()).unbind("plothover",e),f.clickmode=!0))}function e(c,d,e){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(e)b.showTooltip(e,f.tooltipOptions.snap?e:d);else if(f.plotOptions.series.lines.show&&f.tooltipOptions.lines===!0){var i=f.plotOptions.grid.mouseActiveRadius,j={distance:i+1},k=d;a.each(b.getData(),function(a,c){for(var e=0,i=-1,l=1;l=d.x&&(e=l-1,i=l);if(-1===i)return void b.hideTooltip();var m={x:c.data[e][0],y:c.data[e][1]},n={x:c.data[i][0],y:c.data[i][1]},o=h(c.xaxis.p2c(d.x),c.yaxis.p2c(d.y),c.xaxis.p2c(m.x),c.yaxis.p2c(m.y),c.xaxis.p2c(n.x),c.yaxis.p2c(n.y),!1);if(oh;h++)this.plotPlugins.push(a.plot.plugins[h].name);b.hooks.bindEvents.push(function(b,g){if(f.plotOptions=b.getOptions(),"boolean"==typeof f.plotOptions.tooltip&&(f.plotOptions.tooltipOpts.show=f.plotOptions.tooltip,f.plotOptions.tooltip=f.plotOptions.tooltipOpts,delete f.plotOptions.tooltipOpts),f.plotOptions.tooltip.show!==!1&&"undefined"!=typeof f.plotOptions.tooltip.show){f.tooltipOptions=f.plotOptions.tooltip,f.tooltipOptions.$compat?(f.wfunc="width",f.hfunc="height"):(f.wfunc="innerWidth",f.hfunc="innerHeight");f.getDomElement();a(b.getPlaceholder()).bind("plothover",e),f.tooltipOptions.clickTips&&a(b.getPlaceholder()).bind("plotclick",d),f.clickmode=!1,a(g).bind("mousemove",c)}}),b.hooks.shutdown.push(function(b,f){a(b.getPlaceholder()).unbind("plothover",e),a(b.getPlaceholder()).unbind("plotclick",d),b.removeTooltip(),a(f).unbind("mousemove",c)}),b.setTooltipPosition=function(b){var c=f.getDomElement(),d=c.outerWidth()+f.tooltipOptions.shifts.x,e=c.outerHeight()+f.tooltipOptions.shifts.y;b.x-a(window).scrollLeft()>a(window)[f.wfunc]()-d&&(b.x-=d,b.x=Math.max(b.x,0)),b.y-a(window).scrollTop()>a(window)[f.hfunc]()-e&&(b.y-=e),isNaN(b.x)?f.tipPosition.x=f.tipPosition.xPrev:(f.tipPosition.x=b.x,f.tipPosition.xPrev=b.x),isNaN(b.y)?f.tipPosition.y=f.tipPosition.yPrev:(f.tipPosition.y=b.y,f.tipPosition.yPrev=b.y)},b.showTooltip=function(a,c,d){var e=f.getDomElement(),g=f.stringFormat(f.tooltipOptions.content,a);""!==g&&(e.html(g),b.setTooltipPosition({x:f.tipPosition.x,y:f.tipPosition.y}),e.css({left:f.tipPosition.x+f.tooltipOptions.shifts.x,top:f.tipPosition.y+f.tooltipOptions.shifts.y}).show(),"function"==typeof f.tooltipOptions.onHover&&f.tooltipOptions.onHover(a,e))},b.hideTooltip=function(){f.getDomElement().hide().html("")},b.removeTooltip=function(){f.getDomElement().remove()}},c.prototype.getDomElement=function(){var b=a("
");return this.tooltipOptions&&this.tooltipOptions.cssClass&&(b=a("."+this.tooltipOptions.cssClass),0===b.length&&(b=a("
").addClass(this.tooltipOptions.cssClass),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,h=/%p\.{0,1}(\d{0,})/,i=/%s/,j=/%c/,k=/%lx/,l=/%ly/,m=/%x\.{0,1}(\d{0,})/,n=/%y\.{0,1}(\d{0,})/,o="%x",p="%y",q="%ct",r="%n";if("undefined"!=typeof b.series.threshold?(c=b.datapoint[0],d=b.datapoint[1],e=b.datapoint[2]):"undefined"!=typeof b.series.curvedLines?(c=b.datapoint[0],d=b.datapoint[1]):"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)),"boolean"==typeof a&&!a)return"";if(e&&(a=a.replace(q,e)),"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(h,a,f)),b.series.hasOwnProperty("pie")&&"undefined"!=typeof b.series.data[0][1]&&(g=b.series.data[0][1]),"number"==typeof g&&(a=a.replace(r,g)),a="undefined"!=typeof b.series.label?a.replace(i,b.series.label):a.replace(i,""),a="undefined"!=typeof b.series.color?a.replace(j,b.series.color):a.replace(j,""),a=this.hasAxisLabel("xaxis",b)?a.replace(k,b.series.xaxis.options.axisLabel):a.replace(k,""),a=this.hasAxisLabel("yaxis",b)?a.replace(l,b.series.yaxis.options.axisLabel):a.replace(l,""),this.isTimeMode("xaxis",b)&&this.isXDateFormat(b)&&(a=a.replace(m,this.timestampToDate(c,this.tooltipOptions.xDateFormat,b.series.xaxis.options))),this.isTimeMode("yaxis",b)&&this.isYDateFormat(b)&&(a=a.replace(n,this.timestampToDate(d,this.tooltipOptions.yDateFormat,b.series.yaxis.options))),"number"==typeof c&&(a=this.adjustValPrecision(m,a,c)),"number"==typeof d&&(a=this.adjustValPrecision(n,a,d)),"undefined"!=typeof b.series.xaxis.ticks){var s;s=this.hasRotatedXAxisTicks(b)?"rotatedTicks":"ticks";var t=b.dataIndex+b.seriesIndex;for(var u in b.series.xaxis[s])if(b.series.xaxis[s].hasOwnProperty(t)&&!this.isTimeMode("xaxis",b)){var v=this.isCategoriesMode("xaxis",b)?b.series.xaxis[s][t].label:b.series.xaxis[s][t].v;v===c&&(a=a.replace(m,b.series.xaxis[s][t].label.replace(/\$/g,"$$$$")))}}if("undefined"!=typeof b.series.yaxis.ticks)for(var w in b.series.yaxis.ticks)if(b.series.yaxis.ticks.hasOwnProperty(w)){var x=this.isCategoriesMode("yaxis",b)?b.series.yaxis.ticks[w].label:b.series.yaxis.ticks[w].v;x===d&&(a=a.replace(n,b.series.yaxis.ticks[w].label.replace(/\$/g,"$$$$")))}return"undefined"!=typeof b.series.xaxis.tickFormatter&&(a=a.replace(o,b.series.xaxis.tickFormatter(c,b.series.xaxis).replace(/\$/g,"$$"))),"undefined"!=typeof b.series.yaxis.tickFormatter&&(a=a.replace(p,b.series.yaxis.tickFormatter(d,b.series.yaxis).replace(/\$/g,"$$"))),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(a){return"undefined"!=typeof this.tooltipOptions.xDateFormat&&null!==this.tooltipOptions.xDateFormat},c.prototype.isYDateFormat=function(a){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("axisLabels",this.plotPlugins)&&"undefined"!=typeof c.series[b].options.axisLabel&&c.series[b].options.axisLabel.length>0},c.prototype.hasRotatedXAxisTicks=function(b){return-1!==a.inArray("tickRotor",this.plotPlugins)&&"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.5"})}(jQuery); \ No newline at end of file diff --git a/js/jquery.flot.tooltip.source.js b/js/jquery.flot.tooltip.source.js index d3720ce..0e49edc 100644 --- a/js/jquery.flot.tooltip.source.js +++ b/js/jquery.flot.tooltip.source.js @@ -14,7 +14,7 @@ // %y -> Y value, // %x.2 -> precision of X value, // %p -> percent - // %n -> value (not percent) of pie chart + // %n -> value (not percent) of pie chart xDateFormat: null, yDateFormat: null, monthNames: null, @@ -255,13 +255,13 @@ pos.y -= totalTipHeight; } - /* + /* 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 - 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. */ - + if (isNaN(pos.x)) { that.tipPosition.x = that.tipPosition.xPrev; } @@ -276,7 +276,7 @@ that.tipPosition.y = pos.y; that.tipPosition.yPrev = pos.y; } - + }; // Quick little function for showing the tooltip. @@ -289,7 +289,7 @@ return; $tip.html(tipText); - plot.setTooltipPosition({ x: position.pageX, y: position.pageY }); + plot.setTooltipPosition({ x: that.tipPosition.x, y: that.tipPosition.y }); $tip.css({ left: that.tipPosition.x + that.tooltipOptions.shifts.x, top: that.tipPosition.y + that.tooltipOptions.shifts.y @@ -323,7 +323,7 @@ if( $tip.length === 0 ){ $tip = $('
').addClass(this.tooltipOptions.cssClass); $tip.appendTo('body').hide().css({position: 'absolute'}); - + if(this.tooltipOptions.defaultTheme) { $tip.css({ 'background': '#fff', @@ -360,7 +360,7 @@ var yPatternWithoutPrecision = "%y"; var customTextPattern = "%ct"; var nPiePattern = "%n"; - + var x, y, customText, p, n; // for threshold plugin we need to read data from different place @@ -375,7 +375,7 @@ x = item.datapoint[0]; y = item.datapoint[1]; } - + else if (typeof item.series.lines !== "undefined" && item.series.lines.steps) { x = item.series.datapoints.points[item.dataIndex * 2]; y = item.series.datapoints.points[item.dataIndex * 2 + 1]; @@ -402,33 +402,34 @@ return ''; } - /* replacement of %ct and other multi-character templates must - precede the replacement of single-character templates - to avoid conflict between '%c' and '%ct' and similar substrings - */ - if (customText) + /* replacement of %ct and other multi-character templates must + precede the replacement of single-character templates + to avoid conflict between '%c' and '%ct' and similar substrings + */ + if (customText) { content = content.replace(customTextPattern, customText); + } // percent match for pie charts and stacked percent if (typeof (item.series.percent) !== 'undefined') { p = item.series.percent; } else if (typeof (item.series.percents) !== 'undefined') { p = item.series.percents[item.dataIndex]; - } + } if (typeof p === 'number') { content = this.adjustValPrecision(percentPattern, content, p); } - // replace %n with number of items represented by slice in pie charts - if (item.series.hasOwnProperty('pie')) { - if (typeof (item.series.data[0][1] !== 'undefined')) { - n = item.series.data[0][1]; - } - } - if (typeof n === 'number') { + // replace %n with number of items represented by slice in pie charts + if (item.series.hasOwnProperty('pie')) { + if (typeof item.series.data[0][1] !== 'undefined') { + n = item.series.data[0][1]; + } + } + if (typeof n === 'number') { content = content.replace(nPiePattern, n); - } - + } + // series match if (typeof(item.series.label) !== 'undefined') { content = content.replace(seriesPattern, item.series.label); @@ -436,7 +437,7 @@ //remove %s if label is undefined content = content.replace(seriesPattern, ""); } - + // color match if (typeof(item.series.color) !== 'undefined') { content = content.replace(colorPattern, item.series.color); diff --git a/package.json b/package.json index e743c05..0051557 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery.flot.tooltip", - "version": "0.8.7", + "version": "0.9.0", "description": "easy-to-use tooltips for Flot charts", "website": "https://github.com/krzysu/flot.tooltip", "directories": {