legend.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, $timeout) {
  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. ctrl.events.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 series = seriesList[index];
  38. $timeout(function() {
  39. popoverSrv.show({
  40. element: el[0],
  41. position: 'bottom center',
  42. template: '<gf-color-picker></gf-color-picker>',
  43. model: {
  44. series: series,
  45. toggleAxis: function() {
  46. ctrl.toggleAxis(series);
  47. },
  48. colorSelected: function(color) {
  49. ctrl.changeSeriesColor(series, color);
  50. }
  51. },
  52. });
  53. });
  54. }
  55. function toggleSeries(e) {
  56. var el = $(e.currentTarget);
  57. var index = getSeriesIndexForElement(el);
  58. var seriesInfo = seriesList[index];
  59. ctrl.toggleSeries(seriesInfo, e);
  60. }
  61. function sortLegend(e) {
  62. var el = $(e.currentTarget);
  63. var stat = el.data('stat');
  64. if (stat !== panel.legend.sort) { panel.legend.sortDesc = null; }
  65. // if already sort ascending, disable sorting
  66. if (panel.legend.sortDesc === false) {
  67. panel.legend.sort = null;
  68. panel.legend.sortDesc = null;
  69. render();
  70. return;
  71. }
  72. panel.legend.sortDesc = !panel.legend.sortDesc;
  73. panel.legend.sort = stat;
  74. render();
  75. }
  76. function getTableHeaderHtml(statName) {
  77. if (!panel.legend[statName]) { return ""; }
  78. var html = '<th class="pointer" data-stat="' + statName + '">' + statName;
  79. if (panel.legend.sort === statName) {
  80. var cssClass = panel.legend.sortDesc ? 'fa fa-caret-down' : 'fa fa-caret-up' ;
  81. html += ' <span class="' + cssClass + '"></span>';
  82. }
  83. return html + '</th>';
  84. }
  85. function render() {
  86. if (!ctrl.panel.legend.show) {
  87. elem.empty();
  88. firstRender = true;
  89. return;
  90. }
  91. if (firstRender) {
  92. elem.append($container);
  93. $container.on('click', '.graph-legend-icon', openColorSelector);
  94. $container.on('click', '.graph-legend-alias', toggleSeries);
  95. $container.on('click', 'th', sortLegend);
  96. firstRender = false;
  97. }
  98. seriesList = data;
  99. $container.empty();
  100. // Set min-width if side style and there is a value, otherwise remove the CSS propery
  101. var width = panel.legend.rightSide && panel.legend.sideWidth ? panel.legend.sideWidth + "px" : "";
  102. $container.css("min-width", width);
  103. $container.toggleClass('graph-legend-table', panel.legend.alignAsTable === true);
  104. if (panel.legend.alignAsTable) {
  105. var header = '<tr>';
  106. header += '<th colspan="2" style="text-align:left"></th>';
  107. if (panel.legend.values) {
  108. header += getTableHeaderHtml('min');
  109. header += getTableHeaderHtml('max');
  110. header += getTableHeaderHtml('avg');
  111. header += getTableHeaderHtml('current');
  112. header += getTableHeaderHtml('total');
  113. }
  114. header += '</tr>';
  115. $container.append($(header));
  116. }
  117. if (panel.legend.sort) {
  118. seriesList = _.sortBy(seriesList, function(series) {
  119. return series.stats[panel.legend.sort];
  120. });
  121. if (panel.legend.sortDesc) {
  122. seriesList = seriesList.reverse();
  123. }
  124. }
  125. var seriesShown = 0;
  126. for (i = 0; i < seriesList.length; i++) {
  127. var series = seriesList[i];
  128. if (series.hideFromLegend(panel.legend)) {
  129. continue;
  130. }
  131. var html = '<div class="graph-legend-series';
  132. if (series.yaxis === 2) { html += ' graph-legend-series--right-y'; }
  133. if (ctrl.hiddenSeries[series.alias]) { html += ' graph-legend-series-hidden'; }
  134. html += '" data-series-index="' + i + '">';
  135. html += '<div class="graph-legend-icon">';
  136. html += '<i class="fa fa-minus pointer" style="color:' + series.color + '"></i>';
  137. html += '</div>';
  138. html += '<a class="graph-legend-alias pointer">' + _.escape(series.label) + '</a>';
  139. if (panel.legend.values) {
  140. var avg = series.formatValue(series.stats.avg);
  141. var current = series.formatValue(series.stats.current);
  142. var min = series.formatValue(series.stats.min);
  143. var max = series.formatValue(series.stats.max);
  144. var total = series.formatValue(series.stats.total);
  145. if (panel.legend.min) { html += '<div class="graph-legend-value min">' + min + '</div>'; }
  146. if (panel.legend.max) { html += '<div class="graph-legend-value max">' + max + '</div>'; }
  147. if (panel.legend.avg) { html += '<div class="graph-legend-value avg">' + avg + '</div>'; }
  148. if (panel.legend.current) { html += '<div class="graph-legend-value current">' + current + '</div>'; }
  149. if (panel.legend.total) { html += '<div class="graph-legend-value total">' + total + '</div>'; }
  150. }
  151. html += '</div>';
  152. $container.append($(html));
  153. seriesShown++;
  154. }
  155. if (panel.legend.alignAsTable) {
  156. var maxHeight = ctrl.height;
  157. if (!panel.legend.rightSide) {
  158. maxHeight = maxHeight/2;
  159. }
  160. var topPadding = 6;
  161. $container.css("max-height", maxHeight - topPadding);
  162. } else {
  163. $container.css("max-height", "");
  164. }
  165. }
  166. }
  167. };
  168. });
  169. });