heatmap_tooltip.ts 8.3 KB

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