module.ts 6.5 KB

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