module.js 8.5 KB

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