module.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import angular from 'angular';
  5. import {SingleStatCtrl} from './controller';
  6. angular.module('grafana.directives').directive('singleStatPanel', singleStatPanel);
  7. function singleStatPanel($location, linkSrv, $timeout, templateSrv) {
  8. 'use strict';
  9. return {
  10. controller: SingleStatCtrl,
  11. templateUrl: 'app/plugins/panel/singlestat/module.html',
  12. link: function(scope, elem) {
  13. var data, panel, linkInfo, $panelContainer;
  14. var firstRender = true;
  15. scope.$on('render', function() {
  16. if (firstRender) {
  17. var inner = elem.find('.singlestat-panel');
  18. if (inner.length) {
  19. elem = inner;
  20. $panelContainer = elem.parents('.panel-container');
  21. firstRender = false;
  22. hookupDrilldownLinkTooltip();
  23. }
  24. }
  25. render();
  26. scope.panelRenderingComplete();
  27. });
  28. function setElementHeight() {
  29. try {
  30. var height = scope.height || panel.height || scope.row.height;
  31. if (_.isString(height)) {
  32. height = parseInt(height.replace('px', ''), 10);
  33. }
  34. height -= 5; // padding
  35. height -= panel.title ? 24 : 9; // subtract panel title bar
  36. elem.css('height', height + 'px');
  37. return true;
  38. } catch (e) { // IE throws errors sometimes
  39. return false;
  40. }
  41. }
  42. function applyColoringThresholds(value, valueString) {
  43. if (!panel.colorValue) {
  44. return valueString;
  45. }
  46. var color = getColorForValue(value);
  47. if (color) {
  48. return '<span style="color:' + color + '">'+ valueString + '</span>';
  49. }
  50. return valueString;
  51. }
  52. function getColorForValue(value) {
  53. for (var i = data.thresholds.length - 1; i >= 0 ; i--) {
  54. if (value >= data.thresholds[i]) {
  55. return data.colorMap[i];
  56. }
  57. }
  58. return null;
  59. }
  60. function getSpan(className, fontSize, value) {
  61. value = templateSrv.replace(value);
  62. return '<span class="' + className + '" style="font-size:' + fontSize + '">' +
  63. value + '</span>';
  64. }
  65. function getBigValueHtml() {
  66. var body = '<div class="singlestat-panel-value-container">';
  67. if (panel.prefix) { body += getSpan('singlestat-panel-prefix', panel.prefixFontSize, scope.panel.prefix); }
  68. var value = applyColoringThresholds(data.valueRounded, data.valueFormated);
  69. body += getSpan('singlestat-panel-value', panel.valueFontSize, value);
  70. if (panel.postfix) { body += getSpan('singlestat-panel-postfix', panel.postfixFontSize, panel.postfix); }
  71. body += '</div>';
  72. return body;
  73. }
  74. function addSparkline() {
  75. var panel = scope.panel;
  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: scope.range.from.valueOf(),
  109. max: scope.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 (!scope.data) { return; }
  122. data = scope.data;
  123. panel = scope.panel;
  124. setElementHeight();
  125. var body = getBigValueHtml();
  126. if (panel.colorBackground && !isNaN(data.valueRounded)) {
  127. var color = getColorForValue(data.valueRounded);
  128. if (color) {
  129. $panelContainer.css('background-color', color);
  130. if (scope.fullscreen) {
  131. elem.css('background-color', color);
  132. } else {
  133. elem.css('background-color', '');
  134. }
  135. }
  136. } else {
  137. $panelContainer.css('background-color', '');
  138. elem.css('background-color', '');
  139. }
  140. elem.html(body);
  141. if (panel.sparkline.show) {
  142. addSparkline();
  143. }
  144. elem.toggleClass('pointer', panel.links.length > 0);
  145. if (panel.links.length > 0) {
  146. linkInfo = linkSrv.getPanelLinkAnchorInfo(panel.links[0], scope.panel.scopedVars);
  147. } else {
  148. linkInfo = null;
  149. }
  150. }
  151. function hookupDrilldownLinkTooltip() {
  152. // drilldown link tooltip
  153. var drilldownTooltip = $('<div id="tooltip" class="">hello</div>"');
  154. elem.mouseleave(function() {
  155. if (panel.links.length === 0) { return;}
  156. drilldownTooltip.detach();
  157. });
  158. elem.click(function(evt) {
  159. if (!linkInfo) { return; }
  160. // ignore title clicks in title
  161. if ($(evt).parents('.panel-header').length > 0) { return; }
  162. if (linkInfo.target === '_blank') {
  163. var redirectWindow = window.open(linkInfo.href, '_blank');
  164. redirectWindow.location;
  165. return;
  166. }
  167. if (linkInfo.href.indexOf('http') === 0) {
  168. window.location.href = linkInfo.href;
  169. } else {
  170. $timeout(function() {
  171. $location.url(linkInfo.href);
  172. });
  173. }
  174. drilldownTooltip.detach();
  175. });
  176. elem.mousemove(function(e) {
  177. if (!linkInfo) { return;}
  178. drilldownTooltip.text('click to go to: ' + linkInfo.title);
  179. drilldownTooltip.place_tt(e.pageX+20, e.pageY-15);
  180. });
  181. }
  182. }
  183. };
  184. }
  185. export {singleStatPanel as panel};