rendering.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import moment from 'moment';
  4. import kbn from 'app/core/utils/kbn';
  5. import {appEvents, contextSrv} from 'app/core/core';
  6. import {tickStep, getScaledDecimals, getFlotTickSize} from 'app/core/utils/ticks';
  7. import d3 from 'vendor/d3/d3';
  8. import {HeatmapTooltip} from './heatmap_tooltip';
  9. import {mergeZeroBuckets} from './heatmap_data_converter';
  10. let MIN_CARD_SIZE = 1,
  11. CARD_PADDING = 1,
  12. CARD_ROUND = 0,
  13. DATA_RANGE_WIDING_FACTOR = 1.2,
  14. DEFAULT_X_TICK_SIZE_PX = 100,
  15. DEFAULT_Y_TICK_SIZE_PX = 50,
  16. X_AXIS_TICK_PADDING = 10,
  17. Y_AXIS_TICK_PADDING = 5,
  18. MIN_SELECTION_WIDTH = 2;
  19. export default function link(scope, elem, attrs, ctrl) {
  20. let data, timeRange, panel, heatmap;
  21. // $heatmap is JQuery object, but heatmap is D3
  22. let $heatmap = elem.find('.heatmap-panel');
  23. let tooltip = new HeatmapTooltip($heatmap, scope);
  24. let width, height,
  25. yScale, xScale,
  26. chartWidth, chartHeight,
  27. chartTop, chartBottom,
  28. yAxisWidth, xAxisHeight,
  29. cardPadding, cardRound,
  30. cardWidth, cardHeight,
  31. colorScale, opacityScale,
  32. mouseUpHandler;
  33. let selection = {
  34. active: false,
  35. x1: -1,
  36. x2: -1
  37. };
  38. let padding = {left: 0, right: 0, top: 0, bottom: 0},
  39. margin = {left: 25, right: 15, top: 10, bottom: 20},
  40. dataRangeWidingFactor = DATA_RANGE_WIDING_FACTOR;
  41. ctrl.events.on('render', () => {
  42. render();
  43. ctrl.renderingCompleted();
  44. });
  45. function setElementHeight() {
  46. try {
  47. var height = ctrl.height || panel.height || ctrl.row.height;
  48. if (_.isString(height)) {
  49. height = parseInt(height.replace('px', ''), 10);
  50. }
  51. height -= 5; // padding
  52. height -= panel.title ? 24 : 9; // subtract panel title bar
  53. $heatmap.css('height', height + 'px');
  54. return true;
  55. } catch (e) { // IE throws errors sometimes
  56. return false;
  57. }
  58. }
  59. function getYAxisWidth(elem) {
  60. let axis_text = elem.selectAll(".axis-y text").nodes();
  61. let max_text_width = _.max(_.map(axis_text, text => {
  62. let el = $(text);
  63. // Use JQuery outerWidth() to compute full element width
  64. return el.outerWidth();
  65. }));
  66. return max_text_width;
  67. }
  68. function getXAxisHeight(elem) {
  69. let axis_line = elem.select(".axis-x line");
  70. if (!axis_line.empty()) {
  71. let axis_line_position = parseFloat(elem.select(".axis-x line").attr("y2"));
  72. let canvas_width = parseFloat(elem.attr("height"));
  73. return canvas_width - axis_line_position;
  74. } else {
  75. // Default height
  76. return 30;
  77. }
  78. }
  79. function addXAxis() {
  80. scope.xScale = xScale = d3.scaleTime()
  81. .domain([timeRange.from, timeRange.to])
  82. .range([0, chartWidth]);
  83. let ticks = chartWidth / DEFAULT_X_TICK_SIZE_PX;
  84. let grafanaTimeFormatter = grafanaTimeFormat(ticks, timeRange.from, timeRange.to);
  85. let timeFormat;
  86. let dashboardTimeZone = ctrl.dashboard.getTimezone();
  87. if (dashboardTimeZone === 'utc') {
  88. timeFormat = d3.utcFormat(grafanaTimeFormatter);
  89. } else {
  90. timeFormat = d3.timeFormat(grafanaTimeFormatter);
  91. }
  92. let xAxis = d3.axisBottom(xScale)
  93. .ticks(ticks)
  94. .tickFormat(timeFormat)
  95. .tickPadding(X_AXIS_TICK_PADDING)
  96. .tickSize(chartHeight);
  97. let posY = margin.top;
  98. let posX = yAxisWidth;
  99. heatmap.append("g")
  100. .attr("class", "axis axis-x")
  101. .attr("transform", "translate(" + posX + "," + posY + ")")
  102. .call(xAxis);
  103. // Remove horizontal line in the top of axis labels (called domain in d3)
  104. heatmap.select(".axis-x").select(".domain").remove();
  105. }
  106. function addYAxis() {
  107. let ticks = Math.ceil(chartHeight / DEFAULT_Y_TICK_SIZE_PX);
  108. let tick_interval = tickStep(data.heatmapStats.min, data.heatmapStats.max, ticks);
  109. let {y_min, y_max} = wideYAxisRange(data.heatmapStats.min, data.heatmapStats.max, tick_interval);
  110. // Rewrite min and max if it have been set explicitly
  111. y_min = panel.yAxis.min !== null ? panel.yAxis.min : y_min;
  112. y_max = panel.yAxis.max !== null ? panel.yAxis.max : y_max;
  113. // Adjust ticks after Y range widening
  114. tick_interval = tickStep(y_min, y_max, ticks);
  115. ticks = Math.ceil((y_max - y_min) / tick_interval);
  116. let decimalsAuto = getPrecision(tick_interval);
  117. let decimals = panel.yAxis.decimals === null ? decimalsAuto : panel.yAxis.decimals;
  118. // Calculate scaledDecimals for log scales using tick size (as in jquery.flot.js)
  119. let flot_tick_size = getFlotTickSize(y_min, y_max, ticks, decimalsAuto);
  120. let scaledDecimals = getScaledDecimals(decimals, flot_tick_size);
  121. ctrl.decimals = decimals;
  122. ctrl.scaledDecimals = scaledDecimals;
  123. // Set default Y min and max if no data
  124. if (_.isEmpty(data.buckets)) {
  125. y_max = 1;
  126. y_min = -1;
  127. ticks = 3;
  128. decimals = 1;
  129. }
  130. data.yAxis = {
  131. min: y_min,
  132. max: y_max,
  133. ticks: ticks
  134. };
  135. scope.yScale = yScale = d3.scaleLinear()
  136. .domain([y_min, y_max])
  137. .range([chartHeight, 0]);
  138. let yAxis = d3.axisLeft(yScale)
  139. .ticks(ticks)
  140. .tickFormat(tickValueFormatter(decimals, scaledDecimals))
  141. .tickSizeInner(0 - width)
  142. .tickSizeOuter(0)
  143. .tickPadding(Y_AXIS_TICK_PADDING);
  144. heatmap.append("g")
  145. .attr("class", "axis axis-y")
  146. .call(yAxis);
  147. // Calculate Y axis width first, then move axis into visible area
  148. let posY = margin.top;
  149. let posX = getYAxisWidth(heatmap) + Y_AXIS_TICK_PADDING;
  150. heatmap.select(".axis-y").attr("transform", "translate(" + posX + "," + posY + ")");
  151. // Remove vertical line in the right of axis labels (called domain in d3)
  152. heatmap.select(".axis-y").select(".domain").remove();
  153. }
  154. // Wide Y values range and anjust to bucket size
  155. function wideYAxisRange(min, max, tickInterval) {
  156. let y_widing = (max * (dataRangeWidingFactor - 1) - min * (dataRangeWidingFactor - 1)) / 2;
  157. let y_min, y_max;
  158. if (tickInterval === 0) {
  159. y_max = max * dataRangeWidingFactor;
  160. y_min = min - min * (dataRangeWidingFactor - 1);
  161. tickInterval = (y_max - y_min) / 2;
  162. } else {
  163. y_max = Math.ceil((max + y_widing) / tickInterval) * tickInterval;
  164. y_min = Math.floor((min - y_widing) / tickInterval) * tickInterval;
  165. }
  166. // Don't wide axis below 0 if all values are positive
  167. if (min >= 0 && y_min < 0) {
  168. y_min = 0;
  169. }
  170. return {y_min, y_max};
  171. }
  172. function addLogYAxis() {
  173. let log_base = panel.yAxis.logBase;
  174. let {y_min, y_max} = adjustLogRange(data.heatmapStats.minLog, data.heatmapStats.max, log_base);
  175. y_min = panel.yAxis.min && panel.yAxis.min !== '0' ? adjustLogMin(panel.yAxis.min, log_base) : y_min;
  176. y_max = panel.yAxis.max !== null ? adjustLogMax(panel.yAxis.max, log_base) : y_max;
  177. // Set default Y min and max if no data
  178. if (_.isEmpty(data.buckets)) {
  179. y_max = Math.pow(log_base, 2);
  180. y_min = 1;
  181. }
  182. scope.yScale = yScale = d3.scaleLog()
  183. .base(panel.yAxis.logBase)
  184. .domain([y_min, y_max])
  185. .range([chartHeight, 0]);
  186. let domain = yScale.domain();
  187. let tick_values = logScaleTickValues(domain, log_base);
  188. let decimalsAuto = getPrecision(y_min);
  189. let decimals = panel.yAxis.decimals || decimalsAuto;
  190. // Calculate scaledDecimals for log scales using tick size (as in jquery.flot.js)
  191. let flot_tick_size = getFlotTickSize(y_min, y_max, tick_values.length, decimalsAuto);
  192. let scaledDecimals = getScaledDecimals(decimals, flot_tick_size);
  193. ctrl.decimals = decimals;
  194. ctrl.scaledDecimals = scaledDecimals;
  195. data.yAxis = {
  196. min: y_min,
  197. max: y_max,
  198. ticks: tick_values.length
  199. };
  200. let yAxis = d3.axisLeft(yScale)
  201. .tickValues(tick_values)
  202. .tickFormat(tickValueFormatter(decimals, scaledDecimals))
  203. .tickSizeInner(0 - width)
  204. .tickSizeOuter(0)
  205. .tickPadding(Y_AXIS_TICK_PADDING);
  206. heatmap.append("g")
  207. .attr("class", "axis axis-y")
  208. .call(yAxis);
  209. // Calculate Y axis width first, then move axis into visible area
  210. let posY = margin.top;
  211. let posX = getYAxisWidth(heatmap) + Y_AXIS_TICK_PADDING;
  212. heatmap.select(".axis-y").attr("transform", "translate(" + posX + "," + posY + ")");
  213. // Set first tick as pseudo 0
  214. if (y_min < 1) {
  215. heatmap.select(".axis-y").select(".tick text").text("0");
  216. }
  217. // Remove vertical line in the right of axis labels (called domain in d3)
  218. heatmap.select(".axis-y").select(".domain").remove();
  219. }
  220. // Adjust data range to log base
  221. function adjustLogRange(min, max, logBase) {
  222. let y_min, y_max;
  223. y_min = data.heatmapStats.minLog;
  224. if (data.heatmapStats.minLog > 1 || !data.heatmapStats.minLog) {
  225. y_min = 1;
  226. } else {
  227. y_min = adjustLogMin(data.heatmapStats.minLog, logBase);
  228. }
  229. // Adjust max Y value to log base
  230. y_max = adjustLogMax(data.heatmapStats.max, logBase);
  231. return {y_min, y_max};
  232. }
  233. function adjustLogMax(max, base) {
  234. return Math.pow(base, Math.ceil(logp(max, base)));
  235. }
  236. function adjustLogMin(min, base) {
  237. return Math.pow(base, Math.floor(logp(min, base)));
  238. }
  239. function logScaleTickValues(domain, base) {
  240. let domainMin = domain[0];
  241. let domainMax = domain[1];
  242. let tickValues = [];
  243. if (domainMin < 1) {
  244. let under_one_ticks = Math.floor(logp(domainMin, base));
  245. for (let i = under_one_ticks; i < 0; i++) {
  246. let tick_value = Math.pow(base, i);
  247. tickValues.push(tick_value);
  248. }
  249. }
  250. let ticks = Math.ceil(logp(domainMax, base));
  251. for (let i = 0; i <= ticks; i++) {
  252. let tick_value = Math.pow(base, i);
  253. tickValues.push(tick_value);
  254. }
  255. return tickValues;
  256. }
  257. function tickValueFormatter(decimals, scaledDecimals = null) {
  258. let format = panel.yAxis.format;
  259. return function(value) {
  260. return kbn.valueFormats[format](value, decimals, scaledDecimals);
  261. };
  262. }
  263. function fixYAxisTickSize() {
  264. heatmap.select(".axis-y")
  265. .selectAll(".tick line")
  266. .attr("x2", chartWidth);
  267. }
  268. function addAxes() {
  269. chartHeight = height - margin.top - margin.bottom;
  270. chartTop = margin.top;
  271. chartBottom = chartTop + chartHeight;
  272. if (panel.yAxis.logBase === 1) {
  273. addYAxis();
  274. } else {
  275. addLogYAxis();
  276. }
  277. yAxisWidth = getYAxisWidth(heatmap) + Y_AXIS_TICK_PADDING;
  278. chartWidth = width - yAxisWidth - margin.right;
  279. fixYAxisTickSize();
  280. addXAxis();
  281. xAxisHeight = getXAxisHeight(heatmap);
  282. if (!panel.yAxis.show) {
  283. heatmap.select(".axis-y").selectAll("line").style("opacity", 0);
  284. }
  285. if (!panel.xAxis.show) {
  286. heatmap.select(".axis-x").selectAll("line").style("opacity", 0);
  287. }
  288. }
  289. function addHeatmapCanvas() {
  290. let heatmap_elem = $heatmap[0];
  291. width = Math.floor($heatmap.width()) - padding.right;
  292. height = Math.floor($heatmap.height()) - padding.bottom;
  293. cardPadding = panel.cards.cardPadding !== null ? panel.cards.cardPadding : CARD_PADDING;
  294. cardRound = panel.cards.cardRound !== null ? panel.cards.cardRound : CARD_ROUND;
  295. if (heatmap) {
  296. heatmap.remove();
  297. }
  298. heatmap = d3.select(heatmap_elem)
  299. .append("svg")
  300. .attr("width", width)
  301. .attr("height", height);
  302. }
  303. function addHeatmap() {
  304. addHeatmapCanvas();
  305. addAxes();
  306. if (panel.yAxis.logBase !== 1) {
  307. let log_base = panel.yAxis.logBase;
  308. let domain = yScale.domain();
  309. let tick_values = logScaleTickValues(domain, log_base);
  310. data.buckets = mergeZeroBuckets(data.buckets, _.min(tick_values));
  311. }
  312. let cardsData = data.cards;
  313. let maxValueAuto = data.cardStats.max;
  314. let maxValue = panel.color.max || maxValueAuto;
  315. let minValue = panel.color.min || 0;
  316. colorScale = getColorScale(maxValue, minValue);
  317. setOpacityScale(maxValue);
  318. setCardSize();
  319. let cards = heatmap.selectAll(".heatmap-card").data(cardsData);
  320. cards.append("title");
  321. cards = cards.enter().append("rect")
  322. .attr("x", getCardX)
  323. .attr("width", getCardWidth)
  324. .attr("y", getCardY)
  325. .attr("height", getCardHeight)
  326. .attr("rx", cardRound)
  327. .attr("ry", cardRound)
  328. .attr("class", "bordered heatmap-card")
  329. .style("fill", getCardColor)
  330. .style("stroke", getCardColor)
  331. .style("stroke-width", 0)
  332. .style("opacity", getCardOpacity);
  333. let $cards = $heatmap.find(".heatmap-card");
  334. $cards.on("mouseenter", (event) => {
  335. tooltip.mouseOverBucket = true;
  336. highlightCard(event);
  337. })
  338. .on("mouseleave", (event) => {
  339. tooltip.mouseOverBucket = false;
  340. resetCardHighLight(event);
  341. });
  342. }
  343. function highlightCard(event) {
  344. let color = d3.select(event.target).style("fill");
  345. let highlightColor = d3.color(color).darker(2);
  346. let strokeColor = d3.color(color).brighter(4);
  347. let current_card = d3.select(event.target);
  348. tooltip.originalFillColor = color;
  349. current_card.style("fill", highlightColor)
  350. .style("stroke", strokeColor)
  351. .style("stroke-width", 1);
  352. }
  353. function resetCardHighLight(event) {
  354. d3.select(event.target).style("fill", tooltip.originalFillColor)
  355. .style("stroke", tooltip.originalFillColor)
  356. .style("stroke-width", 0);
  357. }
  358. function getColorScale(maxValue, minValue = 0) {
  359. let colorScheme = _.find(ctrl.colorSchemes, {value: panel.color.colorScheme});
  360. let colorInterpolator = d3[colorScheme.value];
  361. let colorScaleInverted = colorScheme.invert === 'always' ||
  362. (colorScheme.invert === 'dark' && !contextSrv.user.lightTheme);
  363. let start = colorScaleInverted ? maxValue : minValue;
  364. let end = colorScaleInverted ? minValue : maxValue;
  365. return d3.scaleSequential(colorInterpolator).domain([start, end]);
  366. }
  367. function setOpacityScale(maxValue) {
  368. if (panel.color.colorScale === 'linear') {
  369. opacityScale = d3.scaleLinear()
  370. .domain([0, maxValue])
  371. .range([0, 1]);
  372. } else if (panel.color.colorScale === 'sqrt') {
  373. opacityScale = d3.scalePow().exponent(panel.color.exponent)
  374. .domain([0, maxValue])
  375. .range([0, 1]);
  376. }
  377. }
  378. function setCardSize() {
  379. let xGridSize = Math.floor(xScale(data.xBucketSize) - xScale(0));
  380. let yGridSize = Math.floor(yScale(yScale.invert(0) - data.yBucketSize));
  381. if (panel.yAxis.logBase !== 1) {
  382. let base = panel.yAxis.logBase;
  383. let splitFactor = data.yBucketSize || 1;
  384. yGridSize = Math.floor((yScale(1) - yScale(base)) / splitFactor);
  385. }
  386. cardWidth = xGridSize - cardPadding * 2;
  387. cardHeight = yGridSize ? yGridSize - cardPadding * 2 : 0;
  388. }
  389. function getCardX(d) {
  390. let x;
  391. if (xScale(d.x) < 0) {
  392. // Cut card left to prevent overlay
  393. x = yAxisWidth + cardPadding;
  394. } else {
  395. x = xScale(d.x) + yAxisWidth + cardPadding;
  396. }
  397. return x;
  398. }
  399. function getCardWidth(d) {
  400. let w;
  401. if (xScale(d.x) < 0) {
  402. // Cut card left to prevent overlay
  403. let cutted_width = xScale(d.x) + cardWidth;
  404. w = cutted_width > 0 ? cutted_width : 0;
  405. } else if (xScale(d.x) + cardWidth > chartWidth) {
  406. // Cut card right to prevent overlay
  407. w = chartWidth - xScale(d.x) - cardPadding;
  408. } else {
  409. w = cardWidth;
  410. }
  411. // Card width should be MIN_CARD_SIZE at least
  412. w = Math.max(w, MIN_CARD_SIZE);
  413. return w;
  414. }
  415. function getCardY(d) {
  416. let y = yScale(d.y) + chartTop - cardHeight - cardPadding;
  417. if (panel.yAxis.logBase !== 1 && d.y === 0) {
  418. y = chartBottom - cardHeight - cardPadding;
  419. } else {
  420. if (y < chartTop) {
  421. y = chartTop;
  422. }
  423. }
  424. return y;
  425. }
  426. function getCardHeight(d) {
  427. let y = yScale(d.y) + chartTop - cardHeight - cardPadding;
  428. let h = cardHeight;
  429. if (panel.yAxis.logBase !== 1 && d.y === 0) {
  430. return cardHeight;
  431. }
  432. // Cut card height to prevent overlay
  433. if (y < chartTop) {
  434. h = yScale(d.y) - cardPadding;
  435. } else if (yScale(d.y) > chartBottom) {
  436. h = chartBottom - y;
  437. } else if (y + cardHeight > chartBottom) {
  438. h = chartBottom - y;
  439. }
  440. // Height can't be more than chart height
  441. h = Math.min(h, chartHeight);
  442. // Card height should be MIN_CARD_SIZE at least
  443. h = Math.max(h, MIN_CARD_SIZE);
  444. return h;
  445. }
  446. function getCardColor(d) {
  447. if (panel.color.mode === 'opacity') {
  448. return panel.color.cardColor;
  449. } else {
  450. return colorScale(d.count);
  451. }
  452. }
  453. function getCardOpacity(d) {
  454. if (panel.color.mode === 'opacity') {
  455. return opacityScale(d.count);
  456. } else {
  457. return 1;
  458. }
  459. }
  460. /////////////////////////////
  461. // Selection and crosshair //
  462. /////////////////////////////
  463. // Shared crosshair and tooltip
  464. appEvents.on('graph-hover', event => {
  465. drawSharedCrosshair(event.pos);
  466. }, scope);
  467. appEvents.on('graph-hover-clear', () => {
  468. clearCrosshair();
  469. }, scope);
  470. function onMouseDown(event) {
  471. selection.active = true;
  472. selection.x1 = event.offsetX;
  473. mouseUpHandler = function() {
  474. onMouseUp();
  475. };
  476. $(document).one("mouseup", mouseUpHandler);
  477. }
  478. function onMouseUp() {
  479. $(document).unbind("mouseup", mouseUpHandler);
  480. mouseUpHandler = null;
  481. selection.active = false;
  482. let selectionRange = Math.abs(selection.x2 - selection.x1);
  483. if (selection.x2 >= 0 && selectionRange > MIN_SELECTION_WIDTH) {
  484. let timeFrom = xScale.invert(Math.min(selection.x1, selection.x2) - yAxisWidth);
  485. let timeTo = xScale.invert(Math.max(selection.x1, selection.x2) - yAxisWidth);
  486. ctrl.timeSrv.setTime({
  487. from: moment.utc(timeFrom),
  488. to: moment.utc(timeTo)
  489. });
  490. }
  491. clearSelection();
  492. }
  493. function onMouseLeave() {
  494. appEvents.emit('graph-hover-clear');
  495. clearCrosshair();
  496. }
  497. function onMouseMove(event) {
  498. if (!heatmap) { return; }
  499. if (selection.active) {
  500. // Clear crosshair and tooltip
  501. clearCrosshair();
  502. tooltip.destroy();
  503. selection.x2 = limitSelection(event.offsetX);
  504. drawSelection(selection.x1, selection.x2);
  505. } else {
  506. emitGraphHoverEvet(event);
  507. drawCrosshair(event.offsetX);
  508. tooltip.show(event, data);
  509. }
  510. }
  511. function emitGraphHoverEvet(event) {
  512. let x = xScale.invert(event.offsetX - yAxisWidth).valueOf();
  513. let y = yScale.invert(event.offsetY);
  514. let pos = {
  515. pageX: event.pageX,
  516. pageY: event.pageY,
  517. x: x, x1: x,
  518. y: y, y1: y,
  519. panelRelY: null
  520. };
  521. // Set minimum offset to prevent showing legend from another panel
  522. pos.panelRelY = Math.max(event.offsetY / height, 0.001);
  523. // broadcast to other graph panels that we are hovering
  524. appEvents.emit('graph-hover', {pos: pos, panel: panel});
  525. }
  526. function limitSelection(x2) {
  527. x2 = Math.max(x2, yAxisWidth);
  528. x2 = Math.min(x2, chartWidth + yAxisWidth);
  529. return x2;
  530. }
  531. function drawSelection(posX1, posX2) {
  532. if (heatmap) {
  533. heatmap.selectAll(".heatmap-selection").remove();
  534. let selectionX = Math.min(posX1, posX2);
  535. let selectionWidth = Math.abs(posX1 - posX2);
  536. if (selectionWidth > MIN_SELECTION_WIDTH) {
  537. heatmap.append("rect")
  538. .attr("class", "heatmap-selection")
  539. .attr("x", selectionX)
  540. .attr("width", selectionWidth)
  541. .attr("y", chartTop)
  542. .attr("height", chartHeight);
  543. }
  544. }
  545. }
  546. function clearSelection() {
  547. selection.x1 = -1;
  548. selection.x2 = -1;
  549. if (heatmap) {
  550. heatmap.selectAll(".heatmap-selection").remove();
  551. }
  552. }
  553. function drawCrosshair(position) {
  554. if (heatmap) {
  555. heatmap.selectAll(".heatmap-crosshair").remove();
  556. let posX = position;
  557. posX = Math.max(posX, yAxisWidth);
  558. posX = Math.min(posX, chartWidth + yAxisWidth);
  559. heatmap.append("g")
  560. .attr("class", "heatmap-crosshair")
  561. .attr("transform", "translate(" + posX + ",0)")
  562. .append("line")
  563. .attr("x1", 1)
  564. .attr("y1", chartTop)
  565. .attr("x2", 1)
  566. .attr("y2", chartBottom)
  567. .attr("stroke-width", 1);
  568. }
  569. }
  570. function drawSharedCrosshair(pos) {
  571. if (heatmap && ctrl.dashboard.graphTooltip !== 0) {
  572. let posX = xScale(pos.x) + yAxisWidth;
  573. drawCrosshair(posX);
  574. }
  575. }
  576. function clearCrosshair() {
  577. if (heatmap) {
  578. heatmap.selectAll(".heatmap-crosshair").remove();
  579. }
  580. }
  581. function render() {
  582. data = ctrl.data;
  583. panel = ctrl.panel;
  584. timeRange = ctrl.range;
  585. if (!setElementHeight() || !data) {
  586. return;
  587. }
  588. // Draw default axes and return if no data
  589. if (_.isEmpty(data.buckets)) {
  590. addHeatmapCanvas();
  591. addAxes();
  592. return;
  593. }
  594. addHeatmap();
  595. scope.yAxisWidth = yAxisWidth;
  596. scope.xAxisHeight = xAxisHeight;
  597. scope.chartHeight = chartHeight;
  598. scope.chartWidth = chartWidth;
  599. scope.chartTop = chartTop;
  600. }
  601. // Register selection listeners
  602. $heatmap.on("mousedown", onMouseDown);
  603. $heatmap.on("mousemove", onMouseMove);
  604. $heatmap.on("mouseleave", onMouseLeave);
  605. }
  606. function grafanaTimeFormat(ticks, min, max) {
  607. if (min && max && ticks) {
  608. let range = max - min;
  609. let secPerTick = (range/ticks) / 1000;
  610. let oneDay = 86400000;
  611. let oneYear = 31536000000;
  612. if (secPerTick <= 45) {
  613. return "%H:%M:%S";
  614. }
  615. if (secPerTick <= 7200 || range <= oneDay) {
  616. return "%H:%M";
  617. }
  618. if (secPerTick <= 80000) {
  619. return "%m/%d %H:%M";
  620. }
  621. if (secPerTick <= 2419200 || range <= oneYear) {
  622. return "%m/%d";
  623. }
  624. return "%Y-%m";
  625. }
  626. return "%H:%M";
  627. }
  628. function logp(value, base) {
  629. return Math.log(value) / Math.log(base);
  630. }
  631. function getPrecision(num) {
  632. let str = num.toString();
  633. let dot_index = str.indexOf(".");
  634. if (dot_index === -1) {
  635. return 0;
  636. } else {
  637. return str.length - dot_index - 1;
  638. }
  639. }