grafanaGraph.js 10 KB

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