singleStatPanel.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. define([
  2. 'angular',
  3. 'app/app',
  4. 'lodash',
  5. 'jquery',
  6. 'jquery.flot',
  7. ],
  8. function (angular, app, _, $) {
  9. 'use strict';
  10. var module = angular.module('grafana.panels.singlestat', []);
  11. app.useModule(module);
  12. module.directive('singlestatPanel', function($location, linkSrv, $timeout, templateSrv) {
  13. return {
  14. link: function(scope, elem) {
  15. var data, panel, linkInfo;
  16. var $panelContainer = elem.parents('.panel-container');
  17. scope.$on('render', function() {
  18. render();
  19. scope.panelRenderingComplete();
  20. });
  21. function setElementHeight() {
  22. try {
  23. var height = scope.height || panel.height || scope.row.height;
  24. if (_.isString(height)) {
  25. height = parseInt(height.replace('px', ''), 10);
  26. }
  27. height -= 5; // padding
  28. height -= panel.title ? 24 : 9; // subtract panel title bar
  29. elem.css('height', height + 'px');
  30. return true;
  31. } catch(e) { // IE throws errors sometimes
  32. return false;
  33. }
  34. }
  35. function applyColoringThresholds(value, valueString) {
  36. if (!panel.colorValue) {
  37. return valueString;
  38. }
  39. var color = getColorForValue(value);
  40. if (color) {
  41. return '<span style="color:' + color + '">'+ valueString + '</span>';
  42. }
  43. return valueString;
  44. }
  45. function getColorForValue(value) {
  46. for (var i = data.thresholds.length - 1; i >= 0 ; i--) {
  47. if (value >= data.thresholds[i]) {
  48. return data.colorMap[i];
  49. }
  50. }
  51. return null;
  52. }
  53. function getSpan(className, fontSize, value) {
  54. value = templateSrv.replace(value);
  55. return '<span class="' + className + '" style="font-size:' + fontSize + '">' +
  56. value + '</span>';
  57. }
  58. function getBigValueHtml() {
  59. var body = '<div class="singlestat-panel-value-container">';
  60. if (panel.prefix) { body += getSpan('singlestat-panel-prefix', panel.prefixFontSize, scope.panel.prefix); }
  61. var value = applyColoringThresholds(data.valueRounded, data.valueFormated);
  62. body += getSpan('singlestat-panel-value', panel.valueFontSize, value);
  63. if (panel.postfix) { body += getSpan('singlestat-panel-postfix', panel.postfixFontSize, panel.postfix); }
  64. body += '</div>';
  65. return body;
  66. }
  67. function addSparkline() {
  68. var panel = scope.panel;
  69. var width = elem.width() + 20;
  70. var height = elem.height() || 100;
  71. var plotCanvas = $('<div></div>');
  72. var plotCss = {};
  73. plotCss.position = 'absolute';
  74. if (panel.sparkline.full) {
  75. plotCss.bottom = '5px';
  76. plotCss.left = '-5px';
  77. plotCss.width = (width - 10) + 'px';
  78. var dynamicHeightMargin = height <= 100 ? 5 : (Math.round((height/100)) * 15) + 5;
  79. plotCss.height = (height - dynamicHeightMargin) + 'px';
  80. }
  81. else {
  82. plotCss.bottom = "0px";
  83. plotCss.left = "-5px";
  84. plotCss.width = (width - 10) + 'px';
  85. plotCss.height = Math.floor(height * 0.25) + "px";
  86. }
  87. plotCanvas.css(plotCss);
  88. var options = {
  89. legend: { show: false },
  90. series: {
  91. lines: {
  92. show: true,
  93. fill: 1,
  94. lineWidth: 1,
  95. fillColor: panel.sparkline.fillColor,
  96. },
  97. },
  98. yaxes: { show: false },
  99. xaxis: {
  100. show: false,
  101. mode: "time",
  102. min: scope.range.from.valueOf(),
  103. max: scope.range.to.valueOf(),
  104. },
  105. grid: { hoverable: false, show: false },
  106. };
  107. elem.append(plotCanvas);
  108. var plotSeries = {
  109. data: data.flotpairs,
  110. color: panel.sparkline.lineColor
  111. };
  112. $.plot(plotCanvas, [plotSeries], options);
  113. }
  114. function render() {
  115. if (!scope.data) { return; }
  116. data = scope.data;
  117. panel = scope.panel;
  118. setElementHeight();
  119. var body = getBigValueHtml();
  120. if (panel.colorBackground && !isNaN(data.valueRounded)) {
  121. var color = getColorForValue(data.valueRounded);
  122. if (color) {
  123. $panelContainer.css('background-color', color);
  124. if (scope.fullscreen) {
  125. elem.css('background-color', color);
  126. } else {
  127. elem.css('background-color', '');
  128. }
  129. }
  130. } else {
  131. $panelContainer.css('background-color', '');
  132. elem.css('background-color', '');
  133. }
  134. elem.html(body);
  135. if (panel.sparkline.show) {
  136. addSparkline();
  137. }
  138. elem.toggleClass('pointer', panel.links.length > 0);
  139. if (panel.links.length > 0) {
  140. linkInfo = linkSrv.getPanelLinkAnchorInfo(panel.links[0], scope.panel.scopedVars);
  141. } else {
  142. linkInfo = null;
  143. }
  144. }
  145. // drilldown link tooltip
  146. var drilldownTooltip = $('<div id="tooltip" class="">hello</div>"');
  147. elem.mouseleave(function() {
  148. if (panel.links.length === 0) { return;}
  149. drilldownTooltip.detach();
  150. });
  151. elem.click(function() {
  152. if (!linkInfo) { return; }
  153. if (linkInfo.target === '_blank') {
  154. var redirectWindow = window.open(linkInfo.href, '_blank');
  155. redirectWindow.location;
  156. return;
  157. }
  158. if (linkInfo.href.indexOf('http') === 0) {
  159. window.location.href = linkInfo.href;
  160. } else {
  161. $timeout(function() {
  162. $location.url(linkInfo.href);
  163. });
  164. }
  165. drilldownTooltip.detach();
  166. });
  167. elem.mousemove(function(e) {
  168. if (!linkInfo) { return;}
  169. drilldownTooltip.text('click to go to: ' + linkInfo.title);
  170. drilldownTooltip.place_tt(e.pageX+20, e.pageY-15);
  171. });
  172. }
  173. };
  174. });
  175. });