You are here

public function Google::chartsGooglePopulateChartAxes in Charts 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/charts_google/src/Plugin/chart/Library/Google.php \Drupal\charts_google\Plugin\chart\Library\Google::chartsGooglePopulateChartAxes()

Utility to populate chart axes.

Parameters

array $element: The element.

array $chart_definition: The chart definition.

Return value

array Return the chart definition.

1 call to Google::chartsGooglePopulateChartAxes()
Google::preRender in modules/charts_google/src/Plugin/chart/Library/Google.php
Pre render.

File

modules/charts_google/src/Plugin/chart/Library/Google.php, line 165

Class

Google
Define a concrete class for a Chart.

Namespace

Drupal\charts_google\Plugin\chart\Library

Code

public function chartsGooglePopulateChartAxes(array $element, array $chart_definition) {

  /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
  $element_info = \Drupal::service('element_info');

  /** @var \Drupal\charts\TypeManager $chart_type_plugin_manager */
  $chart_type_plugin_manager = \Drupal::service('plugin.manager.charts_type');
  foreach (Element::children($element) as $key) {
    if ($element[$key]['#type'] === 'chart_xaxis' || $element[$key]['#type'] === 'chart_yaxis') {

      // Make sure defaults are loaded.
      if (empty($element[$key]['#defaults_loaded'])) {
        $element[$key] += $element_info
          ->getInfo($element[$key]['#type']);
      }

      // Populate the chart data.
      $axis = [];
      $axis['title'] = $element[$key]['#title'] ? $element[$key]['#title'] : '';
      $axis['titleTextStyle']['color'] = $element[$key]['#title_color'];
      $axis['titleTextStyle']['bold'] = $element[$key]['#title_font_weight'] === 'bold' ? TRUE : FALSE;
      $axis['titleTextStyle']['italic'] = $element[$key]['#title_font_style'] === 'italic' ? TRUE : FALSE;
      $axis['titleTextStyle']['fontSize'] = $element[$key]['#title_font_size'];

      // In Google, the row column of data is used as labels.
      if ($element[$key]['#labels'] && $element[$key]['#type'] === 'chart_xaxis') {
        foreach ($element[$key]['#labels'] as $label_key => $label) {
          $chart_definition['data'][$label_key + 1][0] = $label;
        }
      }
      $axis['textStyle']['color'] = $element[$key]['#labels_color'];
      $axis['textStyle']['bold'] = $element[$key]['#labels_font_weight'] === 'bold' ? TRUE : FALSE;
      $axis['textStyle']['italic'] = $element[$key]['#labels_font_style'] === 'italic' ? TRUE : FALSE;
      $axis['textStyle']['fontSize'] = $element[$key]['#labels_font_size'];
      $axis['slantedText'] = !empty($element[$key]['#labels_rotation']) ? TRUE : NULL;
      $axis['slantedTextAngle'] = $element[$key]['#labels_rotation'];
      $axis['gridlines']['color'] = $element[$key]['#grid_line_color'];
      $axis['baselineColor'] = $element[$key]['#base_line_color'];
      $axis['minorGridlines']['color'] = $element[$key]['#minor_grid_line_color'];
      $axis['viewWindowMode'] = isset($element[$key]['#max']) ? 'explicit' : NULL;
      $axis['viewWindow']['max'] = strlen($element[$key]['#max']) ? (int) $element[$key]['#max'] : NULL;
      $axis['viewWindow']['min'] = strlen($element[$key]['#min']) ? (int) $element[$key]['#min'] : NULL;

      // Merge in axis raw options.
      if (!empty($element[$key]['#raw_options'])) {
        $axis = NestedArray::mergeDeepArray([
          $element[$key]['#raw_options'],
          $axis,
        ]);
      }

      // Multi-axis support only applies to the major axis in Google charts.
      $chart_type = $chart_type_plugin_manager
        ->getDefinition($element['#chart_type']);
      $axis_index = $element[$key]['#opposite'] ? 1 : 0;
      if ($element[$key]['#type'] === 'chart_xaxis') {
        $axis_keys = !$chart_type['axis_inverted'] ? [
          'hAxis',
        ] : [
          'vAxes',
          $axis_index,
        ];
      }
      else {
        $axis_keys = !$chart_type['axis_inverted'] ? [
          'vAxes',
          $axis_index,
        ] : [
          'hAxis',
        ];
      }
      $axis_drilldown =& $chart_definition['options'];
      foreach ($axis_keys as $axis_key) {
        $axis_drilldown =& $axis_drilldown[$axis_key];
      }
      $axis_drilldown = $axis;
    }
  }
  return $chart_definition;
}