singleStatPanel.js 5.8 KB

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