heatmap_tooltip.ts 6.4 KB

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