graph_tooltip.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. define([
  2. 'jquery',
  3. 'lodash'
  4. ],
  5. function ($) {
  6. 'use strict';
  7. function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
  8. var self = this;
  9. var ctrl = scope.ctrl;
  10. var panel = ctrl.panel;
  11. var $tooltip = $('<div id="tooltip" class="graph-tooltip">');
  12. this.destroy = function() {
  13. $tooltip.remove();
  14. };
  15. this.findHoverIndexFromDataPoints = function(posX, series, last) {
  16. var ps = series.datapoints.pointsize;
  17. var initial = last*ps;
  18. var len = series.datapoints.points.length;
  19. for (var j = initial; j < len; j += ps) {
  20. if (series.datapoints.points[j] > posX) {
  21. break;
  22. }
  23. }
  24. // Special case of a non stepped line, highlight the very last point just before a null point
  25. while(!series.lines.steps && series.datapoints.points[initial] != null && j>0 && series.datapoints.points[j-ps] == null) {
  26. j-=ps;
  27. }
  28. return Math.max(j - ps, 0)/ps;
  29. };
  30. this.findHoverIndexFromData = function(posX, series) {
  31. var len = series.data.length;
  32. for (var j = 0; j < len; j++) {
  33. if (series.data[j][0] > posX) {
  34. return Math.max(j - 1, 0);
  35. }
  36. }
  37. return j - 1;
  38. };
  39. this.showTooltip = function(absoluteTime, innerHtml, pos) {
  40. var body = '<div class="graph-tooltip-time">'+ absoluteTime + '</div>';
  41. body += innerHtml + '</div>';
  42. $tooltip.html(body).place_tt(pos.pageX + 20, pos.pageY);
  43. };
  44. this.getMultiSeriesPlotHoverInfo = function(seriesList, pos) {
  45. var value, i, series, hoverIndex, hoverDistance, pointTime, yaxis;
  46. var results = [];
  47. //now we know the current X (j) position for X and Y values
  48. var last_value = 0; //needed for stacked values
  49. var minDistance, minTime;
  50. for (i = 0; i < seriesList.length; i++) {
  51. series = seriesList[i];
  52. if (!series.data.length || (panel.legend.hideEmpty && series.allIsNull)) {
  53. // Init value & yaxis so that it does not brake series sorting
  54. results.push({ hidden: true, value: 0, yaxis: 0 });
  55. continue;
  56. }
  57. if (!series.data.length || (panel.legend.hideZero && series.allIsZero)) {
  58. // Init value & yaxis so that it does not brake series sorting
  59. results.push({ hidden: true, value: 0, yaxis: 0 });
  60. continue;
  61. }
  62. hoverIndex = this.findHoverIndexFromData(pos.x, series);
  63. hoverDistance = pos.x - series.data[hoverIndex][0];
  64. pointTime = series.data[hoverIndex][0];
  65. // Take the closest point before the cursor, or if it does not exist, the closest after
  66. if (! minDistance
  67. || (hoverDistance >=0 && (hoverDistance < minDistance || minDistance < 0))
  68. || (hoverDistance < 0 && hoverDistance > minDistance)) {
  69. minDistance = hoverDistance;
  70. minTime = pointTime;
  71. }
  72. if (series.stack) {
  73. if (panel.tooltip.value_type === 'individual') {
  74. value = series.data[hoverIndex][1];
  75. } else if (!series.stack) {
  76. value = series.data[hoverIndex][1];
  77. } else {
  78. last_value += series.data[hoverIndex][1];
  79. value = last_value;
  80. }
  81. } else {
  82. value = series.data[hoverIndex][1];
  83. }
  84. // Highlighting multiple Points depending on the plot type
  85. if (series.lines.steps || series.stack) {
  86. // stacked and steppedLine plots can have series with different length.
  87. // Stacked series can increase its length on each new stacked serie if null points found,
  88. // to speed the index search we begin always on the last found hoverIndex.
  89. hoverIndex = this.findHoverIndexFromDataPoints(pos.x, series, hoverIndex);
  90. }
  91. // Be sure we have a yaxis so that it does not brake series sorting
  92. yaxis = 0;
  93. if (series.yaxis) {
  94. yaxis = series.yaxis.n;
  95. }
  96. results.push({
  97. value: value,
  98. hoverIndex: hoverIndex,
  99. color: series.color,
  100. label: series.label,
  101. time: pointTime,
  102. distance: hoverDistance,
  103. yaxis: yaxis,
  104. index: i
  105. });
  106. }
  107. // Time of the point closer to pointer
  108. results.time = minTime;
  109. return results;
  110. };
  111. elem.mouseleave(function () {
  112. if (panel.tooltip.shared) {
  113. var plot = elem.data().plot;
  114. if (plot) {
  115. $tooltip.detach();
  116. plot.unhighlight();
  117. }
  118. }
  119. if (dashboard.sharedCrosshair) {
  120. ctrl.publishAppEvent('clearCrosshair');
  121. }
  122. });
  123. elem.bind("plothover", function (event, pos, item) {
  124. var plot = elem.data().plot;
  125. var plotData = plot.getData();
  126. var seriesList = getSeriesFn();
  127. var group, value, absoluteTime, hoverInfo, i, series, seriesHtml, tooltipFormat;
  128. if (dashboard.sharedCrosshair) {
  129. ctrl.publishAppEvent('setCrosshair', {pos: pos, scope: scope});
  130. }
  131. if (seriesList.length === 0) {
  132. return;
  133. }
  134. if (seriesList[0].hasMsResolution) {
  135. tooltipFormat = 'YYYY-MM-DD HH:mm:ss.SSS';
  136. } else {
  137. tooltipFormat = 'YYYY-MM-DD HH:mm:ss';
  138. }
  139. if (panel.tooltip.shared) {
  140. plot.unhighlight();
  141. var seriesHoverInfo = self.getMultiSeriesPlotHoverInfo(plotData, pos);
  142. seriesHtml = '';
  143. absoluteTime = dashboard.formatDate(seriesHoverInfo.time, tooltipFormat);
  144. // Dynamically reorder the hovercard for the current time point if the
  145. // option is enabled, sort by yaxis by default.
  146. if (panel.tooltip.sort === 2) {
  147. seriesHoverInfo.sort(function(a, b) {
  148. return b.value - a.value;
  149. });
  150. } else if (panel.tooltip.sort === 1) {
  151. seriesHoverInfo.sort(function(a, b) {
  152. return a.value - b.value;
  153. });
  154. } else {
  155. seriesHoverInfo.sort(function(a, b) {
  156. return a.yaxis - b.yaxis;
  157. });
  158. }
  159. for (i = 0; i < seriesHoverInfo.length; i++) {
  160. hoverInfo = seriesHoverInfo[i];
  161. if (hoverInfo.hidden) {
  162. continue;
  163. }
  164. var highlightClass = '';
  165. if (item && hoverInfo.index === item.seriesIndex) {
  166. highlightClass = 'graph-tooltip-list-item--highlight';
  167. }
  168. series = seriesList[hoverInfo.index];
  169. value = series.formatValue(hoverInfo.value);
  170. seriesHtml += '<div class="graph-tooltip-list-item ' + highlightClass + '"><div class="graph-tooltip-series-name">';
  171. seriesHtml += '<i class="fa fa-minus" style="color:' + hoverInfo.color +';"></i> ' + hoverInfo.label + ':</div>';
  172. seriesHtml += '<div class="graph-tooltip-value">' + value + '</div></div>';
  173. plot.highlight(hoverInfo.index, hoverInfo.hoverIndex);
  174. }
  175. self.showTooltip(absoluteTime, seriesHtml, pos);
  176. }
  177. // single series tooltip
  178. else if (item) {
  179. series = seriesList[item.seriesIndex];
  180. group = '<div class="graph-tooltip-list-item"><div class="graph-tooltip-series-name">';
  181. group += '<i class="fa fa-minus" style="color:' + item.series.color +';"></i> ' + series.label + ':</div>';
  182. if (panel.stack && panel.tooltip.value_type === 'individual') {
  183. value = item.datapoint[1] - item.datapoint[2];
  184. }
  185. else {
  186. value = item.datapoint[1];
  187. }
  188. value = series.formatValue(value);
  189. absoluteTime = dashboard.formatDate(item.datapoint[0], tooltipFormat);
  190. group += '<div class="graph-tooltip-value">' + value + '</div>';
  191. self.showTooltip(absoluteTime, group, pos);
  192. }
  193. // no hit
  194. else {
  195. $tooltip.detach();
  196. }
  197. });
  198. }
  199. return GraphTooltip;
  200. });