public function ChartsBlock::build in Charts 8.3
Same name and namespace in other branches
- 8.4 modules/charts_blocks/src/Plugin/Block/ChartsBlock.php \Drupal\charts_blocks\Plugin\Block\ChartsBlock::build()
- 5.0.x modules/charts_blocks/src/Plugin/Block/ChartsBlock.php \Drupal\charts_blocks\Plugin\Block\ChartsBlock::build()
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- modules/
charts_blocks/ src/ Plugin/ Block/ ChartsBlock.php, line 478
Class
- ChartsBlock
- Provides a 'ChartsBlock' block.
Namespace
Drupal\charts_blocks\Plugin\BlockCode
public function build() {
$categories = explode(",", $this->configuration['categories']);
$data = json_decode('[' . $this->configuration['data'] . ']', TRUE);
if (!empty($this->configuration['data_series'])) {
$seriesData = json_decode('[' . $this->configuration['data_series'] . ']', TRUE);
foreach ($seriesData as &$data) {
if (empty($data['type'])) {
$data['type'] = $this->configuration['type'];
}
}
}
else {
$seriesData = [
[
'name' => $this->configuration['series_label'],
'color' => $this->configuration['color'][0],
'type' => $this->configuration['type'],
'data' => $data,
],
];
}
/*
* Helps for pie and donut charts, which need more colors than configurable
* for a single series.
*/
$colors = $this->colors
->getDefaultColors();
$options = [
'library' => $this->configuration['library'],
'type' => $this->configuration['type'],
'grouping' => $this->configuration['grouping'],
'field_colors' => $this->configuration['field_colors'],
'colors' => $colors,
'title' => $this->configuration['title'],
'title_position' => $this->configuration['title_position'],
'data_labels' => $this->configuration['data_labels'],
'data_markers' => $this->configuration['data_markers'],
'legend' => $this->configuration['legend'],
'legend_position' => $this->configuration['legend_position'],
'background' => $this->configuration['background'],
'three_dimensional' => $this->configuration['three_dimensional'],
'polar' => $this->configuration['polar'],
'tooltips' => $this->configuration['tooltips'],
'tooltips_use_html' => $this->configuration['tooltips_use_html'],
'width' => $this->configuration['width'],
'height' => $this->configuration['height'],
'width_units' => $this->configuration['width_units'],
'height_units' => $this->configuration['height_units'],
'xaxis_title' => $this->configuration['xaxis_title'],
'xaxis_labels_rotation' => $this->configuration['xaxis_labels_rotation'],
'yaxis_title' => $this->configuration['yaxis_title'],
'yaxis_min' => $this->configuration['yaxis_min'],
'yaxis_max' => $this->configuration['yaxis_max'],
'yaxis_prefix' => $this->configuration['yaxis_prefix'],
'yaxis_suffix' => $this->configuration['yaxis_suffix'],
'yaxis_decimal_count' => $this->configuration['yaxis_decimal_count'],
'yaxis_labels_rotation' => $this->configuration['yaxis_labels_rotation'],
'inherit_yaxis' => $this->configuration['inherit_yaxis'],
'secondary_yaxis_title' => $this->configuration['secondary_yaxis_title'],
'secondary_yaxis_min' => $this->configuration['secondary_yaxis_min'],
'secondary_yaxis_max' => $this->configuration['secondary_yaxis_max'],
'secondary_yaxis_prefix' => $this->configuration['secondary_yaxis_prefix'],
'secondary_yaxis_suffix' => $this->configuration['secondary_yaxis_suffix'],
'secondary_yaxis_decimal_count' => $this->configuration['secondary_yaxis_decimal_count'],
'secondary_yaxis_labels_rotation' => $this->configuration['secondary_yaxis_labels_rotation'],
];
// Set gauge specific fields.
$gauge_fields = [
'green_from',
'green_to',
'red_from',
'red_to',
'yellow_from',
'yellow_to',
'max',
'min',
];
foreach ($gauge_fields as $field) {
$options[$field] = $this->configuration[$field];
}
// Adjustments added to provide the secondary y-axis features from Views.
$secondaryOptions = [];
if ($options['inherit_yaxis'] == 1) {
$secondaryOptions[0]['inherit_yaxis'] = 0;
$secondaryOptions[0]['style']['options']['yaxis_title'] = $options['secondary_yaxis_title'];
$secondaryOptions[0]['style']['options']['yaxis_min'] = $options['secondary_yaxis_min'];
$secondaryOptions[0]['style']['options']['yaxis_max'] = $options['secondary_yaxis_max'];
$secondaryOptions[0]['style']['options']['yaxis_prefix'] = $options['secondary_yaxis_prefix'];
$secondaryOptions[0]['style']['options']['yaxis_suffix'] = $options['secondary_yaxis_suffix'];
$secondaryOptions[0]['style']['options']['yaxis_decimal_count'] = $options['secondary_yaxis_decimal_count'];
$secondaryOptions[0]['style']['options']['yaxis_labels_rotation'] = $options['secondary_yaxis_labels_rotation'];
$secondaryOptions[0]['type'] = 'chart';
}
// Creates a UUID for the chart ID.
$uuid_service = \Drupal::service('uuid');
$chartId = 'chart-' . $uuid_service
->generate();
$build = [
'#theme' => 'charts_blocks',
'#library' => $this->configuration['library'],
'#categories' => $categories,
'#seriesData' => $seriesData,
'#secondaryOptions' => $secondaryOptions,
'#options' => $options,
'#id' => $chartId,
'#override' => [],
];
return $build;
}