heatmap_tooltip.ts 6.6 KB

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