GoogleVisualizationAPIHandler.php in Visualization API 8
File
src/Plugin/visualization/handler/GoogleVisualizationAPIHandler.php
View source
<?php
namespace Drupal\visualization\Plugin\visualization\handler;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\visualization\VisualizationHandlerInterface;
class GoogleVisualizationAPIHandler implements VisualizationHandlerInterface {
public $name = 'gva';
protected $addedJavascript = FALSE;
public function render($chart_id, $data, $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;
}
$table_data = array();
$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;
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,
);
$chart['#attached'] = [
'drupalSettings' => [
'visualization' => [
$chart_id => $information,
],
],
];
return $chart;
}
public function postRender() {
if (!$this->addedJavascript) {
$js_libs['#attached']['library'][] = 'visualization/gva';
drupal_render($js_libs);
$this->addedJavascript = TRUE;
}
}
public function available() {
return TRUE;
}
public function supportedTypes() {
return array(
'line',
'column',
'pie',
'map',
);
}
}