module.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, $location) {
  5. // Set and populate defaults
  6. var _d = {
  7. query : "*",
  8. size : 100,
  9. exclude : [],
  10. donut : false,
  11. tilt : false,
  12. legend : true,
  13. }
  14. _.each(_d, function(v, k) {
  15. $scope.panel[k] = _.isUndefined($scope.panel[k])
  16. ? _d[k] : $scope.panel[k];
  17. });
  18. if (!(_.isUndefined($scope.panel.group)) && !(_.isArray($scope.panel.query))) {
  19. $scope.$on($scope.panel.group+"-query", function(event, query) {
  20. $scope.panel.query.query = query;
  21. $scope.get_data();
  22. });
  23. }
  24. $scope.get_data = function() {
  25. var request = $scope.ejs.Request().indices($scope.index);
  26. // If we have an array, use query facet
  27. if(_.isArray($scope.panel.query)) {
  28. var queries = [];
  29. // Build the question part of the query
  30. _.each($scope.panel.query, function(v) {
  31. queries.push(ejs.FilteredQuery(
  32. ejs.QueryStringQuery(v.query || '*'),
  33. ejs.RangeFilter(config.timefield)
  34. .from($scope.from)
  35. .to($scope.to)
  36. .cache(false))
  37. )
  38. });
  39. // Then the insert into facet and make the request
  40. _.each(queries, function(v) {
  41. request = request.facet(ejs.QueryFacet(_.indexOf(queries,v))
  42. .query(v)
  43. .facetFilter(ejs.QueryFilter(v))
  44. )
  45. })
  46. var results = request.doSearch();
  47. // Populate scope when we have results
  48. results.then(function(results) {
  49. $scope.hits = results.hits.total;
  50. $scope.data = [];
  51. _.each(results.facets, function(v, k) {
  52. var series = {};
  53. var slice = { label : $scope.panel.query[k].label, data : v.count };
  54. if (!(_.isUndefined($scope.panel.query[k].color)))
  55. slice.color = $scope.panel.query[k].color;
  56. $scope.data.push(slice)
  57. });
  58. });
  59. // If we don't have an array, assume its a term facet.
  60. } else {
  61. var results = request
  62. .facet(ejs.TermsFacet('pie')
  63. .field($scope.panel.query.field)
  64. .size($scope.panel['size'])
  65. .exclude($scope.panel.exclude)
  66. .facetFilter(ejs.QueryFilter(
  67. ejs.FilteredQuery(
  68. ejs.QueryStringQuery($scope.panel.query.query || '*'),
  69. ejs.RangeFilter(config.timefield)
  70. .from($scope.from)
  71. .to($scope.to)
  72. .cache(false)
  73. )))).size(0)
  74. .doSearch();
  75. // Populate scope when we have results
  76. results.then(function(results) {
  77. $scope.hits = results.hits.total;
  78. $scope.data = [];
  79. var k = 0;
  80. _.each(results.facets.pie.terms, function(v) {
  81. var slice = { label : v.term, data : v.count };
  82. $scope.data.push();
  83. if(!(_.isUndefined($scope.panel.colors))
  84. && _.isArray($scope.panel.colors)
  85. && $scope.panel.colors.length > 0) {
  86. slice.color = $scope.panel.colors[k%$scope.panel.colors.length];
  87. }
  88. $scope.data.push(slice)
  89. k = k + 1;
  90. });
  91. });
  92. }
  93. }
  94. $scope.$watch(function() {
  95. return angular.toJson([$scope.from, $scope.to])
  96. }, function(){
  97. $scope.get_data();
  98. });
  99. })
  100. .directive('pie', function() {
  101. return {
  102. restrict: 'A',
  103. link: function(scope, elem, attrs) {
  104. // Re-rending the panel if it is resized,
  105. scope.$watch('data', function() {
  106. if(!(_.isUndefined(scope.data)))
  107. render_panel(scope,elem,attrs);
  108. });
  109. // Or if the model changes
  110. angular.element(window).bind('resize', function(){
  111. render_panel(scope,elem,attrs);
  112. });
  113. // Function for rendering panel
  114. function render_panel(scope,elem,attrs) {
  115. var pie = {
  116. series: {
  117. pie: {
  118. innerRadius: scope.panel.donut ? 0.4 : 0,
  119. tilt: scope.panel.tilt ? 0.45 : 1,
  120. radius: 1,
  121. show: true,
  122. combine: {
  123. color: '#999',
  124. label: 'The Rest'
  125. },
  126. label: {
  127. show: true,
  128. radius: 2/3,
  129. formatter: function(label, series){
  130. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  131. label+'<br/>'+Math.round(series.percent)+'%</div>';
  132. },
  133. threshold: 0.1
  134. }
  135. }
  136. },
  137. //grid: { hoverable: true, clickable: true },
  138. legend: { show: scope.panel.legend }
  139. };
  140. // Populate element
  141. $.plot(elem, scope.data, pie);
  142. //elem.show();
  143. }
  144. }
  145. };
  146. })