charts.php 2.8 KB

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