Skip to content
Snippets Groups Projects
google_charts.module 1.64 KiB
<?php
// $Id$
/**
 * @author Bruno Massa http://drupal.org/user/67164
 * @file
 * Use Google Charts on your site.
 *
 * @note only hooks are here.
 */

/**
 * Immplementation of hook_chartsapi().
 *
 * Its a Charts module hook. It defines almost all aspects
 * of a chart provider, like its name, what types of charts
 * it can perform and what are the restrictions.
 */
function google_charts_charts_info($op) {
  switch ($op) {
    case 'list':
      return array('google_charts' => 'Google Chart API');

    case 'charttypes':
      return array(
        'line2D'  => t('Line 2D'),
        'hbar2D'  => t('Horizontal Bar 2D'),
        'vbar2D'  => t('Vertical Bar 2D'),
        'pie2D'   => t('Pie 2D'),
        'pie3D'   => t('Pie 3D'),
        'venn'    => t('Venn'),
        'scatter' => t('Scatter Plot'),
      );
  }
}

/**
 * Immplementation of hook_chartsapi().
 *
 * Its a Charts module hook. It transform the data into a HTML chart.
 *
 * @param &$data
 *   Array. The
 */
function google_charts_chartsapi(&$data) {
  // Include the specific Google Chart API helper functions
  include_once drupal_get_path('module', 'google_charts') .'/google_charts.inc';

  $chart = array();
  if ($message = _google_charts_chart($chart, $data)) {
    return $message;
  }

  // Convert the chat DATA into the Google Chart way.
  // Since its a requirement to build the chart on Google, if the value
  // was not found, return nothing and stop the execution.
  if ($message = _google_charts_series($chart, $data)) {
    return $message;
  }

  // If its all ok, build the HTML img tag
  return '<img src="http://chart.apis.google.com/chart?'. implode('&amp;', $chart) .'" />';
}