module.js 8.4 KB

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