heatmap_tooltip.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import d3 from 'd3';
  3. import $ from 'jquery';
  4. import _ from 'lodash';
  5. import kbn from 'app/core/utils/kbn';
  6. import {getValueBucketBound} from './heatmap_data_converter';
  7. let TOOLTIP_PADDING_X = 30;
  8. let TOOLTIP_PADDING_Y = 5;
  9. let HISTOGRAM_WIDTH = 160;
  10. let HISTOGRAM_HEIGHT = 40;
  11. export class HeatmapTooltip {
  12. tooltip: any;
  13. scope: any;
  14. dashboard: 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.panel = scope.ctrl.panel;
  23. this.heatmapPanel = elem;
  24. this.mouseOverBucket = false;
  25. this.originalFillColor = null;
  26. elem.on("mouseover", this.onMouseOver.bind(this));
  27. elem.on("mouseleave", this.onMouseLeave.bind(this));
  28. }
  29. onMouseOver(e) {
  30. if (!this.panel.tooltip.show || !this.scope.ctrl.data || _.isEmpty(this.scope.ctrl.data.buckets)) { return; }
  31. if (!this.tooltip) {
  32. this.add();
  33. this.move(e);
  34. }
  35. }
  36. onMouseLeave() {
  37. this.destroy();
  38. }
  39. onMouseMove(e) {
  40. if (!this.panel.tooltip.show) { return; }
  41. this.move(e);
  42. }
  43. add() {
  44. this.tooltip = d3.select("body")
  45. .append("div")
  46. .attr("class", "heatmap-tooltip graph-tooltip grafana-tooltip");
  47. }
  48. destroy() {
  49. if (this.tooltip) {
  50. this.tooltip.remove();
  51. }
  52. this.tooltip = null;
  53. }
  54. show(pos, data) {
  55. if (!this.panel.tooltip.show || !data) { return; }
  56. // shared tooltip mode
  57. if (pos.panelRelY) {
  58. return;
  59. }
  60. let {xBucketIndex, yBucketIndex} = this.getBucketIndexes(pos, data);
  61. if (!data.buckets[xBucketIndex] || !this.tooltip) {
  62. this.destroy();
  63. return;
  64. }
  65. let boundBottom, boundTop, valuesNumber;
  66. let xData = data.buckets[xBucketIndex];
  67. let yData = xData.buckets[yBucketIndex];
  68. let tooltipTimeFormat = 'YYYY-MM-DD HH:mm:ss';
  69. let time = this.dashboard.formatDate(xData.x, tooltipTimeFormat);
  70. let decimals = this.panel.tooltipDecimals || 5;
  71. let valueFormatter = this.valueFormatter(decimals);
  72. let tooltipHtml = `<div class="graph-tooltip-time">${time}</div>
  73. <div class="heatmap-histogram"></div>`;
  74. if (yData) {
  75. if (yData.bounds) {
  76. boundBottom = valueFormatter(yData.bounds.bottom);
  77. boundTop = valueFormatter(yData.bounds.top);
  78. valuesNumber = yData.count;
  79. tooltipHtml += `<div>
  80. bucket: <b>${boundBottom} - ${boundTop}</b> <br>
  81. count: <b>${valuesNumber}</b> <br>
  82. </div>`;
  83. } else {
  84. // currently no bounds for pre bucketed data
  85. tooltipHtml += `<div>count: <b>${yData.count}</b><br></div>`;
  86. }
  87. } else {
  88. if (!this.panel.tooltip.showHistogram) {
  89. this.destroy();
  90. return;
  91. }
  92. boundBottom = yBucketIndex;
  93. boundTop = '';
  94. valuesNumber = 0;
  95. }
  96. this.tooltip.html(tooltipHtml);
  97. if (this.panel.tooltip.showHistogram) {
  98. this.addHistogram(xData);
  99. }
  100. this.move(pos);
  101. }
  102. getBucketIndexes(pos, data) {
  103. const xBucketIndex = this.getXBucketIndex(pos.offsetX, data);
  104. const yBucketIndex = this.getYBucketIndex(pos.offsetY, data);
  105. return {xBucketIndex, yBucketIndex};
  106. }
  107. getXBucketIndex(offsetX, data) {
  108. let x = this.scope.xScale.invert(offsetX - this.scope.yAxisWidth).valueOf();
  109. let xBucketIndex = getValueBucketBound(x, data.xBucketSize, 1);
  110. return xBucketIndex;
  111. }
  112. getYBucketIndex(offsetY, data) {
  113. let y = this.scope.yScale.invert(offsetY - this.scope.chartTop);
  114. let yBucketIndex = getValueBucketBound(y, data.yBucketSize, this.panel.yAxis.logBase);
  115. return yBucketIndex;
  116. }
  117. getSharedTooltipPos(pos) {
  118. // get pageX from position on x axis and pageY from relative position in original panel
  119. pos.pageX = this.heatmapPanel.offset().left + this.scope.xScale(pos.x);
  120. pos.pageY = this.heatmapPanel.offset().top + this.scope.chartHeight * pos.panelRelY;
  121. return pos;
  122. }
  123. addHistogram(data) {
  124. let xBucket = this.scope.ctrl.data.buckets[data.x];
  125. let yBucketSize = this.scope.ctrl.data.yBucketSize;
  126. let {min, max, ticks} = this.scope.ctrl.data.yAxis;
  127. let histogramData = _.map(xBucket.buckets, bucket => {
  128. return [bucket.y, bucket.values.length];
  129. });
  130. histogramData = _.filter(histogramData, d => {
  131. return d[0] >= min && d[0] <= max;
  132. });
  133. let scale = this.scope.yScale.copy();
  134. let histXScale = scale
  135. .domain([min, max])
  136. .range([0, HISTOGRAM_WIDTH]);
  137. let barWidth;
  138. if (this.panel.yAxis.logBase === 1) {
  139. barWidth = Math.floor(HISTOGRAM_WIDTH / (max - min) * yBucketSize * 0.9);
  140. } else {
  141. barWidth = Math.floor(HISTOGRAM_WIDTH / ticks / yBucketSize * 0.9);
  142. }
  143. barWidth = Math.max(barWidth, 1);
  144. let histYScale = d3.scaleLinear()
  145. .domain([0, _.max(_.map(histogramData, d => d[1]))])
  146. .range([0, HISTOGRAM_HEIGHT]);
  147. let histogram = this.tooltip.select(".heatmap-histogram")
  148. .append("svg")
  149. .attr("width", HISTOGRAM_WIDTH)
  150. .attr("height", HISTOGRAM_HEIGHT);
  151. histogram.selectAll(".bar").data(histogramData)
  152. .enter().append("rect")
  153. .attr("x", d => {
  154. return histXScale(d[0]);
  155. })
  156. .attr("width", barWidth)
  157. .attr("y", d => {
  158. return HISTOGRAM_HEIGHT - histYScale(d[1]);
  159. })
  160. .attr("height", d => {
  161. return histYScale(d[1]);
  162. });
  163. }
  164. move(pos) {
  165. if (!this.tooltip) { return; }
  166. let elem = $(this.tooltip.node())[0];
  167. let tooltipWidth = elem.clientWidth;
  168. let tooltipHeight = elem.clientHeight;
  169. let left = pos.pageX + TOOLTIP_PADDING_X;
  170. let top = pos.pageY + TOOLTIP_PADDING_Y;
  171. if (pos.pageX + tooltipWidth + 40 > window.innerWidth) {
  172. left = pos.pageX - tooltipWidth - TOOLTIP_PADDING_X;
  173. }
  174. if (pos.pageY - window.pageYOffset + tooltipHeight + 20 > window.innerHeight) {
  175. top = pos.pageY - tooltipHeight - TOOLTIP_PADDING_Y;
  176. }
  177. return this.tooltip
  178. .style("left", left + "px")
  179. .style("top", top + "px");
  180. }
  181. valueFormatter(decimals) {
  182. let format = this.panel.yAxis.format;
  183. return function(value) {
  184. if (_.isInteger(value)) {
  185. decimals = 0;
  186. }
  187. return kbn.valueFormats[format](value, decimals);
  188. };
  189. }
  190. }