module.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. labjs = labjs.script("common/lib/panels/jquery.flot.js")
  2. .script("common/lib/panels/jquery.flot.pie.js")
  3. angular.module('kibana.pieterms', [])
  4. .directive('pieterms', function() {
  5. return {
  6. restrict: 'A',
  7. link: function(scope, elem, attrs) {
  8. // Specify defaults for ALL directives
  9. var _d = {
  10. size : 5,
  11. query : "*",
  12. exclude : [],
  13. donut : false,
  14. tilt : false,
  15. legend : true,
  16. }
  17. // Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
  18. scope.$watch(function () {
  19. return (attrs.params && scope.index) ? true : false;
  20. }, function (ready) {
  21. scope.ready = ready;
  22. if(ready) {
  23. scope.params = JSON.parse(attrs.params);
  24. _.each(_d, function(v, k) {
  25. scope.params[k] = _.isUndefined(scope.params[k])
  26. ? _d[k] : scope.params[k];
  27. });
  28. }
  29. });
  30. // Also get the data if time frame changes.
  31. // (REQUIRED IN EVERY PANEL)
  32. scope.$watch(function() {
  33. return angular.toJson([scope.from, scope.to, scope.ready])
  34. }, function(){
  35. if(scope.ready)
  36. get_data(scope,elem,attrs);
  37. });
  38. // Re-rending the panel if it is resized,
  39. scope.$watch('data', function() {
  40. if(scope.ready)
  41. render_panel(scope,elem,attrs);
  42. });
  43. // Or if the model changes
  44. angular.element(window).bind('resize', function(){
  45. render_panel(scope,elem,attrs);
  46. });
  47. // Function for getting data
  48. function get_data(scope,elem,attrs) {
  49. var params = scope.params;
  50. var ejs = scope.ejs;
  51. var request = ejs.Request().indices(scope.index);
  52. // Build the question part of the query
  53. var query = ejs.FilteredQuery(
  54. ejs.QueryStringQuery(params.query || '*'),
  55. ejs.RangeFilter(config.timefield)
  56. .from(scope.from)
  57. .to(scope.to)
  58. .cache(false)
  59. );
  60. // Then the insert into facet and make the request
  61. var results = request
  62. .facet(ejs.TermsFacet('termpie')
  63. .field(params.field)
  64. .size(params['size'])
  65. .exclude(params.exclude)
  66. .facetFilter(ejs.QueryFilter(query))
  67. )
  68. .doSearch();
  69. // Populate scope when we have results
  70. results.then(function(results) {
  71. scope.hits = results.hits.total;
  72. scope.data = results.facets.termpie.terms;
  73. });
  74. }
  75. // Function for rendering panel
  76. function render_panel(scope,elem,attrs) {
  77. // Parse our params object
  78. var params = scope.params;
  79. // Create graph array
  80. scope.graph = [];
  81. _.each(scope.data, function(v, k) {
  82. if(!_.isUndefined(params.only) && _.indexOf(params.only,v['term']) < 0)
  83. return
  84. var point = {
  85. label : v['term'],
  86. data : v['count']
  87. }
  88. if(!_.isUndefined(params.colors))
  89. point.color = params.colors[_.indexOf(params.only,v['term'])]
  90. scope.graph.push(point)
  91. });
  92. var pie = {
  93. series: {
  94. pie: {
  95. innerRadius: params.donut ? 0.4 : 0,
  96. tilt: params.tilt ? 0.45 : 1,
  97. radius: 1,
  98. show: true,
  99. combine: {
  100. color: '#999',
  101. label: 'The Rest'
  102. },
  103. label: {
  104. show: true,
  105. radius: 2/3,
  106. formatter: function(label, series){
  107. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
  108. label+'<br/>'+Math.round(series.percent)+'%</div>';
  109. },
  110. threshold: 0.1
  111. }
  112. }
  113. },
  114. //grid: { hoverable: true, clickable: true },
  115. legend: { show: params.legend }
  116. };
  117. // Populate element
  118. $.plot(elem, scope.graph, pie);
  119. //elem.show();
  120. }
  121. }
  122. };
  123. })