module.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import 'jquery.flot';
  5. import {SingleStatCtrl} from './controller';
  6. import {PanelDirective} from '../../../features/panel/panel';
  7. class SingleStatPanel extends PanelDirective {
  8. templateUrl = 'public/app/plugins/panel/singlestat/module.html';
  9. controller = SingleStatCtrl;
  10. /** @ngInject */
  11. constructor(private $location, private linkSrv, private $timeout, private templateSrv) {
  12. super();
  13. }
  14. link(scope, elem, attrs, ctrl) {
  15. var $location = this.$location;
  16. var linkSrv = this.linkSrv;
  17. var $timeout = this.$timeout;
  18. var panel = ctrl.panel;
  19. var templateSrv = this.templateSrv;
  20. var data, linkInfo, $panelContainer;
  21. var firstRender = true;
  22. scope.$on('render', function() {
  23. if (firstRender) {
  24. var inner = elem.find('.singlestat-panel');
  25. if (inner.length) {
  26. elem = inner;
  27. $panelContainer = elem.parents('.panel-container');
  28. firstRender = false;
  29. hookupDrilldownLinkTooltip();
  30. } else {
  31. return;
  32. }
  33. }
  34. render();
  35. ctrl.renderingCompleted();
  36. });
  37. function setElementHeight() {
  38. try {
  39. var height = scope.height || panel.height || ctrl.row.height;
  40. if (_.isString(height)) {
  41. height = parseInt(height.replace('px', ''), 10);
  42. }
  43. height -= 5; // padding
  44. height -= panel.title ? 24 : 9; // subtract panel title bar
  45. elem.css('height', height + 'px');
  46. return true;
  47. } catch (e) { // IE throws errors sometimes
  48. return false;
  49. }
  50. }
  51. function applyColoringThresholds(value, valueString) {
  52. if (!panel.colorValue) {
  53. return valueString;
  54. }
  55. var color = getColorForValue(data, value);
  56. if (color) {
  57. return '<span style="color:' + color + '">'+ valueString + '</span>';
  58. }
  59. return valueString;
  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, 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 width = elem.width() + 20;
  77. var height = elem.height() || 100;
  78. var plotCanvas = $('<div></div>');
  79. var plotCss: any = {};
  80. plotCss.position = 'absolute';
  81. if (panel.sparkline.full) {
  82. plotCss.bottom = '5px';
  83. plotCss.left = '-5px';
  84. plotCss.width = (width - 10) + 'px';
  85. var dynamicHeightMargin = height <= 100 ? 5 : (Math.round((height/100)) * 15) + 5;
  86. plotCss.height = (height - dynamicHeightMargin) + 'px';
  87. } else {
  88. plotCss.bottom = "0px";
  89. plotCss.left = "-5px";
  90. plotCss.width = (width - 10) + 'px';
  91. plotCss.height = Math.floor(height * 0.25) + "px";
  92. }
  93. plotCanvas.css(plotCss);
  94. var options = {
  95. legend: { show: false },
  96. series: {
  97. lines: {
  98. show: true,
  99. fill: 1,
  100. lineWidth: 1,
  101. fillColor: panel.sparkline.fillColor,
  102. },
  103. },
  104. yaxes: { show: false },
  105. xaxis: {
  106. show: false,
  107. mode: "time",
  108. min: ctrl.range.from.valueOf(),
  109. max: ctrl.range.to.valueOf(),
  110. },
  111. grid: { hoverable: false, show: false },
  112. };
  113. elem.append(plotCanvas);
  114. var plotSeries = {
  115. data: data.flotpairs,
  116. color: panel.sparkline.lineColor
  117. };
  118. $.plot(plotCanvas, [plotSeries], options);
  119. }
  120. function render() {
  121. if (!ctrl.data) { return; }
  122. data = ctrl.data;
  123. setElementHeight();
  124. var body = getBigValueHtml();
  125. if (panel.colorBackground && !isNaN(data.valueRounded)) {
  126. var color = getColorForValue(data, data.valueRounded);
  127. if (color) {
  128. $panelContainer.css('background-color', color);
  129. if (scope.fullscreen) {
  130. elem.css('background-color', color);
  131. } else {
  132. elem.css('background-color', '');
  133. }
  134. }
  135. } else {
  136. $panelContainer.css('background-color', '');
  137. elem.css('background-color', '');
  138. }
  139. elem.html(body);
  140. if (panel.sparkline.show) {
  141. addSparkline();
  142. }
  143. elem.toggleClass('pointer', panel.links.length > 0);
  144. if (panel.links.length > 0) {
  145. linkInfo = linkSrv.getPanelLinkAnchorInfo(panel.links[0], panel.scopedVars);
  146. } else {
  147. linkInfo = null;
  148. }
  149. }
  150. function hookupDrilldownLinkTooltip() {
  151. // drilldown link tooltip
  152. var drilldownTooltip = $('<div id="tooltip" class="">hello</div>"');
  153. elem.mouseleave(function() {
  154. if (panel.links.length === 0) { return;}
  155. drilldownTooltip.detach();
  156. });
  157. elem.click(function(evt) {
  158. if (!linkInfo) { return; }
  159. // ignore title clicks in title
  160. if ($(evt).parents('.panel-header').length > 0) { 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. function getColorForValue(data, value) {
  184. for (var i = data.thresholds.length; i > 0; i--) {
  185. if (value >= data.thresholds[i-1]) {
  186. return data.colorMap[i];
  187. }
  188. }
  189. return _.first(data.colorMap);
  190. }
  191. export {
  192. SingleStatPanel,
  193. SingleStatPanel as Panel,
  194. getColorForValue
  195. };