public function ChartsPluginStyleChart::render in Charts 5.0.x
Same name and namespace in other branches
- 8.4 src/Plugin/views/style/ChartsPluginStyleChart.php \Drupal\charts\Plugin\views\style\ChartsPluginStyleChart::render()
- 8 src/Plugin/views/style/ChartsPluginStyleChart.php \Drupal\charts\Plugin\views\style\ChartsPluginStyleChart::render()
- 8.3 src/Plugin/views/style/ChartsPluginStyleChart.php \Drupal\charts\Plugin\views\style\ChartsPluginStyleChart::render()
Render the display in this style.
Overrides StylePluginBase::render
File
- src/
Plugin/ views/ style/ ChartsPluginStyleChart.php, line 204
Class
- ChartsPluginStyleChart
- Style plugin to render view as a chart.
Namespace
Drupal\charts\Plugin\views\styleCode
public function render() {
$field_handlers = $this->view
->getHandlers('field');
$chart_settings = $this->options['chart_settings'];
$chart_fields = $chart_settings['fields'];
// Calculate the labels field alias.
$label_field = isset($field_handlers[$chart_fields['label']]) ? $field_handlers[$chart_fields['label']] : '';
$label_field_key = $label_field ? $chart_fields['label'] : '';
// Assemble the fields to be used to provide data access.
$field_keys = array_keys($this
->getSelectedDataFields($chart_fields['data_providers']));
$data_fields = array_filter($field_handlers, function ($field_handler) use ($field_keys, $label_field_key) {
if (isset($field_handler['exclude']) && ($field_handler['exclude'] == FALSE || $field_handler['exclude'] == 0)) {
$field_id = $field_handler['id'];
}
else {
$field_id = '';
}
// Do not allow the label field to be used as a data field.
return $field_id !== $label_field_key && in_array($field_id, $field_keys);
});
// Allow argument tokens in the title.
$title = !empty($chart_settings['display']['title']) ? $chart_settings['display']['title'] : '';
if (!empty($this->view->build_info['substitutions'])) {
$tokens = $this->view->build_info['substitutions'];
$title = $this
->viewsTokenReplace($title, $tokens);
}
// To be used with the exposed chart type field.
if ($this->view->storage
->get('exposed_chart_type')) {
$chart_settings['type'] = $this->view->storage
->get('exposed_chart_type');
}
$chart_id = $this->view
->id() . '_' . $this->view->current_display;
$chart = [
'#type' => 'chart',
'#chart_type' => $chart_settings['type'],
'#chart_library' => $chart_settings['library'],
'#chart_id' => $chart_id,
'#id' => Html::getUniqueId('chart_' . $chart_id),
'#stacking' => $chart_settings['fields']['stacking'] ?? '0',
'#polar' => $chart_settings['display']['polar'],
'#three_dimensional' => $chart_settings['display']['three_dimensional'],
'#gauge' => $chart_settings['display']['gauge'],
'#title' => $title,
'#title_position' => $chart_settings['display']['title_position'],
'#tooltips' => $chart_settings['display']['tooltips'],
'#data_labels' => $chart_settings['display']['data_labels'],
// Colors only used if a grouped view or using a type such as a pie chart.
'#colors' => $chart_settings['display']['colors'] ?? [],
'#background' => $chart_settings['display']['background'] ? $chart_settings['display']['background'] : 'transparent',
'#legend' => !empty($chart_settings['display']['legend_position']),
'#legend_position' => $chart_settings['display']['legend_position'] ? $chart_settings['display']['legend_position'] : '',
'#width' => $chart_settings['display']['dimensions']['width'],
'#height' => $chart_settings['display']['dimensions']['height'],
'#width_units' => $chart_settings['display']['dimensions']['width_units'],
'#height_units' => $chart_settings['display']['dimensions']['height_units'],
'#attributes' => [
'data-drupal-selector-chart' => Html::getId($chart_id),
],
// Pass info about the actual view results to allow further processing.
'#view' => $this->view,
];
/** @var \Drupal\charts\TypeManager $chart_type_plugin_manager */
$chart_type_plugin_manager = \Drupal::service('plugin.manager.charts_type');
$chart_type = $chart_type_plugin_manager
->getDefinition($chart_settings['type']);
if ($chart_type['axis'] === ChartInterface::SINGLE_AXIS) {
$data_field_key = key($data_fields);
$data_field = $data_fields[$data_field_key];
$data = [];
$this
->renderFields($this->view->result);
$renders = $this->rendered_fields;
foreach ($renders as $row_number => $row) {
$data_row = [];
if ($label_field_key) {
// Labels need to be decoded, as the charting library will re-encode.
$data_row[] = strip_tags($this
->getField($row_number, $label_field_key), ENT_QUOTES);
}
$data_row[] = $this
->processNumberValueFromField($row_number, $data_field_key);
$data[] = $data_row;
}
// @todo: create a textfield for chart legend title.
// if ($chart_fields['label']) {
// $chart['#legend_title'] = $chart_fields['label'];
// }
$chart[$this->view->current_display . '_series'] = [
'#type' => 'chart_data',
'#data' => $data,
'#title' => $data_field['label'],
'#color' => isset($chart_fields['data_providers'][$data_field_key]) ? $chart_fields['data_providers'][$data_field_key]['color'] : '',
];
}
else {
$chart['xaxis'] = [
'#type' => 'chart_xaxis',
'#title' => $chart_settings['xaxis']['title'] ? $chart_settings['xaxis']['title'] : FALSE,
'#labels_rotation' => $chart_settings['xaxis']['labels_rotation'],
];
$chart['yaxis'] = [
'#type' => 'chart_yaxis',
'#title' => $chart_settings['yaxis']['title'] ? $chart_settings['yaxis']['title'] : '',
'#labels_rotation' => $chart_settings['yaxis']['labels_rotation'],
'#max' => $chart_settings['yaxis']['max'],
'#min' => $chart_settings['yaxis']['min'],
];
$sets = $this
->renderGrouping($this->view->result, $this->options['grouping'], TRUE);
$series_index = 0;
foreach ($sets as $series_label => $data_set) {
foreach ($data_fields as $field_key => $field_handler) {
$element_key = $this->view->current_display . '__' . $field_key . '_' . $series_index;
$chart[$element_key] = [
'#type' => 'chart_data',
'#data' => [],
// If using a grouping field, inherit from the chart level colors.
'#color' => $series_label === '' && isset($chart_fields['data_providers'][$field_key]) ? $chart_fields['data_providers'][$field_key]['color'] : '',
'#title' => $series_label ? strip_tags($series_label) : $field_handler['label'],
'#prefix' => $chart_settings['yaxis']['prefix'] ? $chart_settings['yaxis']['prefix'] : NULL,
'#suffix' => $chart_settings['yaxis']['suffix'] ? $chart_settings['yaxis']['suffix'] : NULL,
'#decimal_count' => $chart_settings['yaxis']['decimal_count'] ? $chart_settings['yaxis']['decimal_count'] : '',
];
}
// Grouped results come back indexed by their original result number
// from before the grouping, so we need to keep our own row number when
// looping through the rows.
$row_number = 0;
foreach ($data_set['rows'] as $result_number => $row) {
if ($label_field_key && !isset($chart['xaxis']['#labels'][$row_number])) {
$chart['xaxis']['#labels'][$row_number] = strip_tags($this
->getField($row_number, $label_field_key));
}
foreach ($data_fields as $field_key => $field_handler) {
// Don't allow the grouping field to provide data.
if (isset($this->options['grouping'][0]['field']) && $field_key === $this->options['grouping'][0]['field']) {
continue;
}
$element_key = $this->view->current_display . '__' . $field_key . '_' . $series_index;
$value = $this
->processNumberValueFromField($result_number, $field_key);
$chart[$element_key]['#data'][] = $value;
}
$row_number++;
}
// Incrementing series index.
$series_index++;
}
}
// Check if this display has any children charts that should be applied
// on top of it.
$children_displays = $this
->getChildrenChartDisplays();
// Contains the different subviews of the attachments.
$attachments = [];
foreach ($children_displays as $child_display) {
// If the user doesn't have access to the child display, skip.
if (!$this->view
->access($child_display)) {
continue;
}
// Generate the subchart by executing the child display. We load a fresh
// view here to avoid collisions in shifting the current display while in
// a display.
$subview = $this->view
->createDuplicate();
$subview
->setDisplay($child_display);
$child_display_handler = $this->view->displayHandlers
->get($child_display);
$child_display_settings = $subview->display_handler->options['style']['options']['chart_settings'];
// Copy the settings for our axes over to the child view.
foreach ($chart_settings as $option_name => $option_value) {
if ($child_display_handler->options['inherit_yaxis'] === '1') {
$child_display_settings[$option_name] = $option_value;
}
}
// Set the arguments on the subview if it is configured to inherit;
// arguments.
if (!empty($child_display_handler->display['display_options']['inherit_arguments']) && $child_display_handler->display['display_options']['inherit_arguments'] == '1') {
$subview
->setArguments($this->view->args);
}
// Execute the subview and get the result.
$subview
->preExecute();
$subview
->execute();
// If there's no results, don't attach the subview.
if (empty($subview->result)) {
continue;
}
$subchart = $subview->style_plugin
->render();
// Add attachment views to attachments array.
array_push($attachments, $subview);
// Create a secondary axis if needed.
if ($child_display_handler->options['inherit_yaxis'] !== '1' && isset($subchart['yaxis'])) {
$chart['secondary_yaxis'] = $subchart['yaxis'];
$chart['secondary_yaxis']['#opposite'] = TRUE;
}
// Merge in the child chart data.
foreach (Element::children($subchart) as $key) {
if ($subchart[$key]['#type'] === 'chart_data') {
$chart[$key] = $subchart[$key];
// If the subchart is a different type than the parent chart, set
// the #chart_type property on the individual chart data elements.
if ($subchart['#chart_type'] !== $chart['#chart_type']) {
$chart[$key]['#chart_type'] = $subchart['#chart_type'];
}
if ($child_display_handler->options['inherit_yaxis'] !== '1') {
$chart[$key]['#target_axis'] = 'secondary_yaxis';
}
}
}
}
$this->attachmentService
->setAttachmentViews($attachments);
// Print the chart.
return $chart;
}