module.ts 6.5 KB

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