heatmap_tooltip.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. // Decimals override. Code from panel/graph/graph.ts
  73. let valueFormatter;
  74. if (_.isNumber(this.panel.tooltipDecimals)) {
  75. valueFormatter = this.valueFormatter(this.panel.tooltipDecimals, null);
  76. } else {
  77. // auto decimals
  78. // legend and tooltip gets one more decimal precision
  79. // than graph legend ticks
  80. let decimals = (this.panelCtrl.decimals || -1) + 1;
  81. valueFormatter = this.valueFormatter(decimals, this.panelCtrl.scaledDecimals + 2);
  82. }
  83. let tooltipHtml = `<div class="graph-tooltip-time">${time}</div>
  84. <div class="heatmap-histogram"></div>`;
  85. if (yData) {
  86. if (yData.bounds) {
  87. boundBottom = valueFormatter(yData.bounds.bottom);
  88. boundTop = valueFormatter(yData.bounds.top);
  89. valuesNumber = yData.count;
  90. tooltipHtml += `<div>
  91. bucket: <b>${boundBottom} - ${boundTop}</b> <br>
  92. count: <b>${valuesNumber}</b> <br>
  93. </div>`;
  94. } else {
  95. // currently no bounds for pre bucketed data
  96. tooltipHtml += `<div>count: <b>${yData.count}</b><br></div>`;
  97. }
  98. } else {
  99. if (!this.panel.tooltip.showHistogram) {
  100. this.destroy();
  101. return;
  102. }
  103. boundBottom = yBucketIndex;
  104. boundTop = '';
  105. valuesNumber = 0;
  106. }
  107. this.tooltip.html(tooltipHtml);
  108. if (this.panel.tooltip.showHistogram) {
  109. this.addHistogram(xData);
  110. }
  111. this.move(pos);
  112. }
  113. getBucketIndexes(pos, data) {
  114. const xBucketIndex = this.getXBucketIndex(pos.offsetX, data);
  115. const yBucketIndex = this.getYBucketIndex(pos.offsetY, data);
  116. return {xBucketIndex, yBucketIndex};
  117. }
  118. getXBucketIndex(offsetX, data) {
  119. let x = this.scope.xScale.invert(offsetX - this.scope.yAxisWidth).valueOf();
  120. let xBucketIndex = getValueBucketBound(x, data.xBucketSize, 1);
  121. return xBucketIndex;
  122. }
  123. getYBucketIndex(offsetY, data) {
  124. let y = this.scope.yScale.invert(offsetY - this.scope.chartTop);
  125. let yBucketIndex = getValueBucketBound(y, data.yBucketSize, this.panel.yAxis.logBase);
  126. return yBucketIndex;
  127. }
  128. getSharedTooltipPos(pos) {
  129. // get pageX from position on x axis and pageY from relative position in original panel
  130. pos.pageX = this.heatmapPanel.offset().left + this.scope.xScale(pos.x);
  131. pos.pageY = this.heatmapPanel.offset().top + this.scope.chartHeight * pos.panelRelY;
  132. return pos;
  133. }
  134. addHistogram(data) {
  135. let xBucket = this.scope.ctrl.data.buckets[data.x];
  136. let yBucketSize = this.scope.ctrl.data.yBucketSize;
  137. let {min, max, ticks} = this.scope.ctrl.data.yAxis;
  138. let histogramData = _.map(xBucket.buckets, bucket => {
  139. return [bucket.y, bucket.values.length];
  140. });
  141. histogramData = _.filter(histogramData, d => {
  142. return d[0] >= min && d[0] <= max;
  143. });
  144. let scale = this.scope.yScale.copy();
  145. let histXScale = scale
  146. .domain([min, max])
  147. .range([0, HISTOGRAM_WIDTH]);
  148. let barWidth;
  149. if (this.panel.yAxis.logBase === 1) {
  150. barWidth = Math.floor(HISTOGRAM_WIDTH / (max - min) * yBucketSize * 0.9);
  151. } else {
  152. barWidth = Math.floor(HISTOGRAM_WIDTH / ticks / yBucketSize * 0.9);
  153. }
  154. barWidth = Math.max(barWidth, 1);
  155. // Normalize histogram Y axis
  156. let histogramDomain = _.reduce(_.map(histogramData, d => d[1]), (sum, val) => sum + val, 0);
  157. let histYScale = d3.scaleLinear()
  158. .domain([0, histogramDomain])
  159. .range([0, HISTOGRAM_HEIGHT]);
  160. let histogram = this.tooltip.select(".heatmap-histogram")
  161. .append("svg")
  162. .attr("width", HISTOGRAM_WIDTH)
  163. .attr("height", HISTOGRAM_HEIGHT);
  164. histogram.selectAll(".bar").data(histogramData)
  165. .enter().append("rect")
  166. .attr("x", d => {
  167. return histXScale(d[0]);
  168. })
  169. .attr("width", barWidth)
  170. .attr("y", d => {
  171. return HISTOGRAM_HEIGHT - histYScale(d[1]);
  172. })
  173. .attr("height", d => {
  174. return histYScale(d[1]);
  175. });
  176. }
  177. move(pos) {
  178. if (!this.tooltip) { return; }
  179. let elem = $(this.tooltip.node())[0];
  180. let tooltipWidth = elem.clientWidth;
  181. let tooltipHeight = elem.clientHeight;
  182. let left = pos.pageX + TOOLTIP_PADDING_X;
  183. let top = pos.pageY + TOOLTIP_PADDING_Y;
  184. if (pos.pageX + tooltipWidth + 40 > window.innerWidth) {
  185. left = pos.pageX - tooltipWidth - TOOLTIP_PADDING_X;
  186. }
  187. if (pos.pageY - window.pageYOffset + tooltipHeight + 20 > window.innerHeight) {
  188. top = pos.pageY - tooltipHeight - TOOLTIP_PADDING_Y;
  189. }
  190. return this.tooltip
  191. .style("left", left + "px")
  192. .style("top", top + "px");
  193. }
  194. valueFormatter(decimals, scaledDecimals = null) {
  195. let format = this.panel.yAxis.format;
  196. return function(value) {
  197. return kbn.valueFormats[format](value, decimals, scaledDecimals);
  198. };
  199. }
  200. }