legend.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. 'jquery.flot',
  6. 'jquery.flot.time',
  7. ],
  8. function (angular, _, $) {
  9. 'use strict';
  10. var module = angular.module('grafana.directives');
  11. module.directive('graphLegend', function(popoverSrv) {
  12. return {
  13. link: function(scope, elem) {
  14. var $container = $('<section class="graph-legend"></section>');
  15. var firstRender = true;
  16. var ctrl = scope.ctrl;
  17. var panel = ctrl.panel;
  18. var data;
  19. var seriesList;
  20. var i;
  21. scope.$on('render', function() {
  22. data = ctrl.seriesList;
  23. if (data) {
  24. render();
  25. }
  26. });
  27. function getSeriesIndexForElement(el) {
  28. return el.parents('[data-series-index]').data('series-index');
  29. }
  30. function openColorSelector(e) {
  31. // if we clicked inside poup container ignore click
  32. if ($(e.target).parents('.popover').length) {
  33. return;
  34. }
  35. var el = $(e.currentTarget).find('.fa-minus');
  36. var index = getSeriesIndexForElement(el);
  37. var seriesInfo = seriesList[index];
  38. var popoverScope = scope.$new();
  39. popoverScope.series = seriesInfo;
  40. popoverSrv.show({
  41. element: el,
  42. templateUrl: 'public/app/plugins/panel/graph/legend.popover.html',
  43. scope: popoverScope
  44. });
  45. }
  46. function toggleSeries(e) {
  47. var el = $(e.currentTarget);
  48. var index = getSeriesIndexForElement(el);
  49. var seriesInfo = seriesList[index];
  50. ctrl.toggleSeries(seriesInfo, e);
  51. }
  52. function sortLegend(e) {
  53. var el = $(e.currentTarget);
  54. var stat = el.data('stat');
  55. if (stat !== panel.legend.sort) { panel.legend.sortDesc = null; }
  56. // if already sort ascending, disable sorting
  57. if (panel.legend.sortDesc === false) {
  58. panel.legend.sort = null;
  59. panel.legend.sortDesc = null;
  60. render();
  61. return;
  62. }
  63. panel.legend.sortDesc = !panel.legend.sortDesc;
  64. panel.legend.sort = stat;
  65. render();
  66. }
  67. function getTableHeaderHtml(statName) {
  68. if (!panel.legend[statName]) { return ""; }
  69. var html = '<th class="pointer" data-stat="' + statName + '">' + statName;
  70. if (panel.legend.sort === statName) {
  71. var cssClass = panel.legend.sortDesc ? 'fa fa-caret-down' : 'fa fa-caret-up' ;
  72. html += ' <span class="' + cssClass + '"></span>';
  73. }
  74. return html + '</th>';
  75. }
  76. function render() {
  77. if (panel.legend.rightSide) {
  78. var panelheight = scope.ctrl.height || scope.ctrl.panel.height || scope.ctrl.row.height;
  79. $container.css("height", panelheight);
  80. $container.toggleClass('graph-legend-fixed-height', true);
  81. } else {
  82. $container.css("height", "");
  83. }
  84. if (firstRender) {
  85. elem.append($container);
  86. $container.on('click', '.graph-legend-icon', openColorSelector);
  87. $container.on('click', '.graph-legend-alias', toggleSeries);
  88. $container.on('click', 'th', sortLegend);
  89. firstRender = false;
  90. }
  91. seriesList = data;
  92. $container.empty();
  93. // Set min-width if side style and there is a value, otherwise remove the CSS propery
  94. var width = panel.legend.rightSide && panel.legend.sideWidth ? panel.legend.sideWidth + "px" : "";
  95. $container.css("min-width", width);
  96. $container.toggleClass('graph-legend-table', panel.legend.alignAsTable === true);
  97. if (panel.legend.alignAsTable) {
  98. var header = '<tr>';
  99. header += '<th colspan="2" style="text-align:left"></th>';
  100. if (panel.legend.values) {
  101. header += getTableHeaderHtml('min');
  102. header += getTableHeaderHtml('max');
  103. header += getTableHeaderHtml('avg');
  104. header += getTableHeaderHtml('current');
  105. header += getTableHeaderHtml('total');
  106. }
  107. header += '</tr>';
  108. $container.append($(header));
  109. }
  110. if (panel.legend.sort) {
  111. seriesList = _.sortBy(seriesList, function(series) {
  112. return series.stats[panel.legend.sort];
  113. });
  114. if (panel.legend.sortDesc) {
  115. seriesList = seriesList.reverse();
  116. }
  117. }
  118. for (i = 0; i < seriesList.length; i++) {
  119. var series = seriesList[i];
  120. // ignore empty series
  121. if (panel.legend.hideEmpty && series.allIsNull) {
  122. continue;
  123. }
  124. // ignore series excluded via override
  125. if (!series.legend) {
  126. continue;
  127. }
  128. // ignore zero series
  129. if (panel.legend.hideZero && series.allIsZero) {
  130. continue;
  131. }
  132. var html = '<div class="graph-legend-series';
  133. if (series.yaxis === 2) { html += ' pull-right'; }
  134. if (ctrl.hiddenSeries[series.alias]) { html += ' graph-legend-series-hidden'; }
  135. html += '" data-series-index="' + i + '">';
  136. html += '<div class="graph-legend-icon">';
  137. html += '<i class="fa fa-minus pointer" style="color:' + series.color + '"></i>';
  138. html += '</div>';
  139. html += '<div class="graph-legend-alias">';
  140. html += '<a>' + _.escape(series.label) + '</a>';
  141. html += '</div>';
  142. if (panel.legend.values) {
  143. var avg = series.formatValue(series.stats.avg);
  144. var current = series.formatValue(series.stats.current);
  145. var min = series.formatValue(series.stats.min);
  146. var max = series.formatValue(series.stats.max);
  147. var total = series.formatValue(series.stats.total);
  148. if (panel.legend.min) { html += '<div class="graph-legend-value min">' + min + '</div>'; }
  149. if (panel.legend.max) { html += '<div class="graph-legend-value max">' + max + '</div>'; }
  150. if (panel.legend.avg) { html += '<div class="graph-legend-value avg">' + avg + '</div>'; }
  151. if (panel.legend.current) { html += '<div class="graph-legend-value current">' + current + '</div>'; }
  152. if (panel.legend.total) { html += '<div class="graph-legend-value total">' + total + '</div>'; }
  153. }
  154. html += '</div>';
  155. $container.append($(html));
  156. }
  157. }
  158. }
  159. };
  160. });
  161. });