module.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. define([
  2. 'angular',
  3. 'app',
  4. 'jquery',
  5. 'lodash',
  6. 'kbn',
  7. 'moment',
  8. 'components/timeSeries',
  9. 'components/panelmeta',
  10. './seriesOverridesCtrl',
  11. './graph',
  12. './legend',
  13. ],
  14. function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) {
  15. 'use strict';
  16. var module = angular.module('grafana.panels.graph');
  17. module.directive('grafanaPanelGraph', function() {
  18. return {
  19. controller: 'GraphCtrl',
  20. templateUrl: 'app/panels/graph/module.html',
  21. };
  22. });
  23. module.controller('GraphCtrl', function($scope, $rootScope, panelSrv, annotationsSrv, panelHelper, $q) {
  24. $scope.panelMeta = new PanelMeta({
  25. panelName: 'Graph',
  26. editIcon: "fa fa-bar-chart",
  27. fullscreen: true,
  28. metricsEditor: true,
  29. });
  30. $scope.panelMeta.addEditorTab('Axes & Grid', 'app/panels/graph/axisEditor.html');
  31. $scope.panelMeta.addEditorTab('Display Styles', 'app/panels/graph/styleEditor.html');
  32. $scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
  33. $scope.panelMeta.addExtendedMenuItem('Export CSV', '', 'exportCsv()');
  34. $scope.panelMeta.addExtendedMenuItem('Toggle legend', '', 'toggleLegend()');
  35. // Set and populate defaults
  36. var _d = {
  37. // datasource name, null = default datasource
  38. datasource: null,
  39. // sets client side (flot) or native graphite png renderer (png)
  40. renderer: 'flot',
  41. // Show/hide the x-axis
  42. 'x-axis' : true,
  43. // Show/hide y-axis
  44. 'y-axis' : true,
  45. // y axis formats, [left axis,right axis]
  46. y_formats : ['short', 'short'],
  47. // grid options
  48. grid : {
  49. leftLogBase: 1,
  50. leftMax: null,
  51. rightMax: null,
  52. leftMin: null,
  53. rightMin: null,
  54. rightLogBase: 1,
  55. threshold1: null,
  56. threshold2: null,
  57. threshold1Color: 'rgba(216, 200, 27, 0.27)',
  58. threshold2Color: 'rgba(234, 112, 112, 0.22)'
  59. },
  60. // show/hide lines
  61. lines : true,
  62. // fill factor
  63. fill : 1,
  64. // line width in pixels
  65. linewidth : 2,
  66. // show hide points
  67. points : false,
  68. // point radius in pixels
  69. pointradius : 5,
  70. // show hide bars
  71. bars : false,
  72. // enable/disable stacking
  73. stack : false,
  74. // stack percentage mode
  75. percentage : false,
  76. // legend options
  77. legend: {
  78. show: true, // disable/enable legend
  79. values: false, // disable/enable legend values
  80. min: false,
  81. max: false,
  82. current: false,
  83. total: false,
  84. avg: false
  85. },
  86. // how null points should be handled
  87. nullPointMode : 'connected',
  88. // staircase line mode
  89. steppedLine: false,
  90. // tooltip options
  91. tooltip : {
  92. value_type: 'cumulative',
  93. shared: true,
  94. },
  95. // time overrides
  96. timeFrom: null,
  97. timeShift: null,
  98. // metric queries
  99. targets: [{}],
  100. // series color overrides
  101. aliasColors: {},
  102. // other style overrides
  103. seriesOverrides: [],
  104. };
  105. _.defaults($scope.panel,_d);
  106. _.defaults($scope.panel.tooltip, _d.tooltip);
  107. _.defaults($scope.panel.annotate, _d.annotate);
  108. _.defaults($scope.panel.grid, _d.grid);
  109. _.defaults($scope.panel.legend, _d.legend);
  110. $scope.logScales = {'linear': 1, 'log (base 2)': 2, 'log (base 10)': 10, 'log (base 32)': 32, 'log (base 1024)': 1024};
  111. $scope.hiddenSeries = {};
  112. $scope.seriesList = [];
  113. $scope.unitFormats = kbn.getUnitFormats();
  114. $scope.setUnitFormat = function(axis, subItem) {
  115. $scope.panel.y_formats[axis] = subItem.value;
  116. $scope.render();
  117. };
  118. $scope.refreshData = function(datasource) {
  119. panelHelper.updateTimeRange($scope);
  120. $scope.annotationsPromise = annotationsSrv.getAnnotations($scope.rangeUnparsed, $scope.dashboard);
  121. return panelHelper.issueMetricQuery($scope, datasource)
  122. .then($scope.dataHandler, function(err) {
  123. $scope.seriesList = [];
  124. $scope.render([]);
  125. throw err;
  126. });
  127. };
  128. $scope.loadSnapshot = function(snapshotData) {
  129. panelHelper.updateTimeRange($scope);
  130. $scope.annotationsPromise = $q.when([]);
  131. $scope.dataHandler(snapshotData);
  132. };
  133. $scope.dataHandler = function(results) {
  134. // png renderer returns just a url
  135. if (_.isString(results)) {
  136. $scope.render(results);
  137. return;
  138. }
  139. console.log('graph data', results);
  140. $scope.datapointsWarning = false;
  141. $scope.datapointsCount = 0;
  142. $scope.datapointsOutside = false;
  143. $scope.seriesList = _.map(results.data, $scope.seriesHandler);
  144. $scope.datapointsWarning = $scope.datapointsCount === 0 || $scope.datapointsOutside;
  145. $scope.annotationsPromise
  146. .then(function(annotations) {
  147. $scope.panelMeta.loading = false;
  148. $scope.seriesList.annotations = annotations;
  149. $scope.render($scope.seriesList);
  150. }, function() {
  151. $scope.panelMeta.loading = false;
  152. $scope.render($scope.seriesList);
  153. });
  154. };
  155. $scope.seriesHandler = function(seriesData, index) {
  156. var datapoints = seriesData.datapoints;
  157. var alias = seriesData.target;
  158. var colorIndex = index % $rootScope.colors.length;
  159. var color = $scope.panel.aliasColors[alias] || $rootScope.colors[colorIndex];
  160. var series = new TimeSeries({
  161. datapoints: datapoints,
  162. alias: alias,
  163. color: color,
  164. });
  165. if (datapoints && datapoints.length > 0) {
  166. var last = moment.utc(datapoints[datapoints.length - 1][1]);
  167. var from = moment.utc($scope.range.from);
  168. if (last - from < -10000) {
  169. $scope.datapointsOutside = true;
  170. }
  171. $scope.datapointsCount += datapoints.length;
  172. }
  173. return series;
  174. };
  175. $scope.render = function(data) {
  176. panelHelper.broadcastRender($scope, data);
  177. };
  178. $scope.changeSeriesColor = function(series, color) {
  179. series.color = color;
  180. $scope.panel.aliasColors[series.alias] = series.color;
  181. $scope.render();
  182. };
  183. $scope.toggleSeries = function(serie, event) {
  184. if (event.ctrlKey || event.metaKey || event.shiftKey) {
  185. if ($scope.hiddenSeries[serie.alias]) {
  186. delete $scope.hiddenSeries[serie.alias];
  187. }
  188. else {
  189. $scope.hiddenSeries[serie.alias] = true;
  190. }
  191. } else {
  192. $scope.toggleSeriesExclusiveMode(serie);
  193. }
  194. $scope.render();
  195. };
  196. $scope.toggleSeriesExclusiveMode = function(serie) {
  197. var hidden = $scope.hiddenSeries;
  198. if (hidden[serie.alias]) {
  199. delete hidden[serie.alias];
  200. }
  201. // check if every other series is hidden
  202. var alreadyExclusive = _.every($scope.seriesList, function(value) {
  203. if (value.alias === serie.alias) {
  204. return true;
  205. }
  206. return hidden[value.alias];
  207. });
  208. if (alreadyExclusive) {
  209. // remove all hidden series
  210. _.each($scope.seriesList, function(value) {
  211. delete $scope.hiddenSeries[value.alias];
  212. });
  213. }
  214. else {
  215. // hide all but this serie
  216. _.each($scope.seriesList, function(value) {
  217. if (value.alias === serie.alias) {
  218. return;
  219. }
  220. $scope.hiddenSeries[value.alias] = true;
  221. });
  222. }
  223. };
  224. $scope.toggleYAxis = function(info) {
  225. var override = _.findWhere($scope.panel.seriesOverrides, { alias: info.alias });
  226. if (!override) {
  227. override = { alias: info.alias };
  228. $scope.panel.seriesOverrides.push(override);
  229. }
  230. override.yaxis = info.yaxis === 2 ? 1 : 2;
  231. $scope.render();
  232. };
  233. $scope.addSeriesOverride = function(override) {
  234. $scope.panel.seriesOverrides.push(override || {});
  235. };
  236. $scope.removeSeriesOverride = function(override) {
  237. $scope.panel.seriesOverrides = _.without($scope.panel.seriesOverrides, override);
  238. $scope.render();
  239. };
  240. // Called from panel menu
  241. $scope.toggleLegend = function() {
  242. $scope.panel.legend.show = !$scope.panel.legend.show;
  243. $scope.get_data();
  244. };
  245. $scope.legendValuesOptionChanged = function() {
  246. var legend = $scope.panel.legend;
  247. legend.values = legend.min || legend.max || legend.avg || legend.current || legend.total;
  248. $scope.render();
  249. };
  250. $scope.exportCsv = function() {
  251. kbn.exportSeriesListToCsv($scope.seriesList);
  252. };
  253. panelSrv.init($scope);
  254. });
  255. });