charts.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Generates JSON used for generating a chart
  4. *
  5. * @param $chart_id
  6. * The unique identifier for the chart
  7. *
  8. * @return
  9. * JSON array needed for the js caller
  10. *
  11. * @ingroup tripal_cv
  12. */
  13. function tripal_cv_chart($chart_id){
  14. // parse out the tripal module name from the chart_id to find out
  15. // which Tripal "hook" to call:
  16. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_chart_(.+)$/","$1",$chart_id);
  17. $callback = $tripal_mod . "_cv_chart";
  18. // now call the function in the module responsible for the chart. This
  19. // should call the tripal_cv_count_chart with the proper parameters set
  20. $opt = call_user_func_array($callback,array($chart_id));
  21. // build the JSON array to return to the javascript caller
  22. $json_arr = tripal_cv_count_chart($opt[count_mview],$opt[cvterm_id_column],
  23. $opt[count_column],$opt[filter],$opt[title], $opt[type],$opt[size]);
  24. $json_arr[] = $chart_id; // add the chart_id back into the json array
  25. return drupal_json($json_arr);
  26. }
  27. /**
  28. * Determines the counts needed for the chart to be rendered
  29. *
  30. * @param $cnt_table
  31. * The table containing counts for the various cvterms
  32. * @param $fk_column
  33. * The column in the count table to join it to the cvterm table
  34. * @param $cnt_column
  35. * The name of the column in the count table containing the counts
  36. * @param $filter
  37. * A Filter string. Default is (1=1).
  38. * @param $title
  39. * The title of the chart to be rendered.
  40. * @param $type
  41. * The type of chart to be rendered. Default is p3 (pie chart).
  42. * @param $size
  43. * The size of the chart to be rendered. Default is 300x75.
  44. *
  45. * @return
  46. * An options array needed to render the chart specified
  47. *
  48. * @ingroup tripal_cv_api
  49. */
  50. function tripal_cv_count_chart($cnt_table, $fk_column,
  51. $cnt_column, $filter = null, $title = '', $type = 'p3', $size='300x75') {
  52. if(!$type){
  53. $type = 'p3';
  54. }
  55. if(!$size){
  56. $size = '300x75';
  57. }
  58. if(!$filter){
  59. $filter = '(1=1)';
  60. }
  61. $isPie = 0;
  62. if(strcmp($type,'p')==0 or strcmp($type,'p3')==0){
  63. $isPie = 1;
  64. }
  65. $sql = "
  66. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  67. FROM {$cnt_table} CNT
  68. INNER JOIN {cvterm} CVT on CNT.$fk_column = CVT.cvterm_id
  69. WHERE $filter
  70. ";
  71. $features = array();
  72. $previous_db = tripal_db_set_active('chado'); // use chado database
  73. $results = db_query($sql);
  74. tripal_db_set_active($previous_db); // now use drupal database
  75. $data = array();
  76. $axis = array();
  77. $legend = array();
  78. $total = 0;
  79. $max = 0;
  80. $i = 1;
  81. while($term = db_fetch_object($results)){
  82. if($isPie){
  83. $axis[] = "$term->name (".number_format($term->num_items).")";
  84. $data[] = array($term->num_items,0,0);
  85. } else {
  86. $axis[] = "$term->name (".number_format($term->num_items).")";
  87. $data[] = array($term->num_items);
  88. // $legend[] = "$term->name (".number_format($term->num_items).")";
  89. }
  90. if($term->num_items > $max){
  91. $max = $term->num_items;
  92. }
  93. $total += $term->num_items;
  94. $i++;
  95. }
  96. // convert numerical values into percentages
  97. foreach($data as &$set){
  98. $set[0] = ($set[0] / $total) * 100;
  99. }
  100. $opt[] = array(
  101. data => $data,
  102. axis_labels => $axis,
  103. legend => $legend,
  104. size => $size,
  105. type => $type,
  106. bar_width => 10,
  107. bar_spacing => 0,
  108. title => $title
  109. );
  110. // $opt[] = $sql;
  111. return $opt;
  112. }