module.js 6.1 KB

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