Skip to content
Snippets Groups Projects
Commit b3032da2 authored by Daniel Cothran's avatar Daniel Cothran
Browse files

Add code documentation and rename variables in...

Add code documentation and rename variables in template_preprocess_views_view_charts. This also removed the unused ChartsRenderInterface class.
parent abfa2875
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ function charts_theme($existing, $type, $theme, $path) { ...@@ -16,7 +16,7 @@ function charts_theme($existing, $type, $theme, $path) {
'views_view_charts' => [ 'views_view_charts' => [
'variables' => [ 'variables' => [
'view' => NULL, 'view' => NULL,
'row' => NULL, 'row' => NULL,
], ],
], ],
]; ];
...@@ -27,75 +27,104 @@ function charts_theme($existing, $type, $theme, $path) { ...@@ -27,75 +27,104 @@ function charts_theme($existing, $type, $theme, $path) {
*/ */
function template_preprocess_views_view_charts(&$variables) { function template_preprocess_views_view_charts(&$variables) {
$options = $variables['view']->style_plugin->options; // Define the View.
$attachmentDisplayOptions = []; $view = $variables['view'];
$service = \Drupal::service('charts.charts_attachment'); // Information on Chart Attachment displays that might be used.
$attachmentView = $service->getAttachmentViews(); $attachmentService = \Drupal::service('charts.charts_attachment');
$view = $variables['view']; // An array of the Chart Attachment displays.
$viewId = $view->id(); $chartAttachments = $attachmentService->getAttachmentViews();
$displayId = $view->display_handler->display['id']; $attachmentCount = count($chartAttachments);
$chartId = $viewId . '__' . $displayId;
$categoriesAttachment = []; /**
$seriesDataAttachment = []; * To build a chart from this View, the following are needed (in this order):
$attachmentChartTypeOption = []; * $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); $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 = $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); // Bring in the Colors from the Chart Attachment.
$attachmentColor = $attachmentView[$i]->style_plugin->options['field_colors']; $attachmentColor = $chartAttachment->style_plugin->options['field_colors'];
$labelField = $attachmentView[$i]->style_plugin->options['label_field'];
$dataAttachment = Util::viewsData($attachmentView[$i], $combinedAttachmentPage, $labelField, $attachmentColor, $attachmentChartTypeOption[$i]); // Bring in the Chart Type from the Chart Attachment.
$dataAttachmentFormatted = Util::createChartableData($dataAttachment); $attachmentChartType = $attachmentDisplay['display_options']['style']['options']['type'];
for ($j = 0; $j < count($dataAttachmentFormatted[0]); $j++) {
array_push($categoriesAttachment, $dataAttachmentFormatted[0][$j]); // 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++) { // Combine the $seriesData from each Chart Attachment into one array.
array_push($seriesDataAttachment, $dataAttachmentFormatted[1][$j]); for ($j = 0; $j < count($attachmentData[1]); $j++) {
array_push($attachmentSeriesData, $attachmentData[1][$j]);
} }
} }
$library = $view->style_plugin->options['library'];
$variables['data'] = []; // Bring in the Value Field from the View.
$labelField = $view->style_plugin->options['label_field'];
$valueField = $view->style_plugin->options['data_fields']; $valueField = $view->style_plugin->options['data_fields'];
$valueField = Util::removeUnselectedFields($valueField); $valueField = Util::removeUnselectedFields($valueField);
$color = $view->style_plugin->options['field_colors'];
$attachmentCount = count($attachmentView); // Bring in the Label Field from the View.
$data = Util::viewsData($view, $valueField, $labelField, $color, $attachmentChartTypeOption = NULL); $labelField = $view->style_plugin->options['label_field'];
for ($i = 0; $i < $attachmentCount; $i++) {
$data = Util::viewsData($view, $valueField, $labelField, $color, $attachmentChartTypeOption[$attachmentCount]); // 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); $data = Util::createChartableData($data);
$categories = $data[0]; $categories = $data[0];
$seriesData = $data[1]; $seriesData = $data[1];
$categories = array_merge($categories, $categoriesAttachment);
// Produce the final array of categories.
$categories = array_merge($categories, $attachmentCategories);
$categories = array_unique($categories); $categories = array_unique($categories);
for ($i = 0; $i < count($attachmentView); $i++) { // Produce the final seriesData object.
$attachmentId = $attachmentView[$i]->display_handler->display['id']; $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); $attachmentDisplay = $view->storage->getDisplay($attachmentId);
$attachmentDisplayOptions[$i] = $attachmentDisplay['display_options']; array_push($attachmentDisplayOptions, $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'];
} }
$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_manager = \Drupal::service('plugin.manager.charts');
$plugin = $plugin_manager->createInstance($library); $plugin = $plugin_manager->createInstance($options['library']);
$variables['height'] = $options['height'];
$variables['width'] = $options['width'];
$plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId); $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId);
} }
<?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 = '');
}
{% 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 }") }} {{ attach_library("#{ library }") }}
<div {{ attributes }} {{ content_attributes }} <div {{ attributes }} {{ content_attributes }}
style="{% if width is not empty %}width:{{ width }}px;{% endif %}{% if height is not empty %}height:{{ height }}px;{% endif %}"></div> style="{% if width is not empty %}width:{{ width }}px;{% endif %}{% if height is not empty %}height:{{ height }}px;{% endif %}"></div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment