legend.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import PerfectScrollbar from 'perfect-scrollbar';
  5. var module = angular.module('grafana.directives');
  6. module.directive('graphLegend', function(popoverSrv, $timeout) {
  7. return {
  8. link: function(scope, elem) {
  9. var $container = $('<section class="graph-legend"></section>');
  10. var firstRender = true;
  11. var ctrl = scope.ctrl;
  12. var panel = ctrl.panel;
  13. var data;
  14. var seriesList;
  15. var i;
  16. var legendScrollbar;
  17. ctrl.events.on('render-legend', () => {
  18. data = ctrl.seriesList;
  19. if (data) {
  20. render();
  21. }
  22. ctrl.events.emit('legend-rendering-complete');
  23. });
  24. function getSeriesIndexForElement(el) {
  25. return el.parents('[data-series-index]').data('series-index');
  26. }
  27. function openColorSelector(e) {
  28. // if we clicked inside poup container ignore click
  29. if ($(e.target).parents('.popover').length) {
  30. return;
  31. }
  32. var el = $(e.currentTarget).find('.fa-minus');
  33. var index = getSeriesIndexForElement(el);
  34. var series = seriesList[index];
  35. $timeout(function() {
  36. popoverSrv.show({
  37. element: el[0],
  38. position: 'bottom center',
  39. template: '<series-color-picker series="series" onToggleAxis="toggleAxis" onColorChange="colorSelected">' +
  40. '</series-color-picker>',
  41. openOn: 'hover',
  42. model: {
  43. series: series,
  44. toggleAxis: function() {
  45. ctrl.toggleAxis(series);
  46. },
  47. colorSelected: function(color) {
  48. ctrl.changeSeriesColor(series, color);
  49. }
  50. },
  51. });
  52. });
  53. }
  54. function toggleSeries(e) {
  55. var el = $(e.currentTarget);
  56. var index = getSeriesIndexForElement(el);
  57. var seriesInfo = seriesList[index];
  58. var scrollPosition = $($container.children('tbody')).scrollTop();
  59. ctrl.toggleSeries(seriesInfo, e);
  60. $($container.children('tbody')).scrollTop(scrollPosition);
  61. }
  62. function sortLegend(e) {
  63. var el = $(e.currentTarget);
  64. var stat = el.data('stat');
  65. if (stat !== panel.legend.sort) { panel.legend.sortDesc = null; }
  66. // if already sort ascending, disable sorting
  67. if (panel.legend.sortDesc === false) {
  68. panel.legend.sort = null;
  69. panel.legend.sortDesc = null;
  70. ctrl.render();
  71. return;
  72. }
  73. panel.legend.sortDesc = !panel.legend.sortDesc;
  74. panel.legend.sort = stat;
  75. ctrl.render();
  76. }
  77. function getTableHeaderHtml(statName) {
  78. if (!panel.legend[statName]) { return ""; }
  79. var html = '<th class="pointer" data-stat="' + statName + '">' + statName;
  80. if (panel.legend.sort === statName) {
  81. var cssClass = panel.legend.sortDesc ? 'fa fa-caret-down' : 'fa fa-caret-up' ;
  82. html += ' <span class="' + cssClass + '"></span>';
  83. }
  84. return html + '</th>';
  85. }
  86. function render() {
  87. if (!ctrl.panel.legend.show) {
  88. elem.empty();
  89. firstRender = true;
  90. return;
  91. }
  92. if (firstRender) {
  93. elem.append($container);
  94. $container.on('click', '.graph-legend-icon', openColorSelector);
  95. $container.on('click', '.graph-legend-alias', toggleSeries);
  96. $container.on('click', 'th', sortLegend);
  97. firstRender = false;
  98. }
  99. seriesList = data;
  100. $container.empty();
  101. // Set min-width if side style and there is a value, otherwise remove the CSS propery
  102. var width = panel.legend.rightSide && panel.legend.sideWidth ? panel.legend.sideWidth + "px" : "";
  103. $container.css("min-width", width);
  104. $container.toggleClass('graph-legend-table', panel.legend.alignAsTable === true);
  105. var tableHeaderElem;
  106. if (panel.legend.alignAsTable) {
  107. var header = '<tr>';
  108. header += '<th colspan="2" style="text-align:left"></th>';
  109. if (panel.legend.values) {
  110. header += getTableHeaderHtml('min');
  111. header += getTableHeaderHtml('max');
  112. header += getTableHeaderHtml('avg');
  113. header += getTableHeaderHtml('current');
  114. header += getTableHeaderHtml('total');
  115. }
  116. header += '</tr>';
  117. tableHeaderElem = $(header);
  118. }
  119. if (panel.legend.sort) {
  120. seriesList = _.sortBy(seriesList, function(series) {
  121. return series.stats[panel.legend.sort];
  122. });
  123. if (panel.legend.sortDesc) {
  124. seriesList = seriesList.reverse();
  125. }
  126. }
  127. var seriesElements = [];
  128. for (i = 0; i < seriesList.length; i++) {
  129. var series = seriesList[i];
  130. if (series.hideFromLegend(panel.legend)) {
  131. continue;
  132. }
  133. var html = '<div class="graph-legend-series';
  134. if (series.yaxis === 2) { html += ' graph-legend-series--right-y'; }
  135. if (ctrl.hiddenSeries[series.alias]) { html += ' graph-legend-series-hidden'; }
  136. html += '" data-series-index="' + i + '">';
  137. html += '<div class="graph-legend-icon">';
  138. html += '<i class="fa fa-minus pointer" style="color:' + series.color + '"></i>';
  139. html += '</div>';
  140. html += '<a class="graph-legend-alias pointer" title="' + series.aliasEscaped + '">' + series.aliasEscaped + '</a>';
  141. if (panel.legend.values) {
  142. var avg = series.formatValue(series.stats.avg);
  143. var current = series.formatValue(series.stats.current);
  144. var min = series.formatValue(series.stats.min);
  145. var max = series.formatValue(series.stats.max);
  146. var total = series.formatValue(series.stats.total);
  147. if (panel.legend.min) { html += '<div class="graph-legend-value min">' + min + '</div>'; }
  148. if (panel.legend.max) { html += '<div class="graph-legend-value max">' + max + '</div>'; }
  149. if (panel.legend.avg) { html += '<div class="graph-legend-value avg">' + avg + '</div>'; }
  150. if (panel.legend.current) { html += '<div class="graph-legend-value current">' + current + '</div>'; }
  151. if (panel.legend.total) { html += '<div class="graph-legend-value total">' + total + '</div>'; }
  152. }
  153. html += '</div>';
  154. seriesElements.push($(html));
  155. }
  156. if (panel.legend.alignAsTable) {
  157. var maxHeight = ctrl.height;
  158. if (!panel.legend.rightSide) {
  159. maxHeight = maxHeight/2;
  160. }
  161. var topPadding = 6;
  162. var tbodyElem = $('<tbody></tbody>');
  163. tbodyElem.css("max-height", maxHeight - topPadding);
  164. tbodyElem.append(tableHeaderElem);
  165. tbodyElem.append(seriesElements);
  166. $container.append(tbodyElem);
  167. } else {
  168. var maxLegendHeight = ctrl.height / 2;
  169. $container.css("max-height", maxLegendHeight - 6);
  170. $container.append(seriesElements);
  171. if (!legendScrollbar) {
  172. legendScrollbar = new PerfectScrollbar($container[0]);
  173. }
  174. legendScrollbar.update();
  175. }
  176. }
  177. }
  178. };
  179. });