You are here

public function GoogleVisualizationAPIHandler::render in Visualization API 8

Renders a chart using the Google Visualization API.

Overrides VisualizationHandlerInterface::render

File

src/Plugin/visualization/handler/GoogleVisualizationAPIHandler.php, line 32
Drupal\visualization\Plugin\visualization\handler\GoogleVisualizationAPIHandler.

Class

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

Namespace

Drupal\visualization\Plugin\visualization\handler

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%',
  );
  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[] = SafeMarkup::checkPlain(strip_tags((string) $row[$options['xAxis']['labelField']]));
    }
    foreach ($options['fields'] as $name => $column) {
      if (!empty($column['enabled'])) {
        $value = is_null($row[$name]) ? NULL : (double) $row[$name]
          ->__toString();
        $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.
  $chart['#attached'] = [
    'drupalSettings' => [
      'visualization' => [
        $chart_id => $information,
      ],
    ],
  ];
  return $chart;
}