legend.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 (firstRender) {
  78. elem.append($container);
  79. $container.on('click', '.graph-legend-icon', openColorSelector);
  80. $container.on('click', '.graph-legend-alias', toggleSeries);
  81. $container.on('click', 'th', sortLegend);
  82. firstRender = false;
  83. }
  84. seriesList = data;
  85. $container.empty();
  86. // Set min-width if side style and there is a value, otherwise remove the CSS propery
  87. var width = panel.legend.rightSide && panel.legend.sideWidth ? panel.legend.sideWidth + "px" : "";
  88. $container.css("min-width", width);
  89. $container.toggleClass('graph-legend-table', panel.legend.alignAsTable === true);
  90. if (panel.legend.alignAsTable) {
  91. var header = '<tr>';
  92. header += '<th colspan="2" style="text-align:left"></th>';
  93. if (panel.legend.values) {
  94. header += getTableHeaderHtml('min');
  95. header += getTableHeaderHtml('max');
  96. header += getTableHeaderHtml('avg');
  97. header += getTableHeaderHtml('current');
  98. header += getTableHeaderHtml('total');
  99. }
  100. header += '</tr>';
  101. $container.append($(header));
  102. }
  103. if (panel.legend.sort) {
  104. seriesList = _.sortBy(seriesList, function(series) {
  105. return series.stats[panel.legend.sort];
  106. });
  107. if (panel.legend.sortDesc) {
  108. seriesList = seriesList.reverse();
  109. }
  110. }
  111. for (i = 0; i < seriesList.length; i++) {
  112. var series = seriesList[i];
  113. // ignore empty series
  114. if (panel.legend.hideEmpty && series.allIsNull) {
  115. continue;
  116. }
  117. // ignore series excluded via override
  118. if (!series.legend) {
  119. continue;
  120. }
  121. // ignore zero series
  122. if (panel.legend.hideZero && series.allIsZero) {
  123. continue;
  124. }
  125. var html = '<div class="graph-legend-series';
  126. if (series.yaxis === 2) { html += ' pull-right'; }
  127. if (ctrl.hiddenSeries[series.alias]) { html += ' graph-legend-series-hidden'; }
  128. html += '" data-series-index="' + i + '">';
  129. html += '<div class="graph-legend-icon">';
  130. html += '<i class="fa fa-minus pointer" style="color:' + series.color + '"></i>';
  131. html += '</div>';
  132. html += '<div class="graph-legend-alias">';
  133. html += '<a>' + _.escape(series.label) + '</a>';
  134. html += '</div>';
  135. if (panel.legend.values) {
  136. var avg = series.formatValue(series.stats.avg);
  137. var current = series.formatValue(series.stats.current);
  138. var min = series.formatValue(series.stats.min);
  139. var max = series.formatValue(series.stats.max);
  140. var total = series.formatValue(series.stats.total);
  141. if (panel.legend.min) { html += '<div class="graph-legend-value min">' + min + '</div>'; }
  142. if (panel.legend.max) { html += '<div class="graph-legend-value max">' + max + '</div>'; }
  143. if (panel.legend.avg) { html += '<div class="graph-legend-value avg">' + avg + '</div>'; }
  144. if (panel.legend.current) { html += '<div class="graph-legend-value current">' + current + '</div>'; }
  145. if (panel.legend.total) { html += '<div class="graph-legend-value total">' + total + '</div>'; }
  146. }
  147. html += '</div>';
  148. $container.append($(html));
  149. }
  150. var legendContainerHeight = $container.parent().height();
  151. var legendHeight = $container.height();
  152. if (panel.legend.rightSide && legendHeight >= legendContainerHeight) {
  153. $container.toggleClass('graph-legend-fixed-height', true);
  154. }
  155. if (panel.legend.rightSide) {
  156. $container.css("height", scope.ctrl.height || scope.ctrl.panel.height || scope.ctrl.row.height);
  157. } else {
  158. $container.css("height", "");
  159. }
  160. }
  161. }
  162. };
  163. });
  164. });