You are here

private function C3::populateData 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::populateData()

Populate data.

Parameters

array $element: The element.

array $chart_definition: The chart definition.

Return value

array Return the chart definition.

1 call to C3::populateData()
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 259

Class

C3
Define a concrete class for a Chart.

Namespace

Drupal\charts_c3\Plugin\chart\Library

Code

private function populateData(array &$element, array $chart_definition) {
  $type = $this
    ->getType($element['#chart_type']);
  $types = [];

  /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
  $element_info = \Drupal::service('element_info');
  $children = Element::children($element);
  $children = array_filter($children, function ($child) use ($element) {
    return $element[$child]['#type'] === 'chart_data';
  });
  $columns = $chart_definition['data']['columns'] ?? [];
  $column_keys = array_keys($columns);
  $columns_key_start = $columns ? end($column_keys) + 1 : 0;
  foreach ($children as $key) {
    $child_element = $element[$key];

    // Make sure defaults are loaded.
    if (empty($child_element['#defaults_loaded'])) {
      $child_element += $element_info
        ->getInfo($child_element['#type']);
    }
    if ($child_element['#color'] && $type !== 'gauge') {
      $chart_definition['color']['pattern'][] = $child_element['#color'];
    }
    if (!in_array($type, [
      'pie',
      'donut',
    ])) {
      $series_title = strip_tags($child_element['#title']);
      $types[$series_title] = $child_element['#chart_type'] ? $this
        ->getType($child_element['#chart_type']) : $type;
      if ($type !== 'scatter') {
        $columns[$columns_key_start][] = $series_title;
        foreach ($child_element['#data'] as $datum) {
          if (gettype($datum) === 'array') {
            if ($type === 'gauge') {
              array_shift($datum);
            }
            $columns[$columns_key_start][] = array_map('strip_tags', $datum);
          }
          else {
            $columns[$columns_key_start][] = strip_tags($datum);
          }
        }
      }
      else {
        $row = [];
        $row[$series_title][0] = $series_title;
        $row[$series_title . '_x'][0] = $series_title . '_x';
        foreach ($child_element['#data'] as $datum) {
          $row[$series_title][] = $datum[0];
          $row[$series_title . '_x'][] = $datum[1];
        }
        $chart_definition['data']['xs'][$series_title] = $series_title . '_x';
        foreach ($row as $value) {
          $columns[] = $value;
        }
        $columns = array_values($columns);
      }
    }
    else {
      foreach ($child_element['#data'] as $datum) {
        $columns[] = $datum;
      }
    }
    $columns_key_start++;
  }
  if ($element['#stacking']) {
    $chart_definition['data']['groups'] = [
      array_keys($types),
    ];
  }
  $chart_definition['data']['types'] = $types;
  $chart_definition['data']['columns'] = $columns;
  return $chart_definition;
}