You are here

public function GoogleVisualizationAPIHandler::render in Visualization API 7

Renders a chart using the Google Visualization API.

Overrides VisualizationHandlerInterface::render

File

includes/plugins/google_visualization_api.inc, line 15
Library plugin for Visualization implementing support for Google Visualization API.

Class

GoogleVisualizationAPIHandler
@file Library plugin for Visualization implementing support for Google Visualization API.

Code

public function render($chart_id, $data, $options) {

  // Chart options.
  $chart_options = array(
    'title' => $options['title'],
    'width' => !empty($options['width']) ? $options['width'] : '100%',
    'height' => !empty($options['height']) ? $options['height'] : '100%',
    'vAxis' => array(
      'title' => !empty($options['yAxis']['title']) ? $options['yAxis']['title'] : '',
    ),
  );
  switch ($options['type']) {
    case 'map':
      $chart_options['dataMode'] = !empty($options['dataMode']) ? $options['dataMode'] : 'regions';
      break;
  }

  // Prepare the table array with the data.
  $table_data = array();

  // Add header row first.
  $header = array();
  if (!empty($options['xAxis']['labelField'])) {
    $header[] = $options['fields'][$options['xAxis']['labelField']]['label'];
  }
  foreach ($options['fields'] as $name => $column) {
    if (!empty($column['enabled'])) {
      $header[] = $column['label'];
    }
  }
  $table_data[] = $header;

  // Then add data, row per row.
  foreach ($data as $row) {
    $table_row = array();
    if (!empty($options['xAxis']['labelField'])) {
      $table_row[] = html_entity_decode((string) $row[$options['xAxis']['labelField']], ENT_QUOTES);
    }
    foreach ($options['fields'] as $name => $column) {
      if (!empty($column['enabled'])) {
        $value = is_null($row[$name]) ? NULL : (double) $row[$name];
        $table_row[] = $value;
      }
    }
    $table_data[] = $table_row;
  }
  $information = array(
    'library' => 'google_visualization',
    'type' => $options['type'],
    'options' => $chart_options,
    'dataArray' => $table_data,
    'chart_id' => $chart_id,
  );

  // Add Drupal.settings for this chart.
  drupal_add_js(array(
    'visualization' => array(
      $chart_id => $information,
    ),
  ), array(
    'type' => 'setting',
  ));
}