? project_usage.charts.inc
Index: project_usage.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project/usage/project_usage.module,v
retrieving revision 1.6.2.11
diff -u -p -r1.6.2.11 project_usage.module
--- project_usage.module	4 Dec 2008 05:48:28 -0000	1.6.2.11
+++ project_usage.module	6 Dec 2008 22:13:32 -0000
@@ -598,7 +598,79 @@ function theme_project_usage_project_pag
 
   $output .= '<h3>'. t('Recent release usage') .'</h3>';
   $output .= theme('table', $release_header, $release_rows);
+
+// OUTPUT THE TABLES FOR DEBUGGING
+$output .= var_export($project_header, 1);
+$output .= var_export($project_rows, 1);
+
+  // Reverse the order of the rows so it's oldest to newest.
+  $rows = array_reverse($project_rows);
+  $col_count = count($project_header);
+  $row_count = count($rows);
+
+  // Pull the API versions from the table header for use as a legend. Since the
+  // table is keyed strangely, make note of which tid is in which order so we
+  // can efficiently iterate over the columns.
+  $legend = array();
+  $mapping = array();
+  foreach (array_slice($project_header, 1) as $tid => $cell) {
+    $legend[] = $cell['data'];
+    $mapping[] = $tid;
+  }
+$output .= var_export($legend, 1);
+
+  // Rotate the table so each series is in a row in the array and grab the
+  // dates for use as axis labels.
+  $series = array();
+  $date_axis = array();
+  foreach (array_values($rows) as $i => $row) {
+    $date_axis[$i] = $row[0]['data'];
+    foreach ($mapping as $j => $tid) {
+      // FIXME: HACK TO REMOVE COMMAS FROM number_format(). It migth be better
+      // to pass in the clean number and format them here.
+      $series[$i][$j] = str_replace($row[$tid]['data'], ',', '');
+    }
+  }
+$output .= var_export($date_axis, 1);
+$output .= var_export($series, 1);
+
+  // Now convert the series into strings with the data. Along the way figure
+  // out the range of data.
+  $min = $max = 0;
+  $data = array();
+  foreach ($series as $s) {
+    $data[] = implode(',', $s);
+    $max = max($max, max($s));
+  }
+
+  // Round the max up to the next decimal place (3->10, 19->100, 8703->1000) so
+  // that the labels have round numbers and the entire range is visible.
+  $places = strlen((int) $max);
+  $max = round($max + (pow(10, $places) / 2), - ($places));
+
+  // Might need more colors than this.
+  $colors = array('EDAA00', '0062A0', 'A17300', 'ED8200', '38B4BA', '215D6E');
+
+  $args = array(
+    'chtt' => t('Usage by API'),
+    'chs' => '600x200',
+    'cht' => 'ls',
+    // Pick some colors.
+    'chco' => implode(',', array_slice($colors, 0, $col_count)),
+    'chd' =>  't:'. implode('|', $data),
+    // Set the range of the chart
+    'chds' => implode(',', array_fill(0, $col_count - 1, $min .','. $max)),
+     // Legend is the header titles after excluding the date.
+    'chdl' => implode('|', $legend),
+  );
+
+  project_usage_chart_axis_labels($args, array(
+    'x' => project_usage_chart_label_subset($date_axis, 5),
+    'y' => array_map('number_format', project_usage_chart_label_subset(range($min, $max, 1000), 5)),
+  ));
+
   $output .= '<h3>'. t('Weekly project usage') .'</h3>';
+  $output .= theme('project_usage_chart', $args);
   $output .= theme('table', $project_header, $project_rows);
   return $output;
 }
@@ -660,3 +732,59 @@ function project_usage_project_page_link
     $links['resources']['links']['project_usage'] = l(t('View usage statistics'), 'project/usage/' . $node->uri);
   }
 }
+
+/**
+ * Extract a subset of labels at regular intervals from a set.
+ *
+ * @param $labels
+ *   Array of values to choose from.
+ * @param $n
+ *   Number of segments in the set. This number should divide $labels wit no
+ *   remander.
+ */
+function project_usage_chart_label_subset($labels, $n) {
+  $subset = array();
+  $count = count($labels) - 1;
+  $n--;
+  for ($i = 0; $i <= $n; $i++) {
+    $subset[] = $labels[$count * ($i / $n)];
+  }
+  return $subset;
+}
+
+/**
+ * Add axis labels to the chart arguments.
+ *
+ * @param $args
+ *   The array where the chart is being built.
+ * @param $labels
+ *   Array keyed by axis (x, t, y, r) the value is an array of labels for that
+ *   axis.
+ * @see http://code.google.com/apis/chart/labels.html#multiple_axes_labels
+ */
+function project_usage_chart_axis_labels(&$args, $labels) {
+  $keys = array_keys($labels);
+  $args['chxt'] = implode(',', $keys);
+
+  $l = array();
+  foreach ($keys as $i => $key) {
+    $l[$i] = $i .':|' . implode('|', $labels[$key]);
+  }
+  $args['chxl'] = implode('|', $l);
+}
+
+/**
+ * Convert the array of Google Chart paramters into an image URL.
+ *
+ * @param $args
+ *   Array of key, value pairs to turn into a Google Charts image.
+ * @return
+ *   HTML image element.
+ */
+function theme_project_usage_chart($args) {
+  $params = array();
+  foreach ($args as $key => $value) {
+    $params[] = $key .'='. $value;
+  }
+  return theme('image', 'http://chart.apis.google.com/chart?'. implode('&', $params), '', '', NULL, FALSE);
+}
