singleStatPanel.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. plotCss.height = (height - 45) + 'px';
  79. }
  80. else {
  81. plotCss.bottom = "0px";
  82. plotCss.left = "-5px";
  83. plotCss.width = (width - 10) + 'px';
  84. plotCss.height = Math.floor(height * 0.25) + "px";
  85. }
  86. plotCanvas.css(plotCss);
  87. var options = {
  88. legend: { show: false },
  89. series: {
  90. lines: {
  91. show: true,
  92. fill: 1,
  93. lineWidth: 1,
  94. fillColor: panel.sparkline.fillColor,
  95. },
  96. },
  97. yaxes: { show: false },
  98. xaxis: {
  99. show: false,
  100. mode: "time",
  101. min: scope.range.from.valueOf(),
  102. max: scope.range.to.valueOf(),
  103. },
  104. grid: { hoverable: false, show: false },
  105. };
  106. elem.append(plotCanvas);
  107. var plotSeries = {
  108. data: data.flotpairs,
  109. color: panel.sparkline.lineColor
  110. };
  111. $.plot(plotCanvas, [plotSeries], options);
  112. }
  113. function render() {
  114. if (!scope.data) { return; }
  115. data = scope.data;
  116. panel = scope.panel;
  117. setElementHeight();
  118. var body = getBigValueHtml();
  119. if (panel.colorBackground && !isNaN(data.valueRounded)) {
  120. var color = getColorForValue(data.valueRounded);
  121. if (color) {
  122. $panelContainer.css('background-color', color);
  123. if (scope.fullscreen) {
  124. elem.css('background-color', color);
  125. } else {
  126. elem.css('background-color', '');
  127. }
  128. }
  129. } else {
  130. $panelContainer.css('background-color', '');
  131. elem.css('background-color', '');
  132. }
  133. elem.html(body);
  134. if (panel.sparkline.show) {
  135. addSparkline();
  136. }
  137. elem.toggleClass('pointer', panel.links.length > 0);
  138. if (panel.links.length > 0) {
  139. linkInfo = linkSrv.getPanelLinkAnchorInfo(panel.links[0], scope.panel.scopedVars);
  140. } else {
  141. linkInfo = null;
  142. }
  143. }
  144. // drilldown link tooltip
  145. var drilldownTooltip = $('<div id="tooltip" class="">hello</div>"');
  146. elem.mouseleave(function() {
  147. if (panel.links.length === 0) { return;}
  148. drilldownTooltip.detach();
  149. });
  150. elem.click(function() {
  151. if (!linkInfo) { return; }
  152. if (linkInfo.target === '_blank') {
  153. var redirectWindow = window.open(linkInfo.href, '_blank');
  154. redirectWindow.location;
  155. return;
  156. }
  157. if (linkInfo.href.indexOf('http') === 0) {
  158. window.location.href = linkInfo.href;
  159. } else {
  160. $timeout(function() {
  161. $location.url(linkInfo.href);
  162. });
  163. }
  164. drilldownTooltip.detach();
  165. });
  166. elem.mousemove(function(e) {
  167. if (!linkInfo) { return;}
  168. drilldownTooltip.text('click to go to: ' + linkInfo.title);
  169. drilldownTooltip.place_tt(e.pageX+20, e.pageY-15);
  170. });
  171. }
  172. };
  173. });
  174. });