module.js 8.5 KB

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