You are here

private function C3::populateData in Charts 8.4

Same name and namespace in other branches
  1. 5.0.x 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 236

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'] ?? [];
  $columns_key_start = $columns ? end(array_keys($columns)) + 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']) {
      $chart_definition['color']['pattern'][] = $child_element['#color'];
    }
    if (!in_array($type, [
      'pie',
      'donut',
    ])) {
      $series_title = strip_tags($child_element['#title']);
      $columns[$columns_key_start] = [
        $series_title,
      ];
      if ($type === 'scatter') {
        $columns[$columns_key_start + 1] = [
          $series_title . '_x',
        ];
      }
      $types[$series_title] = $child_element['#chart_type'] ? $this
        ->getType($child_element['#chart_type']) : $type;
      if ($type !== 'scatter') {
        foreach ($child_element['#data'] as $datum) {
          if (gettype($datum) === 'array') {
            $columns[$columns_key_start][] = array_map('strip_tags', $datum);
          }
          else {
            $columns[$columns_key_start][] = strip_tags($datum);
          }
        }
      }
      else {
        foreach ($child_element['#data'] as $datum) {
          $columns[$columns_key_start][] = $datum[0];
          $columns[$columns_key_start + 1][] = $datum[1];
        }
      }
    }
    else {
      foreach ($child_element['#data'] as $datum) {
        $columns[] = array_map('strip_tags', $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;
}