grafanaGraph.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'kbn',
  5. 'moment',
  6. 'underscore'
  7. ],
  8. function (angular, $, kbn, moment, _) {
  9. 'use strict';
  10. var module = angular.module('kibana.directives');
  11. module.directive('grafanaGraph', function(filterSrv, $rootScope, dashboard) {
  12. return {
  13. restrict: 'A',
  14. template: '<div> </div>',
  15. link: function(scope, elem) {
  16. var data, plot;
  17. var hiddenData = {};
  18. scope.$on('refresh',function() {
  19. if (scope.otherPanelInFullscreenMode()) { return; }
  20. scope.get_data();
  21. });
  22. scope.$on('toggleLegend', function(e, alias) {
  23. if (hiddenData[alias]) {
  24. data.push(hiddenData[alias]);
  25. delete hiddenData[alias];
  26. }
  27. render_panel();
  28. });
  29. // Receive render events
  30. scope.$on('render',function(event, d) {
  31. data = d || data;
  32. render_panel();
  33. });
  34. // Re-render if the window is resized
  35. angular.element(window).bind('resize', function() {
  36. render_panel();
  37. });
  38. function setElementHeight() {
  39. try {
  40. elem.css({ height: scope.height || scope.panel.height || scope.row.height });
  41. return true;
  42. } catch(e) { // IE throws errors sometimes
  43. return false;
  44. }
  45. }
  46. // Function for rendering panel
  47. function render_panel() {
  48. if (!data) { return; }
  49. if (scope.otherPanelInFullscreenMode()) { return; }
  50. if (!setElementHeight()) { return; }
  51. if (_.isString(data)) {
  52. render_panel_as_graphite_png(data);
  53. return;
  54. }
  55. _.each(_.keys(scope.hiddenSeries), function(seriesAlias) {
  56. var dataSeries = _.find(data, function(series) {
  57. return series.info.alias === seriesAlias;
  58. });
  59. if (dataSeries) {
  60. hiddenData[dataSeries.info.alias] = dataSeries;
  61. data = _.without(data, dataSeries);
  62. }
  63. });
  64. // Set barwidth based on specified interval
  65. var barwidth = kbn.interval_to_ms(scope.interval);
  66. var stack = scope.panel.stack ? true : null;
  67. // Populate element
  68. var options = {
  69. legend: { show: false },
  70. series: {
  71. stackpercent: scope.panel.stack ? scope.panel.percentage : false,
  72. stack: scope.panel.percentage ? null : stack,
  73. lines: {
  74. show: scope.panel.lines,
  75. zero: false,
  76. fill: scope.panel.fill === 0 ? false : scope.panel.fill/10,
  77. lineWidth: scope.panel.linewidth,
  78. steps: scope.panel.steppedLine
  79. },
  80. bars: {
  81. show: scope.panel.bars,
  82. fill: 1,
  83. barWidth: barwidth/1.5,
  84. zero: false,
  85. lineWidth: 0
  86. },
  87. points: {
  88. show: scope.panel.points,
  89. fill: 1,
  90. fillColor: false,
  91. radius: scope.panel.pointradius
  92. },
  93. shadowSize: 1
  94. },
  95. yaxes: [],
  96. xaxis: {
  97. timezone: dashboard.current.timezone,
  98. show: scope.panel['x-axis'],
  99. mode: "time",
  100. min: _.isUndefined(scope.range.from) ? null : scope.range.from.getTime(),
  101. max: _.isUndefined(scope.range.to) ? null : scope.range.to.getTime(),
  102. timeformat: time_format(scope.interval),
  103. label: "Datetime",
  104. ticks: elem.width()/100
  105. },
  106. grid: {
  107. backgroundColor: null,
  108. borderWidth: 0,
  109. hoverable: true,
  110. color: '#c8c8c8'
  111. },
  112. selection: {
  113. mode: "x",
  114. color: '#666'
  115. }
  116. };
  117. addAnnotations(options);
  118. for (var i = 0; i < data.length; i++) {
  119. var _d = data[i].getFlotPairs(scope.panel.nullPointMode);
  120. data[i].data = _d;
  121. }
  122. configureAxisOptions(data, options);
  123. plot = $.plot(elem, data, options);
  124. addAxisLabels();
  125. }
  126. function render_panel_as_graphite_png(url) {
  127. url += '&width=' + elem.width();
  128. url += '&height=' + elem.css('height').replace('px', '');
  129. url += '&bgcolor=1f1f1f'; // @grayDarker & @kibanaPanelBackground
  130. url += '&fgcolor=BBBFC2'; // @textColor & @grayLighter
  131. url += scope.panel.stack ? '&areaMode=stacked' : '';
  132. url += scope.panel.fill !== 0 ? ('&areaAlpha=' + (scope.panel.fill/10).toFixed(1)) : '';
  133. url += scope.panel.linewidth !== 0 ? '&lineWidth=' + scope.panel.linewidth : '';
  134. url += scope.panel.legend ? '' : '&hideLegend=true';
  135. url += scope.panel.grid.min ? '&yMin=' + scope.panel.grid.min : '';
  136. url += scope.panel.grid.max ? '&yMax=' + scope.panel.grid.max : '';
  137. url += scope.panel['x-axis'] ? '' : '&hideAxes=true';
  138. url += scope.panel['y-axis'] ? '' : '&hideYAxis=true';
  139. switch(scope.panel.y_formats[0]) {
  140. case 'bytes':
  141. url += '&yUnitSystem=binary';
  142. break;
  143. case 'short':
  144. url += '&yUnitSystem=si';
  145. break;
  146. case 'none':
  147. url += '&yUnitSystem=none';
  148. break;
  149. }
  150. switch(scope.panel.nullPointMode) {
  151. case 'connected':
  152. url += '&lineMode=connected';
  153. break;
  154. case 'null':
  155. break; // graphite default lineMode
  156. case 'null as zero':
  157. url += "&drawNullAsZero=true";
  158. break;
  159. }
  160. url += scope.panel.steppedLine ? '&lineMode=staircase' : '';
  161. elem.html('<img src="' + url + '"></img>');
  162. }
  163. function addAnnotations(options) {
  164. if(!scope.panel.annotate.enable) {
  165. return;
  166. }
  167. options.events = {
  168. levels: 1,
  169. data: scope.annotations,
  170. types: {
  171. 'annotation': {
  172. level: 1,
  173. icon: {
  174. icon: "icon-tag icon-flip-vertical",
  175. size: 20,
  176. color: "#222",
  177. outline: "#bbb"
  178. }
  179. }
  180. }
  181. };
  182. }
  183. function addAxisLabels() {
  184. if (scope.panel.leftYAxisLabel) {
  185. elem.css('margin-left', '10px');
  186. var yaxisLabel = $("<div class='axisLabel yaxisLabel'></div>")
  187. .text(scope.panel.leftYAxisLabel)
  188. .appendTo(elem);
  189. yaxisLabel.css("margin-top", yaxisLabel.width() / 2 - 20);
  190. } else if (elem.css('margin-left')) {
  191. elem.css('margin-left', '');
  192. }
  193. }
  194. function configureAxisOptions(data, options) {
  195. var defaults = {
  196. position: 'left',
  197. show: scope.panel['y-axis'],
  198. min: null,
  199. max: null,
  200. };
  201. options.yaxes.push(defaults);
  202. if (_.findWhere(data, {yaxis: 2})) {
  203. var secondY = _.clone(defaults);
  204. secondY.position = 'right';
  205. options.yaxes.push(secondY);
  206. configureAxisMode(options.yaxes[1], scope.panel.y_formats[1]);
  207. }
  208. configureAxisMode(options.yaxes[0], scope.panel.y_formats[0]);
  209. }
  210. function configureAxisMode(axis, format) {
  211. if (format === 'bytes') {
  212. axis.mode = "byte";
  213. }
  214. if (format === 'short') {
  215. axis.tickFormatter = function(val) {
  216. return kbn.shortFormat(val,0);
  217. };
  218. }
  219. if (format === 'ms') {
  220. axis.tickFormatter = kbn.msFormat;
  221. }
  222. }
  223. function time_format(interval) {
  224. var _int = kbn.interval_to_seconds(interval);
  225. if(_int >= 2628000) {
  226. return "%Y-%m";
  227. }
  228. if(_int >= 10000) {
  229. return "%m/%d";
  230. }
  231. if(_int >= 3600) {
  232. return "%m/%d %H:%M";
  233. }
  234. if(_int >= 700) {
  235. return "%a %H:%M";
  236. }
  237. return "%H:%M";
  238. }
  239. var $tooltip = $('<div>');
  240. elem.bind("plothover", function (event, pos, item) {
  241. var group, value, timestamp;
  242. if (item) {
  243. if (item.series.info.alias || scope.panel.tooltip.query_as_alias) {
  244. group = '<small style="font-size:0.9em;">' +
  245. '<i class="icon-circle" style="color:'+item.series.color+';"></i>' + ' ' +
  246. (item.series.info.alias || item.series.info.query)+
  247. '</small><br>';
  248. } else {
  249. group = kbn.query_color_dot(item.series.color, 15) + ' ';
  250. }
  251. value = (scope.panel.stack && scope.panel.tooltip.value_type === 'individual') ?
  252. item.datapoint[1] - item.datapoint[2] :
  253. item.datapoint[1];
  254. if(item.series.info.y_format === 'bytes') {
  255. value = kbn.byteFormat(value, 2);
  256. }
  257. if(item.series.info.y_format === 'short') {
  258. value = kbn.shortFormat(value, 2);
  259. }
  260. if(item.series.info.y_format === 'ms') {
  261. value = kbn.msFormat(value);
  262. }
  263. timestamp = dashboard.current.timezone === 'browser' ?
  264. moment(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss') :
  265. moment.utc(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss');
  266. $tooltip
  267. .html(
  268. group + value + " @ " + timestamp
  269. )
  270. .place_tt(pos.pageX, pos.pageY);
  271. } else {
  272. $tooltip.detach();
  273. }
  274. });
  275. elem.bind("plotselected", function (event, ranges) {
  276. filterSrv.setTime({
  277. from : moment.utc(ranges.xaxis.from).toDate(),
  278. to : moment.utc(ranges.xaxis.to).toDate(),
  279. });
  280. });
  281. }
  282. };
  283. });
  284. });