charts.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*************************************************************************
  3. *
  4. */
  5. function tripal_cv_chart($chart_id){
  6. // parse out the tripal module name from the chart_id to find out
  7. // which Tripal "hook" to call:
  8. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_chart_(.+)$/","$1",$chart_id);
  9. $callback = $tripal_mod . "_cv_chart";
  10. // now call the function in the module responsible for the chart. This
  11. // should call the tripal_cv_count_chart with the proper parameters set
  12. $opt = call_user_func_array($callback,array($chart_id));
  13. // build the JSON array to return to the javascript caller
  14. $json_arr = tripal_cv_count_chart($opt[count_mview],$opt[cvterm_id_column],
  15. $opt[count_column],$opt[filter],$opt[title], $opt[type],$opt[size]);
  16. $json_arr[] = $chart_id; // add the chart_id back into the json array
  17. return drupal_json($json_arr);
  18. }
  19. /*************************************************************************
  20. * name: tripal_cv_chart
  21. * description:
  22. */
  23. function tripal_cv_count_chart($cnt_table, $fk_column,
  24. $cnt_column, $filter = null, $title = '', $type = 'p3', $size='300x75') {
  25. if(!$type){
  26. $type = 'p3';
  27. }
  28. if(!$size){
  29. $size = '300x75';
  30. }
  31. if(!$filter){
  32. $filter = '(1=1)';
  33. }
  34. $isPie = 0;
  35. if(strcmp($type,'p')==0 or strcmp($type,'p3')==0){
  36. $isPie = 1;
  37. }
  38. $sql = "
  39. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  40. FROM {$cnt_table} CNT
  41. INNER JOIN {cvterm} CVT on CNT.$fk_column = CVT.cvterm_id
  42. WHERE $filter
  43. ";
  44. $features = array();
  45. $previous_db = tripal_db_set_active('chado'); // use chado database
  46. $results = db_query($sql);
  47. tripal_db_set_active($previous_db); // now use drupal database
  48. $data = array();
  49. $axis = array();
  50. $legend = array();
  51. $total = 0;
  52. $max = 0;
  53. $i = 1;
  54. while($term = db_fetch_object($results)){
  55. if($isPie){
  56. $axis[] = "$term->name (".number_format($term->num_items).")";
  57. $data[] = array($term->num_items,0,0);
  58. } else {
  59. $axis[] = "$term->name (".number_format($term->num_items).")";
  60. $data[] = array($term->num_items);
  61. // $legend[] = "$term->name (".number_format($term->num_items).")";
  62. }
  63. if($term->num_items > $max){
  64. $max = $term->num_items;
  65. }
  66. $total += $term->num_items;
  67. $i++;
  68. }
  69. // convert numerical values into percentages
  70. foreach($data as &$set){
  71. $set[0] = ($set[0] / $total) * 100;
  72. }
  73. $opt[] = array(
  74. data => $data,
  75. axis_labels => $axis,
  76. legend => $legend,
  77. size => $size,
  78. type => $type,
  79. bar_width => 10,
  80. bar_spacing => 0,
  81. title => $title
  82. );
  83. // $opt[] = $sql;
  84. return $opt;
  85. }