You are here

private function C3::populateAxes in Charts 5.0.x

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

Populate axes.

Parameters

array $element: The element.

array $chart_definition: The chart definition.

Return value

array Return chart definition.

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

File

modules/charts_c3/src/Plugin/chart/Library/C3.php, line 206

Class

C3
Define a concrete class for a Chart.

Namespace

Drupal\charts_c3\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');
  $children = Element::children($element);
  $axes = array_filter($children, function ($child) use ($element) {
    $type = $element[$child]['#type'];
    return $type === 'chart_xaxis' || $type === 'chart_yaxis';
  });
  $chart_type = $this
    ->getType($element['#chart_type']);
  if ($axes) {
    foreach ($axes as $key) {

      // Make sure defaults are loaded.
      if (empty($element[$key]['#defaults_loaded'])) {
        $element[$key] += $element_info
          ->getInfo($element[$key]['#type']);
      }
      $axis_type = $element[$key]['#type'] === 'chart_xaxis' ? 'x' : 'y';
      if ($axis_type === 'x') {
        $categories = $element[$key]['#labels'] ? array_map('strip_tags', $element[$key]['#labels']) : [];
        if (!in_array($chart_type, [
          'pie',
          'donut',
        ])) {
          if ($chart_type === 'scatter') {

            //
          }
          else {
            $chart_definition['data']['columns'][] = [
              'x',
            ];
            $chart_definition['data']['x'] = 'x';
            $categories_keys = array_keys($chart_definition['data']['columns']);
            $categories_key = end($categories_keys);
            foreach ($categories as $category) {
              $chart_definition['data']['columns'][$categories_key][] = $category;
            }
          }
        }
        else {
          $chart_definition['data']['columns'] = array_map(NULL, $categories, $chart_definition['data']['columns']);
        }
      }
    }
  }
  return $chart_definition;
}