From b3032da2803b711ff0540dff82f0cc26a7b196fb Mon Sep 17 00:00:00 2001 From: Daniel Cothran <daniel@andile.co> Date: Wed, 11 Apr 2018 11:03:43 -0400 Subject: [PATCH] Add code documentation and rename variables in template_preprocess_views_view_charts. This also removed the unused ChartsRenderInterface class. --- charts.module | 117 ++++++++++++++++---------- src/Charts/ChartsRenderInterface.php | 28 ------ templates/views-view-charts.html.twig | 2 +- 3 files changed, 74 insertions(+), 73 deletions(-) delete mode 100644 src/Charts/ChartsRenderInterface.php diff --git a/charts.module b/charts.module index 41b8e24..77cdf63 100644 --- a/charts.module +++ b/charts.module @@ -16,7 +16,7 @@ function charts_theme($existing, $type, $theme, $path) { 'views_view_charts' => [ 'variables' => [ 'view' => NULL, - 'row' => NULL, + 'row' => NULL, ], ], ]; @@ -27,75 +27,104 @@ function charts_theme($existing, $type, $theme, $path) { */ function template_preprocess_views_view_charts(&$variables) { - $options = $variables['view']->style_plugin->options; - $attachmentDisplayOptions = []; + // Define the View. + $view = $variables['view']; - $service = \Drupal::service('charts.charts_attachment'); - $attachmentView = $service->getAttachmentViews(); + // Information on Chart Attachment displays that might be used. + $attachmentService = \Drupal::service('charts.charts_attachment'); - $view = $variables['view']; - $viewId = $view->id(); - $displayId = $view->display_handler->display['id']; - $chartId = $viewId . '__' . $displayId; - $categoriesAttachment = []; - $seriesDataAttachment = []; - $attachmentChartTypeOption = []; + // An array of the Chart Attachment displays. + $chartAttachments = $attachmentService->getAttachmentViews(); + $attachmentCount = count($chartAttachments); + + /** + * To build a chart from this View, the following are needed (in this order): + * $options, $categories, $seriesData, $attachmentDisplayOptions, $variables, + * and $chartId. The $variables are pulled directly from the parameter. + */ + + // Bring in the options from the View's style plugin. + $options = $view->style_plugin->options; - for ($i = 0; $i < count($attachmentView); $i++) { - $attachmentId = $attachmentView[$i]->display_handler->display['id']; + // Get the data from the Chart Attachment displays. + $attachmentCategories = []; + $attachmentSeriesData = []; + for ($i = 0; $i < $attachmentCount; $i++) { + // Define the Chart Attachment. + $chartAttachment = $chartAttachments[$i]; + $attachmentId = $chartAttachment->display_handler->display['id']; $attachmentDisplay = $view->storage->getDisplay($attachmentId); - $attachmentChartType = $attachmentDisplay['display_options']['style']['options']['type']; - array_push($attachmentChartTypeOption, $attachmentChartType); + + // Bring in the Value Field from the Chart Attachment. $attachedValueField = $attachmentDisplay['display_options']['style']['options']['data_fields']; + $attachedValueField = Util::removeUnselectedFields($attachedValueField); + + // Bring in the Label Field from the Chart Attachment. + $attachmentLabelField = $chartAttachment->style_plugin->options['label_field']; - $combinedAttachmentPage = Util::removeUnselectedFields($attachedValueField); - $attachmentColor = $attachmentView[$i]->style_plugin->options['field_colors']; - $labelField = $attachmentView[$i]->style_plugin->options['label_field']; + // Bring in the Colors from the Chart Attachment. + $attachmentColor = $chartAttachment->style_plugin->options['field_colors']; - $dataAttachment = Util::viewsData($attachmentView[$i], $combinedAttachmentPage, $labelField, $attachmentColor, $attachmentChartTypeOption[$i]); - $dataAttachmentFormatted = Util::createChartableData($dataAttachment); - for ($j = 0; $j < count($dataAttachmentFormatted[0]); $j++) { - array_push($categoriesAttachment, $dataAttachmentFormatted[0][$j]); + // Bring in the Chart Type from the Chart Attachment. + $attachmentChartType = $attachmentDisplay['display_options']['style']['options']['type']; + + // Create an array of categories and seriesData from the Chart Attachment. + $attachmentData = Util::viewsData($chartAttachment, $attachedValueField, $attachmentLabelField, $attachmentColor, $attachmentChartType); + $attachmentData = Util::createChartableData($attachmentData); + + // Combine the $categories from each Chart Attachment into one array. + for ($j = 0; $j < count($attachmentData[0]); $j++) { + array_push($attachmentCategories, $attachmentData[0][$j]); } - for ($j = 0; $j < count($dataAttachmentFormatted[1]); $j++) { - array_push($seriesDataAttachment, $dataAttachmentFormatted[1][$j]); + // Combine the $seriesData from each Chart Attachment into one array. + for ($j = 0; $j < count($attachmentData[1]); $j++) { + array_push($attachmentSeriesData, $attachmentData[1][$j]); } + } - $library = $view->style_plugin->options['library']; - $variables['data'] = []; - $labelField = $view->style_plugin->options['label_field']; + + // Bring in the Value Field from the View. $valueField = $view->style_plugin->options['data_fields']; $valueField = Util::removeUnselectedFields($valueField); - $color = $view->style_plugin->options['field_colors']; - $attachmentCount = count($attachmentView); - $data = Util::viewsData($view, $valueField, $labelField, $color, $attachmentChartTypeOption = NULL); - for ($i = 0; $i < $attachmentCount; $i++) { - $data = Util::viewsData($view, $valueField, $labelField, $color, $attachmentChartTypeOption[$attachmentCount]); - } + // Bring in the Label Field from the View. + $labelField = $view->style_plugin->options['label_field']; + + // Bring in the colors from the View. + $color = $view->style_plugin->options['field_colors']; + // Create an array of categories and seriesData from the View. + // $data = Util::viewsData($view, $valueField, $labelField, $color, $attachmentChartTypes = NULL); + $data = Util::viewsData($view, $valueField, $labelField, $color, $options['type']); $data = Util::createChartableData($data); $categories = $data[0]; $seriesData = $data[1]; - $categories = array_merge($categories, $categoriesAttachment); + + // Produce the final array of categories. + $categories = array_merge($categories, $attachmentCategories); $categories = array_unique($categories); - for ($i = 0; $i < count($attachmentView); $i++) { - $attachmentId = $attachmentView[$i]->display_handler->display['id']; + // Produce the final seriesData object. + $seriesData = array_merge($seriesData, $attachmentSeriesData); + + // Bring in the Chart Attachment display options. + $attachmentDisplayOptions = []; + for ($i = 0; $i < $attachmentCount; $i++) { + $attachmentId = $chartAttachments[$i]->display_handler->display['id']; $attachmentDisplay = $view->storage->getDisplay($attachmentId); - $attachmentDisplayOptions[$i] = $attachmentDisplay['display_options']; - // Gets rid of inherit_yaxis issue, but doesn't fix underlying issue. - $attachmentDisplayOptions[$i]['inherit_yaxis'] = $view->displayHandlers->get($attachmentId)->options['inherit_yaxis']; + array_push($attachmentDisplayOptions, $attachmentDisplay['display_options']); } - $seriesData = array_merge($seriesData, $seriesDataAttachment); + + // Generate a unique chart ID by combining the View and display IDs. + $viewId = $view->id(); + $displayId = $view->display_handler->display['id']; + $chartId = $viewId . '__' . $displayId; $plugin_manager = \Drupal::service('plugin.manager.charts'); - $plugin = $plugin_manager->createInstance($library); - $variables['height'] = $options['height']; - $variables['width'] = $options['width']; + $plugin = $plugin_manager->createInstance($options['library']); $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId); } diff --git a/src/Charts/ChartsRenderInterface.php b/src/Charts/ChartsRenderInterface.php deleted file mode 100644 index 4c823e5..0000000 --- a/src/Charts/ChartsRenderInterface.php +++ /dev/null @@ -1,28 +0,0 @@ -<?php - -namespace Drupal\charts\Charts; - -/** - * Defines an interface for charts render classes. - */ -interface ChartsRenderInterface { - - /** - * Charts render charts. - * - * @param array $options - * Options. - * @param array $categories - * Categories. - * @param array $seriesData - * Series Data. - * @param array $attachmentDisplayOptions - * Attachment Display Options. - * @param array $variables - * Variables. - * @param string $chartId - * Chart Id. - */ - public function chartsRenderCharts(array $options = [], array $categories = [], array $seriesData = [], array $attachmentDisplayOptions = [], array &$variables = [], $chartId = ''); - -} diff --git a/templates/views-view-charts.html.twig b/templates/views-view-charts.html.twig index 24734d4..eb9b217 100644 --- a/templates/views-view-charts.html.twig +++ b/templates/views-view-charts.html.twig @@ -1,4 +1,4 @@ -{% set library, height, width = 'charts_' ~ chart_type ~ '/' ~ chart_type, height, width %} +{% set library, height, width = 'charts_' ~ chart_type ~ '/' ~ chart_type, options.height, options.width %} {{ attach_library("#{ library }") }} <div {{ attributes }} {{ content_attributes }} style="{% if width is not empty %}width:{{ width }}px;{% endif %}{% if height is not empty %}height:{{ height }}px;{% endif %}"></div> -- GitLab