You are here

charts.module in Charts 8

Charts module file that provides hook_theme.

File

charts.module
View source
<?php

/**
 * @file
 * Charts module file that provides hook_theme.
 */
use Drupal\charts\Util\Util;

/**
 * {@inheritdoc}
 */
function charts_theme($existing, $type, $theme, $path) {
  return array(
    'views_view_charts' => array(
      'variables' => array(
        'view' => NULL,
        'row' => NULL,
      ),
    ),
  );
}

/**
 * {@inheritdoc}
 */
function template_preprocess_views_view_charts(&$variables) {
  $options = $variables['view']->style_plugin->options;
  $service = \Drupal::service('charts.charts_attachment');
  $attachmentView = $service
    ->getAttachmentViews();
  $view = $variables['view'];
  $viewId = $view
    ->id();
  $displayId = $view->display_handler->display['id'];
  $chartId = $viewId . '__' . $displayId;
  $categoriesAttachment = array();
  $seriesDataAttachment = array();
  for ($i = 0; $i < count($attachmentView); $i++) {
    $attachmentId = $attachmentView[$i]->display_handler->display['id'];
    $attachmentDisplay = $view->storage
      ->getDisplay($attachmentId);
    $attachedValueField = $attachmentDisplay['display_options']['style']['options']['data_fields'];
    $combinedAttachmentPage = Util::removeUnselectedFields($attachedValueField);
    $attachmentColor = $attachmentView[$i]->style_plugin->options['field_colors'];
    $labelField = $attachmentView[$i]->style_plugin->options['label_field'];
    $dataAttachment = Util::viewsData($attachmentView[$i], $combinedAttachmentPage, $labelField, $attachmentColor);
    $dataAttachmentFormatted = Util::createChartableData($dataAttachment);
    for ($j = 0; $j < count($dataAttachmentFormatted[0]); $j++) {
      array_push($categoriesAttachment, $dataAttachmentFormatted[0][$j]);
    }
    for ($j = 0; $j < count($dataAttachmentFormatted[1]); $j++) {
      array_push($seriesDataAttachment, $dataAttachmentFormatted[1][$j]);
    }
  }
  $library = $view->style_plugin->options['library'];
  $variables['data'] = array();
  $labelField = $view->style_plugin->options['label_field'];
  $valueField = $view->style_plugin->options['data_fields'];
  $valueField = Util::removeUnselectedFields($valueField);
  $color = $view->style_plugin->options['field_colors'];
  $data = Util::viewsData($view, $valueField, $labelField, $color);
  $data = Util::createChartableData($data);
  $categories = $data[0];
  $seriesData = $data[1];
  $categories = array_merge($categories, $categoriesAttachment);
  $seriesData = array_merge($seriesData, $seriesDataAttachment);

  // Handles the toggling from one library to another.
  switch ($library) {
    case 'google':
      $googleData = charts_google_render_charts($categories, $seriesData);
      $googleOptions = charts_google_create_charts_options($options, $seriesData);
      $googleChartType = charts_google_create_chart_type($options);
      $variables['chart_type'] = 'google';
      $variables['attributes']['class'][0] = 'charts-google';
      $variables['attributes']['id'][0] = $chartId;
      $variables['content_attributes']['data-chart'][] = $googleData;
      $variables['attributes']['google-options'][1] = json_encode($googleOptions);
      $variables['attributes']['google-chart-type'][2] = json_encode($googleChartType);
      break;
    case 'highcharts':
      $highchart = charts_highcharts_render_charts($options, $categories, $seriesData);
      $variables['chart_type'] = 'highcharts';
      $variables['content_attributes']['data-chart'][] = json_encode($highchart);
      $variables['attributes']['id'][0] = $chartId;
      $variables['attributes']['class'][] = 'charts-highchart';
      break;
    case 'c3':
      $c3 = charts_c3_render_charts($options, $categories, $seriesData, $chartId);
      $variables['chart_type'] = 'c3';
      $variables['content_attributes']['data-chart'][] = json_encode($c3);
      $variables['attributes']['id'][0] = $chartId;
      $variables['attributes']['class'][] = 'charts-c3';
      break;
    default:
  }
}