module.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. labjs = labjs.script("common/lib/panels/jquery.flot.js")
  2. .script("common/lib/panels/jquery.flot.pie.js")
  3. angular.module('kibana.pie', [])
  4. .controller('pie', function($scope, $rootScope) {
  5. var _id = _.uniqueId();
  6. // Set and populate defaults
  7. var _d = {
  8. query : "*",
  9. size : 100,
  10. exclude : [],
  11. donut : false,
  12. tilt : false,
  13. legend : true,
  14. labels : true,
  15. group : "default"
  16. }
  17. _.defaults($scope.panel,_d)
  18. $scope.init = function() {
  19. $scope.$on(_id+"-time", function(event,time){set_time(time)});
  20. $scope.$on($scope.panel.group+"-time", function(event,time){set_time(time)});
  21. if(!(_.isArray($scope.panel.query))) {
  22. $scope.$on($scope.panel.group+"-query", function(event, query) {
  23. $scope.panel.query.query = query;
  24. $scope.get_data();
  25. });
  26. }
  27. // Now that we're all setup, request the time from our group
  28. $rootScope.$broadcast($scope.panel.group+"-get_time",_id)
  29. }
  30. $scope.get_data = function() {
  31. // Make sure we have everything for the request to complete
  32. if(_.isUndefined($scope.panel.index) || _.isUndefined($scope.panel.time))
  33. return
  34. var request = $scope.ejs.Request().indices($scope.panel.index);
  35. // If we have an array, use query facet
  36. if(_.isArray($scope.panel.query)) {
  37. var queries = [];
  38. // Build the question part of the query
  39. _.each($scope.panel.query, function(v) {
  40. queries.push(ejs.FilteredQuery(
  41. ejs.QueryStringQuery(v.query || '*'),
  42. ejs.RangeFilter(config.timefield)
  43. .from($scope.panel.time.from)
  44. .to($scope.panel.time.to))
  45. )
  46. });
  47. // Then the insert into facet and make the request
  48. _.each(queries, function(v) {
  49. request = request.facet(ejs.QueryFacet(_.indexOf(queries,v))
  50. .query(v)
  51. .facetFilter(ejs.QueryFilter(v))
  52. )
  53. })
  54. var results = request.doSearch();
  55. // Populate scope when we have results
  56. results.then(function(results) {
  57. $scope.hits = results.hits.total;
  58. $scope.data = [];
  59. _.each(results.facets, function(v, k) {
  60. var series = {};
  61. var slice = { label : $scope.panel.query[k].label, data : v.count };
  62. if (!(_.isUndefined($scope.panel.query[k].color)))
  63. slice.color = $scope.panel.query[k].color;
  64. $scope.data.push(slice)
  65. });
  66. });
  67. // If we don't have an array, assume its a term facet.
  68. } else {
  69. var results = request
  70. .facet(ejs.TermsFacet('pie')
  71. .field($scope.panel.query.field)
  72. .size($scope.panel['size'])
  73. .exclude($scope.panel.exclude)
  74. .facetFilter(ejs.QueryFilter(
  75. ejs.FilteredQuery(
  76. ejs.QueryStringQuery($scope.panel.query.query || '*'),
  77. ejs.RangeFilter(config.timefield)
  78. .from($scope.panel.time.from)
  79. .to($scope.panel.time.to)
  80. .cache(false)
  81. )))).size(0)
  82. .doSearch();
  83. // Populate scope when we have results
  84. results.then(function(results) {
  85. $scope.hits = results.hits.total;
  86. $scope.data = [];
  87. var k = 0;
  88. _.each(results.facets.pie.terms, function(v) {
  89. var slice = { label : v.term, data : v.count };
  90. $scope.data.push();
  91. if(!(_.isUndefined($scope.panel.colors))
  92. && _.isArray($scope.panel.colors)
  93. && $scope.panel.colors.length > 0) {
  94. slice.color = $scope.panel.colors[k%$scope.panel.colors.length];
  95. }
  96. $scope.data.push(slice)
  97. k = k + 1;
  98. });
  99. });
  100. }
  101. }
  102. function set_time(time) {
  103. $scope.panel.time = time;
  104. $scope.panel.index = _.isUndefined(time.index) ? $scope.panel.index : time.index
  105. $scope.get_data();
  106. }
  107. // Ready, init
  108. $scope.init()
  109. })
  110. .directive('pie', function() {
  111. return {
  112. restrict: 'A',
  113. link: function(scope, elem, attrs) {
  114. // Watch if data or row state changes
  115. scope.$watch(function () {
  116. return angular.toJson([scope.data, scope.row])
  117. }, function() {
  118. if(!(_.isUndefined(scope.data)))
  119. render_panel(scope,elem,attrs);
  120. });
  121. // Or if the window is resized
  122. angular.element(window).bind('resize', function(){
  123. render_panel(scope,elem,attrs);
  124. });
  125. // Function for rendering panel
  126. function render_panel(scope,elem,attrs) {
  127. var pie = {
  128. series: {
  129. pie: {
  130. innerRadius: scope.panel.donut ? 0.4 : 0,
  131. tilt: scope.panel.tilt ? 0.45 : 1,
  132. radius: 1,
  133. show: true,
  134. combine: {
  135. color: '#999',
  136. label: 'The Rest'
  137. },
  138. label: {
  139. show: scope.panel.labels,
  140. radius: 2/3,
  141. formatter: function(label, series){
  142. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  143. label+'<br/>'+Math.round(series.percent)+'%</div>';
  144. },
  145. threshold: 0.1
  146. }
  147. }
  148. },
  149. //grid: { hoverable: true, clickable: true },
  150. grid: { hoverable: true, clickable: true },
  151. legend: { show: scope.panel.legend }
  152. };
  153. // Populate element
  154. if(elem.is(":visible")){
  155. $.plot(elem, scope.data, pie);
  156. }
  157. function piett(x, y, contents) {
  158. var tooltip = $('#pie-tooltip').length ?
  159. $('#pie-tooltip') : $('<div id="pie-tooltip"></div>');
  160. tooltip.text(contents).css({
  161. position: 'absolute',
  162. top : y + 10,
  163. left : x + 10,
  164. color : "#FFF",
  165. border : '1px solid #FFF',
  166. padding : '2px',
  167. 'font-size': '8pt',
  168. 'background-color': '#000',
  169. }).appendTo("body");
  170. }
  171. elem.bind("plothover", function (event, pos, item) {
  172. if (item) {
  173. var percent = parseFloat(item.series.percent).toFixed(1) + "%";
  174. piett(pos.pageX, pos.pageY, percent + " " + item.series.label);
  175. } else {
  176. $("#pie-tooltip").remove();
  177. }
  178. });
  179. }
  180. }
  181. };
  182. })