module.js 6.7 KB

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