grafanaGraph.js 10 KB

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