module.ts 6.3 KB

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