Browse Source

Streamlined the code for the feature admin summart chart and added extensive commenting to make it easier to create similar charts for other admin pages in the future.

Lacey Sanderson 10 years ago
parent
commit
5b4b33c34f
1 changed files with 52 additions and 40 deletions
  1. 52 40
      tripal_feature/includes/tripal_feature.admin.inc

+ 52 - 40
tripal_feature/includes/tripal_feature.admin.inc

@@ -37,61 +37,70 @@ function tripal_feature_admin_feature_view() {
   }
 
   // Add a summary chart.
+  //-----------------------------------
+  // We are using the organism_feature_count materialized view as the source for our data.
+  // Thus grab all the records from this materialized view.
   $organism_feature_count = chado_select_record(
     'organism_feature_count',
     array('*'),
     array(),
     array('order_by' => array('genus' => 'ASC', 'species' => 'ASC', 'feature_type' => 'ASC', 'num_features' => 'DESC'))
   );
-  $summary = array();
-  $organisms = array();
-  $types = array();
+
+  // Initialize variables.
   $chart = array();
+  $type_names = array();
+  $organism_names = array();
+
+  // Process each row of the materialzied view into the chart array.
+  // Note: it's first keyed by type since each type will be a bar. Each type will have
+  // a "bars" array with the start (y0) and end (y1) height on the bar for a given
+  // organism. Finally we keep a record of the names of the types & organisms
+  // for axis' and legend generation respectively.
   foreach ($organism_feature_count as $row) {
 
-    $summary[$row->organism_id][$row->cvterm_id] = $row->num_features;
-
-    $organisms[$row->organism_id]['organism_id'] = $row->organism_id;
-    $organisms[$row->organism_id]['genus'] = $row->genus;
-    $organisms[$row->organism_id]['species'] = $row->species;
-    $organisms[$row->organism_id]['common_name'] = $row->common_name;
-    $organisms[$row->organism_id]['scientific_name'] = $row->genus . ' ' . $row->species;
-    $organisms[$row->organism_id]['total_features'] = (isset($organisms[$row->organism_id]['total_features'])) ? $organisms[$row->organism_id]['total_features'] + $row->num_features : $row->num_features;
-
-    $types[$row->cvterm_id]['cvterm_id'] = $row->cvterm_id;
-    $types[$row->cvterm_id]['name'] = $row->feature_type;
-    $types[$row->cvterm_id]['total_features'] = (isset($types[$row->cvterm_id]['total_features'])) ? $types[$row->cvterm_id]['total_features'] + $row->num_features : $row->num_features;
-
-
+    // Build up the easy details for the current row's type. These will be overridden
+    // multiple times but that's more efficient than checking each time.
     $chart[$row->cvterm_id]['cvterm_id'] = $row->cvterm_id;
     $chart[$row->cvterm_id]['name'] = $row->feature_type;
-    $chart[$row->cvterm_id]['max_features'] = (isset($types[$row->cvterm_id]['max_features'])) ? max($types[$row->cvterm_id]['max_features'], $row->num_features) : $row->num_features;
+
+    // Save the name of the type and organism into their respective arrays
+    // for generation of axis' and legends for the chart.
+    $type_names[$row->cvterm_id] = $row->feature_type;
+    $organism_names[$row->organism_id] = $row->genus . ' ' . $row->species;
+
+    // Save information about the current organism. This isn't actually used by the
+    // chart but can be used to debug the bar generation to follow.
     $chart[$row->cvterm_id]['organisms'][] = array(
       'name' => $row->genus . ' ' . $row->species,
       'value' => (int) $row->num_features
     );
-    $type_names[$row->cvterm_id] = $row->feature_type;
-    $organism_names[$row->organism_id] = $row->genus . ' ' . $row->species;
-  }
 
-  // Quick processing of the organisms array per type
-  // to build-up the stacked bars.
-  foreach ($chart as $type_id => $bar) {
-    $y0 = 0;
-    $y1 = 0;
-    $chart[$type_id]['bars'] = array();
-    foreach ($bar['organisms'] as $k => $org) {
-      $y0 = $y1;
-      $y1 = $y0 + $org['value'];
-      $chart[$type_id]['bars'][$k]['name'] = $org['name'];
-      $chart[$type_id]['bars'][$k]['y0'] = $y0;
-      $chart[$type_id]['bars'][$k]['y1'] = $y1;
+    // Now to build the bar array with the start (y0) and end (y1) height on the
+    // bar for a given organism.
+    // NOTE: we cannot assume the types are all in order so store y0 & y1 in the
+    // $chart[type] array.
+    // If y0 has not yet been set for this type then we're starting with the first
+    // chunk (organism) on the bar.
+    if (!isset($chart[$row->cvterm_id]['y0'])) {
+      $chart[$row->cvterm_id]['y0'] = 0;
+      $chart[$row->cvterm_id]['y1'] = $row->num_features;
     }
-    $chart[$type_id]['total_features'] = $y1;
-  }
+    // Otherwise, add the next chunk (organism) on top of the pre-existing bar.
+    else {
+      $chart[$row->cvterm_id]['y0'] = $chart[$row->cvterm_id]['y1'];
+      $chart[$row->cvterm_id]['y1'] = $chart[$row->cvterm_id]['y0'] + $row->num_features;
+    }
+    // Now save the bar chunk we just determined.
+    $chart[$row->cvterm_id]['bars'][] = array(
+      'name' => $row->genus . ' ' . $row->species,
+      'y0' => $chart[$row->cvterm_id]['y0'],
+      'y1' => $chart[$row->cvterm_id]['y1'],
+    );
 
-  $variables['organisms'] = $organisms;
-  $variables['types'] = $types;
+    // We also need to keep track of the total number of features for a single bar (Type).
+    $chart[$row->cvterm_id]['total_features'] = $chart[$row->cvterm_id]['y1'];
+  }
 
   // Sort based on the total number of features.
   // NOTE: This changes the keys so it's no longer the organism/type_id.
@@ -108,9 +117,8 @@ function tripal_feature_admin_feature_view() {
     array(':mv_table' => 'organism_feature_count')
   )->fetchObject();
 
-  tripal_add_d3js();
-  drupal_add_css(drupal_get_path('module','tripal_feature') . '/theme/css/tripal_feature.css');
-  drupal_add_js(drupal_get_path('module','tripal_feature') . '/theme/js/tripalFeature.adminChart.js');
+  // Save everything we just determined as a Drupal JS settings so that we have access to
+  // it in our js script.
   drupal_add_js(array('tripalFeature' => array('admin' => array(
     'summary' => $chart,
     'types' => $type_names,
@@ -120,6 +128,10 @@ function tripal_feature_admin_feature_view() {
     'mviewLastUpdate' => format_date($mview->last_update),
   ))), 'setting');
 
+  // Finally add all the javascript and css needed to render the chart.
+  tripal_add_d3js();
+  drupal_add_css(drupal_get_path('module','tripal_feature') . '/theme/css/tripal_feature.css');
+  drupal_add_js(drupal_get_path('module','tripal_feature') . '/theme/js/tripalFeature.adminChart.js');
 
   return $output;
 }