You are here

private function Highcharts::populateAxes in Charts 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/charts_highcharts/src/Plugin/chart/Library/Highcharts.php \Drupal\charts_highcharts\Plugin\chart\Library\Highcharts::populateAxes()

Populate axes.

Parameters

array $element: The element.

array $chart_definition: The chart definition.

Return value

array Return the chart definition.

1 call to Highcharts::populateAxes()
Highcharts::preRender in modules/charts_highcharts/src/Plugin/chart/Library/Highcharts.php
Pre render.

File

modules/charts_highcharts/src/Plugin/chart/Library/Highcharts.php, line 500

Class

Highcharts
Defines a concrete class for a Highcharts.

Namespace

Drupal\charts_highcharts\Plugin\chart\Library

Code

private function populateAxes(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_type = $element[$key]['#type'] === 'chart_xaxis' ? 'xAxis' : 'yAxis';
      $axis = [];
      $axis['type'] = $element[$key]['#axis_type'];
      $axis['title']['text'] = $element[$key]['#title'];
      $axis['title']['style']['color'] = $element[$key]['#title_color'];
      $axis['categories'] = $element[$key]['#labels'];
      $axis['labels']['style']['color'] = $element[$key]['#labels_color'];
      $axis['labels']['rotation'] = $element[$key]['#labels_rotation'];
      $axis['gridLineColor'] = $element[$key]['#grid_line_color'];
      $axis['lineColor'] = $element[$key]['#base_line_color'];
      $axis['minorGridLineColor'] = $element[$key]['#minor_grid_line_color'];
      $axis['endOnTick'] = isset($element[$key]['#max']) ? FALSE : NULL;
      $axis['max'] = $element[$key]['#max'];
      $axis['min'] = $element[$key]['#min'];
      $axis['opposite'] = $element[$key]['#opposite'];
      if ($axis['labels']['rotation']) {
        $chart_type = $chart_type_plugin_manager
          ->getDefinition($element['#chart_type']);
        if ($axis_type === 'xAxis' && !$chart_type['axis_inverted']) {
          $axis['labels']['align'] = 'left';
        }
        elseif ($axis_type === 'yAxis' && $chart_type['axis_inverted']) {
          $axis['labels']['align'] = 'left';
        }
      }

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