/modules/backend/assets/vendor/flot/jquery.flot.tooltip.js

https://github.com/demiron/appbet · JavaScript · 251 lines · 156 code · 43 blank · 52 comment · 26 complexity · 149701304d10d12a9fc65040f3bcc659 MD5 · raw file

  1. /*
  2. * jquery.flot.tooltip
  3. *
  4. * description: easy-to-use tooltips for Flot charts
  5. * version: 0.6.1
  6. * author: Krzysztof Urbas @krzysu [myviews.pl]
  7. * website: https://github.com/krzysu/flot.tooltip
  8. *
  9. * build on 2013-04-12
  10. * released under MIT License, 2012
  11. */
  12. (function ($) {
  13. // plugin options, default values
  14. var defaultOptions = {
  15. tooltip: false,
  16. tooltipOpts: {
  17. content: "%s | X: %x | Y: %y",
  18. // allowed templates are:
  19. // %s -> series label,
  20. // %x -> X value,
  21. // %y -> Y value,
  22. // %x.2 -> precision of X value,
  23. // %p -> percent
  24. xDateFormat: null,
  25. yDateFormat: null,
  26. shifts: {
  27. x: 10,
  28. y: 20
  29. },
  30. defaultTheme: true,
  31. // callbacks
  32. onHover: function(flotItem, $tooltipEl) {}
  33. }
  34. };
  35. // object
  36. var FlotTooltip = function(plot) {
  37. // variables
  38. this.tipPosition = {x: 0, y: 0};
  39. this.init(plot);
  40. };
  41. // main plugin function
  42. FlotTooltip.prototype.init = function(plot) {
  43. var that = this;
  44. plot.hooks.bindEvents.push(function (plot, eventHolder) {
  45. // get plot options
  46. that.plotOptions = plot.getOptions();
  47. // if not enabled return
  48. if (that.plotOptions.tooltip === false || typeof that.plotOptions.tooltip === 'undefined') return;
  49. // shortcut to access tooltip options
  50. that.tooltipOptions = that.plotOptions.tooltipOpts;
  51. // create tooltip DOM element
  52. var $tip = that.getDomElement();
  53. // bind event
  54. $( plot.getPlaceholder() ).bind("plothover", function (event, pos, item) {
  55. if (item) {
  56. var tipText;
  57. // convert tooltip content template to real tipText
  58. tipText = that.stringFormat(that.tooltipOptions.content, item);
  59. $tip.html( tipText );
  60. that.updateTooltipPosition({ x: pos.pageX, y: pos.pageY });
  61. $tip.css({
  62. left: that.tipPosition.x + that.tooltipOptions.shifts.x,
  63. top: that.tipPosition.y + that.tooltipOptions.shifts.y
  64. })
  65. .show();
  66. // run callback
  67. if(typeof that.tooltipOptions.onHover === 'function') {
  68. that.tooltipOptions.onHover(item, $tip);
  69. }
  70. }
  71. else {
  72. $tip.hide().html('');
  73. }
  74. });
  75. eventHolder.mousemove( function(e) {
  76. var pos = {};
  77. pos.x = e.pageX;
  78. pos.y = e.pageY;
  79. that.updateTooltipPosition(pos);
  80. });
  81. });
  82. };
  83. /**
  84. * get or create tooltip DOM element
  85. * @return jQuery object
  86. */
  87. FlotTooltip.prototype.getDomElement = function() {
  88. var $tip;
  89. if( $('#flotTip').length > 0 ){
  90. $tip = $('#flotTip');
  91. }
  92. else {
  93. $tip = $('<div />').attr('id', 'flotTip');
  94. $tip.appendTo('body').hide().css({position: 'absolute'});
  95. if(this.tooltipOptions.defaultTheme) {
  96. $tip.css({
  97. 'background': '#fff',
  98. 'z-index': '100',
  99. 'padding': '0.4em 0.6em',
  100. 'border-radius': '0.5em',
  101. 'font-size': '0.8em',
  102. 'border': '1px solid #111',
  103. 'display': 'inline-block',
  104. 'white-space': 'nowrap'
  105. });
  106. }
  107. }
  108. return $tip;
  109. };
  110. // as the name says
  111. FlotTooltip.prototype.updateTooltipPosition = function(pos) {
  112. var totalTipWidth = $("#flotTip").outerWidth() + this.tooltipOptions.shifts.x;
  113. var totalTipHeight = $("#flotTip").outerHeight() + this.tooltipOptions.shifts.y;
  114. if ((pos.x - $(window).scrollLeft()) > ($(window).innerWidth() - totalTipWidth)) {
  115. pos.x -= totalTipWidth;
  116. }
  117. if ((pos.y - $(window).scrollTop()) > ($(window).innerHeight() - totalTipHeight)) {
  118. pos.y -= totalTipHeight;
  119. }
  120. this.tipPosition.x = pos.x;
  121. this.tipPosition.y = pos.y;
  122. };
  123. /**
  124. * core function, create tooltip content
  125. * @param {string} content - template with tooltip content
  126. * @param {object} item - Flot item
  127. * @return {string} real tooltip content for current item
  128. */
  129. FlotTooltip.prototype.stringFormat = function(content, item) {
  130. var percentPattern = /%p\.{0,1}(\d{0,})/;
  131. var seriesPattern = /%s/;
  132. var xPattern = /%x\.{0,1}(\d{0,})/;
  133. var yPattern = /%y\.{0,1}(\d{0,})/;
  134. // if it is a function callback get the content string
  135. if( typeof(content) === 'function' ) {
  136. content = content(item.series.data[item.dataIndex][0], item.series.data[item.dataIndex][1]);
  137. }
  138. // percent match for pie charts
  139. if( typeof (item.series.percent) !== 'undefined' ) {
  140. content = this.adjustValPrecision(percentPattern, content, item.series.percent);
  141. }
  142. // series match
  143. if( typeof(item.series.label) !== 'undefined' ) {
  144. content = content.replace(seriesPattern, item.series.label);
  145. }
  146. // time mode axes with custom dateFormat
  147. if(this.isTimeMode('xaxis', item) && this.isXDateFormat(item)) {
  148. content = content.replace(xPattern, this.timestampToDate(item.series.data[item.dataIndex][0], this.tooltipOptions.xDateFormat));
  149. }
  150. if(this.isTimeMode('yaxis', item) && this.isYDateFormat(item)) {
  151. content = content.replace(yPattern, this.timestampToDate(item.series.data[item.dataIndex][1], this.tooltipOptions.yDateFormat));
  152. }
  153. // set precision if defined
  154. if( typeof item.series.data[item.dataIndex][0] === 'number' ) {
  155. content = this.adjustValPrecision(xPattern, content, item.series.data[item.dataIndex][0]);
  156. }
  157. if( typeof item.series.data[item.dataIndex][1] === 'number' ) {
  158. content = this.adjustValPrecision(yPattern, content, item.series.data[item.dataIndex][1]);
  159. }
  160. // if no value customization, use tickFormatter by default
  161. if(typeof item.series.xaxis.tickFormatter !== 'undefined') {
  162. content = content.replace(xPattern, item.series.xaxis.tickFormatter(item.series.data[item.dataIndex][0], item.series.xaxis));
  163. }
  164. if(typeof item.series.yaxis.tickFormatter !== 'undefined') {
  165. content = content.replace(yPattern, item.series.yaxis.tickFormatter(item.series.data[item.dataIndex][1], item.series.yaxis));
  166. }
  167. return content;
  168. };
  169. // helpers just for readability
  170. FlotTooltip.prototype.isTimeMode = function(axisName, item) {
  171. return (typeof item.series[axisName].options.mode !== 'undefined' && item.series[axisName].options.mode === 'time');
  172. };
  173. FlotTooltip.prototype.isXDateFormat = function(item) {
  174. return (typeof this.tooltipOptions.xDateFormat !== 'undefined' && this.tooltipOptions.xDateFormat !== null);
  175. };
  176. FlotTooltip.prototype.isYDateFormat = function(item) {
  177. return (typeof this.tooltipOptions.yDateFormat !== 'undefined' && this.tooltipOptions.yDateFormat !== null);
  178. };
  179. //
  180. FlotTooltip.prototype.timestampToDate = function(tmst, dateFormat) {
  181. var theDate = new Date(tmst);
  182. return $.plot.formatDate(theDate, dateFormat);
  183. };
  184. //
  185. FlotTooltip.prototype.adjustValPrecision = function(pattern, content, value) {
  186. var precision;
  187. if( content.match(pattern) !== null ) {
  188. if(RegExp.$1 !== '') {
  189. precision = RegExp.$1;
  190. value = value.toFixed(precision);
  191. // only replace content if precision exists
  192. content = content.replace(pattern, value);
  193. }
  194. }
  195. return content;
  196. };
  197. //
  198. var init = function(plot) {
  199. new FlotTooltip(plot);
  200. };
  201. // define Flot plugin
  202. $.plot.plugins.push({
  203. init: init,
  204. options: defaultOptions,
  205. name: 'tooltip',
  206. version: '0.6.1'
  207. });
  208. })(jQuery);