rendering.ts 23 KB

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