module.js 6.5 KB

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