module.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'moment',
  5. 'app/core/utils/kbn',
  6. 'app/core/time_series',
  7. 'app/features/panel/panel_meta',
  8. './seriesOverridesCtrl',
  9. './graph',
  10. './legend',
  11. ],
  12. function (angular, _, moment, kbn, TimeSeries, PanelMeta) {
  13. 'use strict';
  14. var module = angular.module('grafana.panels.graph');
  15. module.directive('grafanaPanelGraph', function() {
  16. return {
  17. controller: 'GraphCtrl',
  18. templateUrl: 'app/plugins/panels/graph/module.html',
  19. };
  20. });
  21. module.controller('GraphCtrl', function($scope, $rootScope, panelSrv, annotationsSrv, panelHelper, $q) {
  22. $scope.panelMeta = new PanelMeta({
  23. panelName: 'Graph',
  24. editIcon: "fa fa-bar-chart",
  25. fullscreen: true,
  26. metricsEditor: true,
  27. });
  28. $scope.panelMeta.addEditorTab('Axes & Grid', 'app/plugins/panels/graph/axisEditor.html');
  29. $scope.panelMeta.addEditorTab('Display Styles', 'app/plugins/panels/graph/styleEditor.html');
  30. $scope.panelMeta.addEditorTab('Time range', 'app/features/panel/partials/panelTime.html');
  31. $scope.panelMeta.addExtendedMenuItem('Export CSV', '', 'exportCsv()');
  32. $scope.panelMeta.addExtendedMenuItem('Toggle legend', '', 'toggleLegend()');
  33. // Set and populate defaults
  34. var _d = {
  35. // datasource name, null = default datasource
  36. datasource: null,
  37. // sets client side (flot) or native graphite png renderer (png)
  38. renderer: 'flot',
  39. // Show/hide the x-axis
  40. 'x-axis' : true,
  41. // Show/hide y-axis
  42. 'y-axis' : true,
  43. // y axis formats, [left axis,right axis]
  44. y_formats : ['short', 'short'],
  45. // grid options
  46. grid : {
  47. leftLogBase: 1,
  48. leftMax: null,
  49. rightMax: null,
  50. leftMin: null,
  51. rightMin: null,
  52. rightLogBase: 1,
  53. threshold1: null,
  54. threshold2: null,
  55. threshold1Color: 'rgba(216, 200, 27, 0.27)',
  56. threshold2Color: 'rgba(234, 112, 112, 0.22)'
  57. },
  58. // show/hide lines
  59. lines : true,
  60. // fill factor
  61. fill : 1,
  62. // line width in pixels
  63. linewidth : 2,
  64. // show hide points
  65. points : false,
  66. // point radius in pixels
  67. pointradius : 5,
  68. // show hide bars
  69. bars : false,
  70. // enable/disable stacking
  71. stack : false,
  72. // stack percentage mode
  73. percentage : false,
  74. // legend options
  75. legend: {
  76. show: true, // disable/enable legend
  77. values: false, // disable/enable legend values
  78. min: false,
  79. max: false,
  80. current: false,
  81. total: false,
  82. avg: false
  83. },
  84. // how null points should be handled
  85. nullPointMode : 'connected',
  86. // staircase line mode
  87. steppedLine: false,
  88. // tooltip options
  89. tooltip : {
  90. value_type: 'cumulative',
  91. shared: true,
  92. },
  93. // time overrides
  94. timeFrom: null,
  95. timeShift: null,
  96. // metric queries
  97. targets: [{}],
  98. // series color overrides
  99. aliasColors: {},
  100. // other style overrides
  101. seriesOverrides: [],
  102. };
  103. _.defaults($scope.panel,_d);
  104. _.defaults($scope.panel.tooltip, _d.tooltip);
  105. _.defaults($scope.panel.annotate, _d.annotate);
  106. _.defaults($scope.panel.grid, _d.grid);
  107. _.defaults($scope.panel.legend, _d.legend);
  108. $scope.logScales = {'linear': 1, 'log (base 2)': 2, 'log (base 10)': 10, 'log (base 32)': 32, 'log (base 1024)': 1024};
  109. $scope.hiddenSeries = {};
  110. $scope.seriesList = [];
  111. $scope.unitFormats = kbn.getUnitFormats();
  112. $scope.setUnitFormat = function(axis, subItem) {
  113. $scope.panel.y_formats[axis] = subItem.value;
  114. $scope.render();
  115. };
  116. $scope.refreshData = function(datasource) {
  117. panelHelper.updateTimeRange($scope);
  118. $scope.annotationsPromise = annotationsSrv.getAnnotations($scope.dashboard);
  119. return panelHelper.issueMetricQuery($scope, datasource)
  120. .then($scope.dataHandler, function(err) {
  121. $scope.seriesList = [];
  122. $scope.render([]);
  123. throw err;
  124. });
  125. };
  126. $scope.zoomOut = function(evt) {
  127. $scope.appEvent('zoom-out', evt);
  128. };
  129. $scope.loadSnapshot = function(snapshotData) {
  130. panelHelper.updateTimeRange($scope);
  131. $scope.annotationsPromise = $q.when([]);
  132. $scope.dataHandler(snapshotData);
  133. };
  134. $scope.dataHandler = function(results) {
  135. // png renderer returns just a url
  136. if (_.isString(results)) {
  137. $scope.render(results);
  138. return;
  139. }
  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. });