module.js 4.5 KB

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