class GoogleVisualizationAPIHandler in Visualization API 8
Library plugin for Visualization implementing support for Google Visualization API.
Plugin annotation
@Plugin(
id = "gva",
name = "gva",
label = @Translation("Google Visualization API")
)
Hierarchy
- class \Drupal\visualization\Plugin\visualization\handler\GoogleVisualizationAPIHandler implements VisualizationHandlerInterface
Expanded class hierarchy of GoogleVisualizationAPIHandler
File
- src/
Plugin/ visualization/ handler/ GoogleVisualizationAPIHandler.php, line 23 - Drupal\visualization\Plugin\visualization\handler\GoogleVisualizationAPIHandler.
Namespace
Drupal\visualization\Plugin\visualization\handlerView source
class GoogleVisualizationAPIHandler implements VisualizationHandlerInterface {
public $name = 'gva';
protected $addedJavascript = FALSE;
/**
* Renders a chart using the Google Visualization API.
*/
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;
}
/**
* Loads the global Javascript required by the Google Visualization API.
*/
public function postRender() {
if (!$this->addedJavascript) {
$js_libs['#attached']['library'][] = 'visualization/gva';
drupal_render($js_libs);
$this->addedJavascript = TRUE;
}
}
/**
* Returns whether or not the plugin is available.
*/
public function available() {
return TRUE;
}
/**
* Returns an array of supported chart types.
*/
public function supportedTypes() {
return array(
'line',
'column',
'pie',
'map',
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GoogleVisualizationAPIHandler:: |
protected | property | ||
GoogleVisualizationAPIHandler:: |
public | property | ||
GoogleVisualizationAPIHandler:: |
public | function |
Returns whether or not the plugin is available. Overrides VisualizationHandlerInterface:: |
|
GoogleVisualizationAPIHandler:: |
public | function |
Loads the global Javascript required by the Google Visualization API. Overrides VisualizationHandlerInterface:: |
|
GoogleVisualizationAPIHandler:: |
public | function |
Renders a chart using the Google Visualization API. Overrides VisualizationHandlerInterface:: |
|
GoogleVisualizationAPIHandler:: |
public | function |
Returns an array of supported chart types. Overrides VisualizationHandlerInterface:: |