rendering.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import moment from 'moment';
  4. import * as d3 from 'd3';
  5. import kbn from 'app/core/utils/kbn';
  6. import {appEvents, contextSrv} from 'app/core/core';
  7. import {tickStep, getScaledDecimals, getFlotTickSize} from 'app/core/utils/ticks';
  8. import {HeatmapTooltip} from './heatmap_tooltip';
  9. import {mergeZeroBuckets} from './heatmap_data_converter';
  10. import {getColorScale, getOpacityScale} from './color_scale';
  11. let MIN_CARD_SIZE = 1,
  12. CARD_PADDING = 1,
  13. CARD_ROUND = 0,
  14. DATA_RANGE_WIDING_FACTOR = 1.2,
  15. DEFAULT_X_TICK_SIZE_PX = 100,
  16. DEFAULT_Y_TICK_SIZE_PX = 50,
  17. X_AXIS_TICK_PADDING = 10,
  18. Y_AXIS_TICK_PADDING = 5,
  19. MIN_SELECTION_WIDTH = 2;
  20. export default function link(scope, elem, attrs, ctrl) {
  21. let data, timeRange, panel, heatmap;
  22. // $heatmap is JQuery object, but heatmap is D3
  23. let $heatmap = elem.find('.heatmap-panel');
  24. let tooltip = new HeatmapTooltip($heatmap, scope);
  25. let width, height,
  26. yScale, xScale,
  27. chartWidth, chartHeight,
  28. chartTop, chartBottom,
  29. yAxisWidth, xAxisHeight,
  30. cardPadding, cardRound,
  31. cardWidth, cardHeight,
  32. colorScale, opacityScale,
  33. mouseUpHandler;
  34. let selection = {
  35. active: false,
  36. x1: -1,
  37. x2: -1
  38. };
  39. let padding = {left: 0, right: 0, top: 0, bottom: 0},
  40. margin = {left: 25, right: 15, top: 10, bottom: 20},
  41. dataRangeWidingFactor = DATA_RANGE_WIDING_FACTOR;
  42. ctrl.events.on('render', () => {
  43. render();
  44. ctrl.renderingCompleted();
  45. });
  46. function setElementHeight() {
  47. try {
  48. var height = ctrl.height || panel.height || ctrl.row.height;
  49. if (_.isString(height)) {
  50. height = parseInt(height.replace('px', ''), 10);
  51. }
  52. height -= 5; // padding
  53. height -= panel.title ? 24 : 9; // subtract panel title bar
  54. $heatmap.css('height', height + 'px');
  55. return true;
  56. } catch (e) { // IE throws errors sometimes
  57. return false;
  58. }
  59. }
  60. function getYAxisWidth(elem) {
  61. let axis_text = elem.selectAll(".axis-y text").nodes();
  62. let max_text_width = _.max(_.map(axis_text, text => {
  63. // Use SVG getBBox method
  64. return text.getBBox().width;
  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. let colorScheme = _.find(ctrl.colorSchemes, {value: panel.color.colorScheme});
  317. colorScale = getColorScale(colorScheme, contextSrv.user.lightTheme, maxValue, minValue);
  318. opacityScale = getOpacityScale(panel.color, maxValue);
  319. setCardSize();
  320. let cards = heatmap.selectAll(".heatmap-card").data(cardsData);
  321. cards.append("title");
  322. cards = cards.enter().append("rect")
  323. .attr("x", getCardX)
  324. .attr("width", getCardWidth)
  325. .attr("y", getCardY)
  326. .attr("height", getCardHeight)
  327. .attr("rx", cardRound)
  328. .attr("ry", cardRound)
  329. .attr("class", "bordered heatmap-card")
  330. .style("fill", getCardColor)
  331. .style("stroke", getCardColor)
  332. .style("stroke-width", 0)
  333. .style("opacity", getCardOpacity);
  334. let $cards = $heatmap.find(".heatmap-card");
  335. $cards.on("mouseenter", (event) => {
  336. tooltip.mouseOverBucket = true;
  337. highlightCard(event);
  338. })
  339. .on("mouseleave", (event) => {
  340. tooltip.mouseOverBucket = false;
  341. resetCardHighLight(event);
  342. });
  343. }
  344. function highlightCard(event) {
  345. let color = d3.select(event.target).style("fill");
  346. let highlightColor = d3.color(color).darker(2);
  347. let strokeColor = d3.color(color).brighter(4);
  348. let current_card = d3.select(event.target);
  349. tooltip.originalFillColor = color;
  350. current_card.style("fill", highlightColor.toString())
  351. .style("stroke", strokeColor.toString())
  352. .style("stroke-width", 1);
  353. }
  354. function resetCardHighLight(event) {
  355. d3.select(event.target).style("fill", tooltip.originalFillColor)
  356. .style("stroke", tooltip.originalFillColor)
  357. .style("stroke-width", 0);
  358. }
  359. function setCardSize() {
  360. let xGridSize = Math.floor(xScale(data.xBucketSize) - xScale(0));
  361. let yGridSize = Math.floor(yScale(yScale.invert(0) - data.yBucketSize));
  362. if (panel.yAxis.logBase !== 1) {
  363. let base = panel.yAxis.logBase;
  364. let splitFactor = data.yBucketSize || 1;
  365. yGridSize = Math.floor((yScale(1) - yScale(base)) / splitFactor);
  366. }
  367. cardWidth = xGridSize - cardPadding * 2;
  368. cardHeight = yGridSize ? yGridSize - cardPadding * 2 : 0;
  369. }
  370. function getCardX(d) {
  371. let x;
  372. if (xScale(d.x) < 0) {
  373. // Cut card left to prevent overlay
  374. x = yAxisWidth + cardPadding;
  375. } else {
  376. x = xScale(d.x) + yAxisWidth + cardPadding;
  377. }
  378. return x;
  379. }
  380. function getCardWidth(d) {
  381. let w;
  382. if (xScale(d.x) < 0) {
  383. // Cut card left to prevent overlay
  384. let cutted_width = xScale(d.x) + cardWidth;
  385. w = cutted_width > 0 ? cutted_width : 0;
  386. } else if (xScale(d.x) + cardWidth > chartWidth) {
  387. // Cut card right to prevent overlay
  388. w = chartWidth - xScale(d.x) - cardPadding;
  389. } else {
  390. w = cardWidth;
  391. }
  392. // Card width should be MIN_CARD_SIZE at least
  393. w = Math.max(w, MIN_CARD_SIZE);
  394. return w;
  395. }
  396. function getCardY(d) {
  397. let y = yScale(d.y) + chartTop - cardHeight - cardPadding;
  398. if (panel.yAxis.logBase !== 1 && d.y === 0) {
  399. y = chartBottom - cardHeight - cardPadding;
  400. } else {
  401. if (y < chartTop) {
  402. y = chartTop;
  403. }
  404. }
  405. return y;
  406. }
  407. function getCardHeight(d) {
  408. let y = yScale(d.y) + chartTop - cardHeight - cardPadding;
  409. let h = cardHeight;
  410. if (panel.yAxis.logBase !== 1 && d.y === 0) {
  411. return cardHeight;
  412. }
  413. // Cut card height to prevent overlay
  414. if (y < chartTop) {
  415. h = yScale(d.y) - cardPadding;
  416. } else if (yScale(d.y) > chartBottom) {
  417. h = chartBottom - y;
  418. } else if (y + cardHeight > chartBottom) {
  419. h = chartBottom - y;
  420. }
  421. // Height can't be more than chart height
  422. h = Math.min(h, chartHeight);
  423. // Card height should be MIN_CARD_SIZE at least
  424. h = Math.max(h, MIN_CARD_SIZE);
  425. return h;
  426. }
  427. function getCardColor(d) {
  428. if (panel.color.mode === 'opacity') {
  429. return panel.color.cardColor;
  430. } else {
  431. return colorScale(d.count);
  432. }
  433. }
  434. function getCardOpacity(d) {
  435. if (panel.color.mode === 'opacity') {
  436. return opacityScale(d.count);
  437. } else {
  438. return 1;
  439. }
  440. }
  441. /////////////////////////////
  442. // Selection and crosshair //
  443. /////////////////////////////
  444. // Shared crosshair and tooltip
  445. appEvents.on('graph-hover', event => {
  446. drawSharedCrosshair(event.pos);
  447. }, scope);
  448. appEvents.on('graph-hover-clear', () => {
  449. clearCrosshair();
  450. }, scope);
  451. function onMouseDown(event) {
  452. selection.active = true;
  453. selection.x1 = event.offsetX;
  454. mouseUpHandler = function() {
  455. onMouseUp();
  456. };
  457. $(document).one("mouseup", mouseUpHandler);
  458. }
  459. function onMouseUp() {
  460. $(document).unbind("mouseup", mouseUpHandler);
  461. mouseUpHandler = null;
  462. selection.active = false;
  463. let selectionRange = Math.abs(selection.x2 - selection.x1);
  464. if (selection.x2 >= 0 && selectionRange > MIN_SELECTION_WIDTH) {
  465. let timeFrom = xScale.invert(Math.min(selection.x1, selection.x2) - yAxisWidth);
  466. let timeTo = xScale.invert(Math.max(selection.x1, selection.x2) - yAxisWidth);
  467. ctrl.timeSrv.setTime({
  468. from: moment.utc(timeFrom),
  469. to: moment.utc(timeTo)
  470. });
  471. }
  472. clearSelection();
  473. }
  474. function onMouseLeave() {
  475. appEvents.emit('graph-hover-clear');
  476. clearCrosshair();
  477. }
  478. function onMouseMove(event) {
  479. if (!heatmap) { return; }
  480. if (selection.active) {
  481. // Clear crosshair and tooltip
  482. clearCrosshair();
  483. tooltip.destroy();
  484. selection.x2 = limitSelection(event.offsetX);
  485. drawSelection(selection.x1, selection.x2);
  486. } else {
  487. emitGraphHoverEvet(event);
  488. drawCrosshair(event.offsetX);
  489. tooltip.show(event, data);
  490. }
  491. }
  492. function emitGraphHoverEvet(event) {
  493. let x = xScale.invert(event.offsetX - yAxisWidth).valueOf();
  494. let y = yScale.invert(event.offsetY);
  495. let pos = {
  496. pageX: event.pageX,
  497. pageY: event.pageY,
  498. x: x, x1: x,
  499. y: y, y1: y,
  500. panelRelY: null
  501. };
  502. // Set minimum offset to prevent showing legend from another panel
  503. pos.panelRelY = Math.max(event.offsetY / height, 0.001);
  504. // broadcast to other graph panels that we are hovering
  505. appEvents.emit('graph-hover', {pos: pos, panel: panel});
  506. }
  507. function limitSelection(x2) {
  508. x2 = Math.max(x2, yAxisWidth);
  509. x2 = Math.min(x2, chartWidth + yAxisWidth);
  510. return x2;
  511. }
  512. function drawSelection(posX1, posX2) {
  513. if (heatmap) {
  514. heatmap.selectAll(".heatmap-selection").remove();
  515. let selectionX = Math.min(posX1, posX2);
  516. let selectionWidth = Math.abs(posX1 - posX2);
  517. if (selectionWidth > MIN_SELECTION_WIDTH) {
  518. heatmap.append("rect")
  519. .attr("class", "heatmap-selection")
  520. .attr("x", selectionX)
  521. .attr("width", selectionWidth)
  522. .attr("y", chartTop)
  523. .attr("height", chartHeight);
  524. }
  525. }
  526. }
  527. function clearSelection() {
  528. selection.x1 = -1;
  529. selection.x2 = -1;
  530. if (heatmap) {
  531. heatmap.selectAll(".heatmap-selection").remove();
  532. }
  533. }
  534. function drawCrosshair(position) {
  535. if (heatmap) {
  536. heatmap.selectAll(".heatmap-crosshair").remove();
  537. let posX = position;
  538. posX = Math.max(posX, yAxisWidth);
  539. posX = Math.min(posX, chartWidth + yAxisWidth);
  540. heatmap.append("g")
  541. .attr("class", "heatmap-crosshair")
  542. .attr("transform", "translate(" + posX + ",0)")
  543. .append("line")
  544. .attr("x1", 1)
  545. .attr("y1", chartTop)
  546. .attr("x2", 1)
  547. .attr("y2", chartBottom)
  548. .attr("stroke-width", 1);
  549. }
  550. }
  551. function drawSharedCrosshair(pos) {
  552. if (heatmap && ctrl.dashboard.graphTooltip !== 0) {
  553. let posX = xScale(pos.x) + yAxisWidth;
  554. drawCrosshair(posX);
  555. }
  556. }
  557. function clearCrosshair() {
  558. if (heatmap) {
  559. heatmap.selectAll(".heatmap-crosshair").remove();
  560. }
  561. }
  562. function render() {
  563. data = ctrl.data;
  564. panel = ctrl.panel;
  565. timeRange = ctrl.range;
  566. if (!setElementHeight() || !data) {
  567. return;
  568. }
  569. // Draw default axes and return if no data
  570. if (_.isEmpty(data.buckets)) {
  571. addHeatmapCanvas();
  572. addAxes();
  573. return;
  574. }
  575. addHeatmap();
  576. scope.yAxisWidth = yAxisWidth;
  577. scope.xAxisHeight = xAxisHeight;
  578. scope.chartHeight = chartHeight;
  579. scope.chartWidth = chartWidth;
  580. scope.chartTop = chartTop;
  581. }
  582. // Register selection listeners
  583. $heatmap.on("mousedown", onMouseDown);
  584. $heatmap.on("mousemove", onMouseMove);
  585. $heatmap.on("mouseleave", onMouseLeave);
  586. }
  587. function grafanaTimeFormat(ticks, min, max) {
  588. if (min && max && ticks) {
  589. let range = max - min;
  590. let secPerTick = (range/ticks) / 1000;
  591. let oneDay = 86400000;
  592. let oneYear = 31536000000;
  593. if (secPerTick <= 45) {
  594. return "%H:%M:%S";
  595. }
  596. if (secPerTick <= 7200 || range <= oneDay) {
  597. return "%H:%M";
  598. }
  599. if (secPerTick <= 80000) {
  600. return "%m/%d %H:%M";
  601. }
  602. if (secPerTick <= 2419200 || range <= oneYear) {
  603. return "%m/%d";
  604. }
  605. return "%Y-%m";
  606. }
  607. return "%H:%M";
  608. }
  609. function logp(value, base) {
  610. return Math.log(value) / Math.log(base);
  611. }
  612. function getPrecision(num) {
  613. let str = num.toString();
  614. let dot_index = str.indexOf(".");
  615. if (dot_index === -1) {
  616. return 0;
  617. } else {
  618. return str.length - dot_index - 1;
  619. }
  620. }