heatmap_tooltip.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import * as d3 from 'd3';
  2. import $ from 'jquery';
  3. import _ from 'lodash';
  4. import kbn from 'app/core/utils/kbn';
  5. import { getValueBucketBound } from './heatmap_data_converter';
  6. const TOOLTIP_PADDING_X = 30;
  7. const TOOLTIP_PADDING_Y = 5;
  8. const HISTOGRAM_WIDTH = 160;
  9. const HISTOGRAM_HEIGHT = 40;
  10. export class HeatmapTooltip {
  11. tooltip: any;
  12. scope: any;
  13. dashboard: any;
  14. panelCtrl: any;
  15. panel: any;
  16. heatmapPanel: any;
  17. mouseOverBucket: boolean;
  18. originalFillColor: any;
  19. constructor(elem, scope) {
  20. this.scope = scope;
  21. this.dashboard = scope.ctrl.dashboard;
  22. this.panelCtrl = scope.ctrl;
  23. this.panel = scope.ctrl.panel;
  24. this.heatmapPanel = elem;
  25. this.mouseOverBucket = false;
  26. this.originalFillColor = null;
  27. elem.on('mouseleave', this.onMouseLeave.bind(this));
  28. }
  29. onMouseLeave() {
  30. this.destroy();
  31. }
  32. onMouseMove(e) {
  33. if (!this.panel.tooltip.show) {
  34. return;
  35. }
  36. this.move(e);
  37. }
  38. add() {
  39. this.tooltip = d3
  40. .select('body')
  41. .append('div')
  42. .attr('class', 'heatmap-tooltip graph-tooltip grafana-tooltip');
  43. }
  44. destroy() {
  45. if (this.tooltip) {
  46. this.tooltip.remove();
  47. }
  48. this.tooltip = null;
  49. }
  50. show(pos, data) {
  51. if (!this.panel.tooltip.show || !data) {
  52. return;
  53. }
  54. // shared tooltip mode
  55. if (pos.panelRelY) {
  56. return;
  57. }
  58. const { xBucketIndex, yBucketIndex } = this.getBucketIndexes(pos, data);
  59. if (!data.buckets[xBucketIndex]) {
  60. this.destroy();
  61. return;
  62. }
  63. if (!this.tooltip) {
  64. this.add();
  65. }
  66. let boundBottom, boundTop, valuesNumber;
  67. const xData = data.buckets[xBucketIndex];
  68. // Search in special 'zero' bucket also
  69. const yData = _.find(xData.buckets, (bucket, bucketIndex) => {
  70. return bucket.bounds.bottom === yBucketIndex || bucketIndex === yBucketIndex.toString();
  71. });
  72. const tooltipTimeFormat = 'YYYY-MM-DD HH:mm:ss';
  73. const time = this.dashboard.formatDate(xData.x, tooltipTimeFormat);
  74. // Decimals override. Code from panel/graph/graph.ts
  75. let countValueFormatter, bucketBoundFormatter;
  76. if (_.isNumber(this.panel.tooltipDecimals)) {
  77. countValueFormatter = this.countValueFormatter(this.panel.tooltipDecimals, null);
  78. bucketBoundFormatter = this.panelCtrl.tickValueFormatter(this.panelCtrl.decimals, null);
  79. } else {
  80. // auto decimals
  81. // legend and tooltip gets one more decimal precision
  82. // than graph legend ticks
  83. const decimals = (this.panelCtrl.decimals || -1) + 1;
  84. countValueFormatter = this.countValueFormatter(decimals, this.panelCtrl.scaledDecimals + 2);
  85. bucketBoundFormatter = this.panelCtrl.tickValueFormatter(decimals, this.panelCtrl.scaledDecimals + 2);
  86. }
  87. let tooltipHtml = `<div class="graph-tooltip-time">${time}</div>
  88. <div class="heatmap-histogram"></div>`;
  89. if (yData) {
  90. if (yData.bounds) {
  91. if (data.tsBuckets) {
  92. // Use Y-axis labels
  93. const tickFormatter = valIndex => {
  94. return data.tsBucketsFormatted ? data.tsBucketsFormatted[valIndex] : data.tsBuckets[valIndex];
  95. };
  96. boundBottom = tickFormatter(yBucketIndex);
  97. boundTop = yBucketIndex < data.tsBuckets.length - 1 ? tickFormatter(yBucketIndex + 1) : '';
  98. } else {
  99. // Display 0 if bucket is a special 'zero' bucket
  100. const bottom = yData.y ? yData.bounds.bottom : 0;
  101. boundBottom = bucketBoundFormatter(bottom);
  102. boundTop = bucketBoundFormatter(yData.bounds.top);
  103. }
  104. valuesNumber = countValueFormatter(yData.count);
  105. tooltipHtml += `<div>
  106. bucket: <b>${boundBottom} - ${boundTop}</b> <br>
  107. count: <b>${valuesNumber}</b> <br>
  108. </div>`;
  109. } else {
  110. // currently no bounds for pre bucketed data
  111. tooltipHtml += `<div>count: <b>${yData.count}</b><br></div>`;
  112. }
  113. } else {
  114. if (!this.panel.tooltip.showHistogram) {
  115. this.destroy();
  116. return;
  117. }
  118. boundBottom = yBucketIndex;
  119. boundTop = '';
  120. valuesNumber = 0;
  121. }
  122. this.tooltip.html(tooltipHtml);
  123. if (this.panel.tooltip.showHistogram) {
  124. this.addHistogram(xData);
  125. }
  126. this.move(pos);
  127. }
  128. getBucketIndexes(pos, data) {
  129. const xBucketIndex = this.getXBucketIndex(pos.x, data);
  130. const yBucketIndex = this.getYBucketIndex(pos.y, data);
  131. return { xBucketIndex, yBucketIndex };
  132. }
  133. getXBucketIndex(x, data) {
  134. // First try to find X bucket by checking x pos is in the
  135. // [bucket.x, bucket.x + xBucketSize] interval
  136. const xBucket = _.find(data.buckets, bucket => {
  137. return x > bucket.x && x - bucket.x <= data.xBucketSize;
  138. });
  139. return xBucket ? xBucket.x : getValueBucketBound(x, data.xBucketSize, 1);
  140. }
  141. getYBucketIndex(y, data) {
  142. if (data.tsBuckets) {
  143. return Math.floor(y);
  144. }
  145. const yBucketIndex = getValueBucketBound(y, data.yBucketSize, this.panel.yAxis.logBase);
  146. return yBucketIndex;
  147. }
  148. getSharedTooltipPos(pos) {
  149. // get pageX from position on x axis and pageY from relative position in original panel
  150. pos.pageX = this.heatmapPanel.offset().left + this.scope.xScale(pos.x);
  151. pos.pageY = this.heatmapPanel.offset().top + this.scope.chartHeight * pos.panelRelY;
  152. return pos;
  153. }
  154. addHistogram(data) {
  155. const xBucket = this.scope.ctrl.data.buckets[data.x];
  156. const yBucketSize = this.scope.ctrl.data.yBucketSize;
  157. let min, max, ticks;
  158. if (this.scope.ctrl.data.tsBuckets) {
  159. min = 0;
  160. max = this.scope.ctrl.data.tsBuckets.length - 1;
  161. ticks = this.scope.ctrl.data.tsBuckets.length;
  162. } else {
  163. min = this.scope.ctrl.data.yAxis.min;
  164. max = this.scope.ctrl.data.yAxis.max;
  165. ticks = this.scope.ctrl.data.yAxis.ticks;
  166. }
  167. let histogramData = _.map(xBucket.buckets, bucket => {
  168. const count = bucket.count !== undefined ? bucket.count : bucket.values.length;
  169. return [bucket.bounds.bottom, count];
  170. });
  171. histogramData = _.filter(histogramData, d => {
  172. return d[0] >= min && d[0] <= max;
  173. });
  174. const scale = this.scope.yScale.copy();
  175. const histXScale = scale.domain([min, max]).range([0, HISTOGRAM_WIDTH]);
  176. let barWidth;
  177. if (this.panel.yAxis.logBase === 1) {
  178. barWidth = Math.floor(HISTOGRAM_WIDTH / (max - min) * yBucketSize * 0.9);
  179. } else {
  180. const barNumberFactor = yBucketSize ? yBucketSize : 1;
  181. barWidth = Math.floor(HISTOGRAM_WIDTH / ticks / barNumberFactor * 0.9);
  182. }
  183. barWidth = Math.max(barWidth, 1);
  184. // Normalize histogram Y axis
  185. const histogramDomain = _.reduce(_.map(histogramData, d => d[1]), (sum, val) => sum + val, 0);
  186. const histYScale = d3
  187. .scaleLinear()
  188. .domain([0, histogramDomain])
  189. .range([0, HISTOGRAM_HEIGHT]);
  190. const histogram = this.tooltip
  191. .select('.heatmap-histogram')
  192. .append('svg')
  193. .attr('width', HISTOGRAM_WIDTH)
  194. .attr('height', HISTOGRAM_HEIGHT);
  195. histogram
  196. .selectAll('.bar')
  197. .data(histogramData)
  198. .enter()
  199. .append('rect')
  200. .attr('x', d => {
  201. return histXScale(d[0]);
  202. })
  203. .attr('width', barWidth)
  204. .attr('y', d => {
  205. return HISTOGRAM_HEIGHT - histYScale(d[1]);
  206. })
  207. .attr('height', d => {
  208. return histYScale(d[1]);
  209. });
  210. }
  211. move(pos) {
  212. if (!this.tooltip) {
  213. return;
  214. }
  215. const elem = $(this.tooltip.node())[0];
  216. const tooltipWidth = elem.clientWidth;
  217. const tooltipHeight = elem.clientHeight;
  218. let left = pos.pageX + TOOLTIP_PADDING_X;
  219. let top = pos.pageY + TOOLTIP_PADDING_Y;
  220. if (pos.pageX + tooltipWidth + 40 > window.innerWidth) {
  221. left = pos.pageX - tooltipWidth - TOOLTIP_PADDING_X;
  222. }
  223. if (pos.pageY - window.pageYOffset + tooltipHeight + 20 > window.innerHeight) {
  224. top = pos.pageY - tooltipHeight - TOOLTIP_PADDING_Y;
  225. }
  226. return this.tooltip.style('left', left + 'px').style('top', top + 'px');
  227. }
  228. countValueFormatter(decimals, scaledDecimals = null) {
  229. const format = 'short';
  230. return value => {
  231. return kbn.valueFormats[format](value, decimals, scaledDecimals);
  232. };
  233. }
  234. }